您的位置:首页 > 大数据 > 人工智能

AfxWinMain中pThread->InitInstance()出错的解决方法

2014-05-10 10:00 429 查看
最近在写程序时,在Debug下能正常运行,但是换到Release下就报一个中断错误:“0x71ea1cbc(mfc100.dll) 处未处理的异常: 0xC0000005: 读取位置 0x00000000 时发生访问冲突”,并且,错误箭头指到了winmain.cpp文件中AfxWinMain函数内的if (!pThread->InitInstance())语句,经过分析是pThread为NULL,这就让我搞不清楚了,都没有进入我程序中的main函数,怎么会报这个错误呢。而且,我发现导致这个错误的原因是我加入了一个头文件#include<afx.h>导致的。就在网上找资料,还是stackoverflow比较牛叉啊,通过【http://stackoverflow.com/questions/20242974/mfc-application-crash-immediately-after-start】,内容如下:

If I understand the question correctly, you've created an empty project and just added these lines of code to a new cpp file.

If you want to use Afx support, or MFC, you need to state that when you create the project. I suggest you let the IDE create a non-empty project, check that builds
and runs, then start adding code to it.

pthread
should
be initialised by the framework for you - if you allow it to automatically generate the supoorting code for you. From memory, at very least it creates a global (arggg) variable called
theApp
of
type
CWinApp
,
which will initialise things it needs, like threads.

Go back through (the sample of) the book very carefully and follow all the instructions. If that doesn't work, get a better book. Or buy the whole book. Or work through the tutorials on
the msdn.
看过以后,发现是我建的工程不适合用#include<afx.h>,因为我建的Qt的工程(控制台程序也是如此),没有在建工程时没有设置为MFC类型,就没有提前初始化pThread(它是在建project时自动初始化的)。

另外,类似的错误及解决方法可参考:
http://bbs.csdn.net/topics/390349358
由于本人对MFC不熟悉,就不想再重新建一个MFC工程了,想继续在Qt工程中运行,怎么办呢?因为我用到了CString,所以才引入了头文件#include<afx.h>,后来在网上看到,可以用 ATL,#inlcude <atlstr.h>,就可以用CString了。(Win32 Application不能包含MFC,如果要用CString,也可以这么做)

另外,TRACE在MFC支持程序里用,不能在WIN32 Application程序里用;TRACE使用的是AfxTrace对象,这个对象只有在支持MFC的程序才有;

OutputDebugString("这个好用啊");TRACE是MFC中的定义,在win32代码中可以直接用OutputDebugString来达到相同效果。

OK,问题到此就解决了。我觉得能不用#include<afx.h>的时候,尽量别用。用了以后,会带来很多问题。

==================================================

另外,VC中Debug正常,Release异常的解决方法可参考:
http://blog.csdn.net/Tony_Jas/article/details/5875083 http://blog.csdn.net/frankiewang008/article/details/8021411
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: