程序退出时崩溃
每当我退出程序时,它都会给我这个异常“0xC0000022:进程已请求访问对象,但尚未被授予这些访问权限。”
它在 _file.c 中名为 _lock_file 的函数末尾处中断。
在尝试缩小问题的原因后,我发现如果我删除程序中的所有 fclose() 函数调用然后清理并重建我的程序,它不会崩溃。即使函数本身从未被调用,它仍然会崩溃。显然这个解决方案并不理想。
当我尝试使用 fstream 时,它在程序启动时产生了类似的崩溃。
还值得一提的是,我的程序使用了 SDL。
编辑:有人要求一个最小的例子,这就是我想到的。
main.cpp
#include <stdlib.h>
#include <SDL.h>
/*...*/
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")
/*...*/
int main( int argc, char **argv)
{
if(false)
fclose(NULL);
return 0;
}
draw.cpp
/*...*/
如果我运行它,它会在退出时崩溃,就像我上面提到的那样。是的,draw.cpp 已完全注释掉,但如果我将其从项目中删除,程序将运行良好。所有其他文件均已从项目中删除。
Edit2:为了回应 karlphillip,我决定仔细检查它是否确实在运行,并且似乎在这个示例开始时它实际上崩溃了。
它也是一个 Win32 项目。
Whenever I exit my program it gives me this exception "0xC0000022: A process has requested access to an object, but has not been granted those access rights."
It breaks right at the end of a function called _lock_file in _file.c.
After trying to narrow down what the cause of the problem is I found out that it does not crash if I remove all fclose() function calls in my program then cleaning and rebuilding my program. Even if the function itself is never called it will still crash. Obviously this solution is not ideal.
When I tried to use fstream instead it produced a similar crash at the start of the program.
It's also worth mentioning that my program uses SDL.
Edit: Someone requested a minimal example and this is what I cam up with.
main.cpp
#include <stdlib.h>
#include <SDL.h>
/*...*/
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")
/*...*/
int main( int argc, char **argv)
{
if(false)
fclose(NULL);
return 0;
}
draw.cpp
/*...*/
If I run this it will crash on exit just like I mentioned above. And yes the draw.cpp is completely commented out, but if I remove it from the project the program will run fine. All other files were removed from the project.
Edit2: In response to karlphillip I decided to double check if it is actually running and it seems that it is actually crashing at the start with this example.
Also it is a Win32 project.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
退出时崩溃通常意味着堆在程序执行期间被损坏。尝试使用内存检查器来查找位置。尝试使用 _CrtDumpMemoryLeaks()
Having a crash on exit usually means that the heap is corrupted during program execution. Try using a memory checker to find where. Try using _CrtDumpMemoryLeaks()
您的主程序是否使用与构建 SDL 库相同的运行时库(调试 DLL、调试、发布 DLL、发布等)?这通常(但并非总是)会导致奇怪的问题,并且在运行时遇到这种奇怪的行为时,这将是我的第一个调用端口。
(如果您在构建时收到 LNK4098 警告,这就是它试图告诉您的内容,您确实需要正确修复它;警告文本建议的“解决方案”绝非如此。)
另一种选择是内存损坏。考虑运行调试版本,并在启动时调用以下命令:
这将激活更彻底的堆检查。 (当你的程序在打开的情况下运行时,如果它在运行时分配了很多东西,你可能不得不去泡一杯茶。)如果然后在一个内存分配函数中“崩溃”——它实际上是一个断言,但你不能总是分辨出来——然后在该调用和之前对内存管理函数的调用之间的某个时刻,某些东西覆盖了一些它不应该拥有的内存。你可以从那里开始了解什么。
-编辑:“_CRTDBG_REPORT_FLAG_DF”,可能是“_CRTDBG_REPORT_FLAG”。
Are you using the same runtime library (Debug DLL, Debug, Release DLL, Release, etc.) for your main program as was used to build the SDL library? That can often (but not always) cause odd problems, and would be my first port of call when getting this sort of odd behaviour at runtime.
(If you get an LNK4098 warning when building, this is what it is trying to tell you, and you really need to fix it properly; the "solution" the text of the warning suggests is anything but.)
Another option is memory corruption. Consider running a debug build, and calling the following on startup:
This activates more thorough heap checking. (You might have to go and make a cup of tea when your program runs with this switched on, if it allocates lots of stuff while it's running.) If then "crashes" in one of the memory allocation functions -- it's actually an assert, you can't always tell though -- then at some point between that call, and the previous call to a memory management function, something has overwritten some memory it should not have. And you can take it from there, to find out what.
-Edit: "_CRTDBG_REPORT_FLAG_DF", was probably intended to be "_CRTDBG_REPORT_FLAG".
退出时崩溃也可能是由静态变量破坏和访问已清理的对象引起的。
检查静态对象并确保它们的析构函数不会导致崩溃。
Crashing on exit can also be caused by static variables destructing and accessing objects that have already been cleaned up.
Check you static objects and ensure their destructors are not causing the crash.
您如何知道您的应用程序正在被执行?在调用 main() 后立即添加调试:
您要创建什么类型的项目?控制台、Win32 还是其他?
我发现 这篇文章非常有趣。
How do you know your application is being executed in the first place? Add a debug right after main() is called:
What kind of project are you creating? Console, Win32 or something else?
I find this post very interesting.