启动多线程后出现问题
程序的Debug版本执行时弹出对话框,提示出现错误:
Debug Assertion Failed!
Program: G:\vc6\CommApp.exe
File:wincore.cpp
line:895
打开MFC\SRC\wincore.cpp,其895行为:
ASSERT((CWnd*)p == this); // must be us
紧接着有以下注释:
// Note: if either of the above asserts fire and you are
// writing a multithreaded application, it is likely that
// you have passed a C++ object from one thread to another
// and have used that object in a way that was not intended.
// (only simple inline wrapper functions should be used)
//
// In general, CWnd objects should be passed by HWND from
// one thread to another. The receiving thread can wrap
// the HWND with a CWnd object by using CWnd::FromHandle.
//
// It is dangerous to pass C++ objects from one thread to
// another, unless the objects are designed to be used in
// such a manner.
刚好我的程序里面用到了多线程,如果没有启动那个线程(注释掉相关代码),就不会弹出上述对话框,但是线程函数里面并没有用到线程函数传递的参数,只是用到了一个全局指针变量,程序是基于对话框的程序,指针指向主程序窗口即主对话框。这样用不行吗?
另外,在release版本中,启动那个线程也没有上述错误出现,应该是release版本不包括调试信息的缘故,但dubug版本出现错误而release没错说明程序还是有问题吧。