简体   繁体   English

代码::阻止调试模式:如果构建和运行,我的代码会崩溃,但如果调试/继续,则不会崩溃

[英]Code::Blocks debug mode: my code crashes if build and run, but not if debug/continue

My IDE is Code::Blocks. 我的IDE是Code :: Blocks。 I am in debug mode, I click on the "red play button" for Debug / Continue and my code runs fine. 我处于调试模式,单击“红色播放按钮”进行调试/继续,我的代码运行良好。 Then, always in debug mode, I click on the "green play button" for Run and my code crashes. 然后,始终在调试模式下,单击“绿色播放按钮”进行运行,我的代码崩溃。 Any ideas on why this happens? 关于发生这种情况的任何想法? How can I find the bug in my code if clicking on Debug / Continue everything runs fine? 如果单击“调试/继续”,一切正常,如何在代码中找到错误? I cannot include a copy of my code because it is too long. 我不能包含我的代码的副本,因为它太长了。

After a trial and error debugging, I discovered that the program crashes when I use "delete[]" to deallocate the memory block pointed to by a pointer allocated with "new". 经过调试和错误调试之后,我发现当我使用“ delete []”释放由“ new”分配的指针所指向的内存块时,程序崩溃。 Strange thing is that this dynamic allocation and deallocation is in a for loop, and the program crashes after a few loops, so not right at the beginning. 奇怪的是,这种动态分配和释放是在for循环中进行的,程序在几个循环后崩溃,所以一开始并不正确。

Thanks for any suggestions you may have. 感谢您的任何建议。

EDIT: Allocation is with "new[]". 编辑:分配是与“ new []”。 I cannot paste the code because it is too long. 我无法粘贴代码,因为它太长。 The same allocation and deallocation is used in my code for other pointers without problems, but apparently only some of them cause the code to crash when deallocated. 在我的代码中,其他指针使用相同的分配和释放没有问题,但显然只有其中一些会在释放时导致代码崩溃。 How can I debug if when I click on "Debug / Continue" the program does not crash but it crashes when I click on "Run"? 如果我单击“调试/继续”时程序没有崩溃,但是当我单击“运行”时崩溃了,该如何调试?

One major difference between debug and release modes is that often in debug, all memory will be initialized to zero. 调试和释放模式之间的主要区别在于,通常在调试中,所有内存都将初始化为零。 Often this makes things work better in debug than in release. 通常,这使调试工作比发布工作更好。

However, if you are accessing a pointer which has been initialized to 0, then this will cause a segmentation fault or similar. 但是,如果访问的指针已初始化为0,则将导致分段错误或类似情况。 In release mode, you might just be lucky and have this pointer looking at memory that's accessible. 在发布模式下,您可能很幸运,并且使该指针查看了可访问的内存。

Thanks for your help. 谢谢你的帮助。 In the end my program was trying to access a pointer out of bounds, so I solved it. 最后,我的程序试图越界访问指针,所以我解决了它。 Weird thing is that the debugger did not show any error/warning (eg segmentation fault). 奇怪的是,调试器未显示任何错误/警告(例如,分段错误)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM