C++仅 Eclipse 外部的分段错误
我在 Eclipse 中开发了一个 C++ 应用程序。当在 Eclipse 外部运行时,在连续 (4) 次用户操作后会出现分段错误。起初看起来并没有什么特别的。我想我应该使用 Eclipse 来调试应用程序并找到错误。但是,当我从 Eclipse 运行该应用程序时,它运行得很好。有人对如何解决这个问题有建议吗?
谢谢。
代码库太大,无法在此处显示,但我已经缩小了导致分段错误的代码行范围:
SDL_Surface* textSurface = TTF_RenderText_Solid( font, text.c_str(), color );
奇怪的是,它在同一调用失败之前调用了这行代码数百次。字体和颜色的值是在其他地方定义并每次传入的常量。所以它们每次都是完全相同的。文本的值为“-”。
I have developed a C++ application in Eclipse. When run outside of Eclipse, it takes a segmentation fault after a consistent number (4) of user actions. It did not seem like anything special at first. I thought I would just use Eclipse to debug through the application and find the bug. However, when I run the application from Eclipse, it runs just fine. Does anyone have recommendations on how to troubleshoot this problem??
Thanks.
The codebase is too large to display here, but I've narrowed down the line of code which causes the segmentation fault:
SDL_Surface* textSurface = TTF_RenderText_Solid( font, text.c_str(), color );
The odd part about this is it calls this line of code hundreds of times before failing on the exact same call. The values of font and color are constants defined elsewhere and passed in each time. So they are the exact same every time. The value of text is "-".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先确保您在 eclipse 中运行的版本与命令行发布与调试上运行的版本相同。
有些bug会因为不同的编译器设置或者只是被调试而改变。这些通常是由未初始化的数据引起的。像 valgrind 这样的内存调试器工具可以帮助您发现此类问题,因为它们可以随机化未初始化数据的内容。
还要确保编译设置中的所有警告均已打开。然后编译器会警告您潜在的错误内容。
编辑:
是的 -Wall 和 -pedantic 可以获取所有警告。
有时,很难发现内存错误,该错误实际上并不是发生段错误的地方。段错误的发生只是由于之前未被注意到的错误。最好使用像 valgrind 这样的内存调试器。否则你将不得不仔细检查大量代码。
First make sure you run the same version within eclipse as on the commandline release vs debug.
Some bugs will change because of different compiler settings or just being debugged. These are often caused by uninitialized data. Memory debugger tools like valgrind can you help find these kind of problems as they can randomize the contents of uninitialized data.
Also make sure all warnings are on in your compile settings. The compiler will then warn you about potentialy incorrect stuff.
Edit:
Yes -Wall and -pedantic is fine for getting all warnings.
Sometimes with hard to find memory errors the error is not actually where the segfault occurs. The segfault only occurs because of earlier errors that went unnoticed. Best to use a memory debugger like valgrind. Otherwise you will have to scrutinize a lot of code.
在 Node 的推荐下(参见原始问题的评论),我通过 Valgrind 运行了我的应用程序。清理了 Valgrind 识别的内存管理问题后,我的问题就消失了。谢谢!
At the recommendation of Node (see comments on original question, I ran my app through Valgrind. After cleaning up memory management issues identified by Valgrind, my problem is gone. Thank you!