简体   繁体   English

如何调试这种内存损坏?

[英]How to debug such memory corruption?

int main () {
       allocating_resource();
       call_other_libs();
       ...
       release_resource();
       return 0;
}

After the program runs, the main returns. 程序运行后,主程序返回。

And after it accessed returne 0 , the stack pointer points to a bad address in main(), then the executable crashed. 在访问returne 0 ,堆栈指针指向main()中的错误地址,然后可执行文件崩溃。

Program received signal SIGSEGV, Segmentation fault. 程序收到信号SIGSEGV,分段故障。

GI __libc_free(mem=0x3f21a843) at malloc.c:2020 GI __libc_free(mem = 0x3f21a843)在malloc.c:2020

I guess there are some illegal memory accesses, but the code base is too large to check. 我猜有一些非法的内存访问,但是代码库太大,无法检查。 Review and analyse all the code is not realistic. 复查和分析所有代码都不现实。

Disable some code is also unacceptable due to large code base. 禁用某些代码也是不可接受的,因为代码量很大。

With core dump there 's no hint I can use due to it's crashed at the main stack and after the return clause executed. 对于核心转储,没有任何提示可以使用,因为它在主堆栈和执行return子句后崩溃了。

I know how to use gdb, but the project is so large that it seems to hard to find the root cause. 我知道如何使用gdb,但是该项目太大,似乎很难找到根本原因。

valgrind --tool=memcheck seems to no help. valgrind --tool = memcheck似乎没有帮助。

How to solve such problem? 如何解决这样的问题?

You can use GDB. 您可以使用GDB。 This stackoverflow link has details about how to debug using GDB. stackoverflow链接包含有关如何使用GDB进行调试的详细信息。 If you google, you can get many such helpful links on GDB. 如果您使用Google,则可以在GDB上获得许多此类有用的链接。
You can also use valgrind , if you are sure about memory related issues. 如果您确定与内存相关的问题,也可以使用valgrind

There is one more memory profiler called MemProf . 还有一个名为MemProf的内存分析器。 It gives memory allocated for each function and can also detect issues. 它为每个功能分配了内存,还可以检测问题。 See the link for details. 有关详细信息,请参见链接。
There are also c++ specific tools for memory profiling like: 还有一些特定于c ++的内存分析工具,例如:
mempro and MTuner . memproMTuner You can use trial version for free. 您可以免费使用试用版。

Since we are not having some kind of code access here, I'll have to assume that in some magic way free is called when the scope of main is destroyed (maybe the use of smart pointers? maybe some sophisticated macro definitions... can't really tell). 由于我们这里没有某种代码访问权限,因此我不得不假设当main的作用域被破坏时,以某种神奇的方式调用了free(也许使用了智能指针?也许还有一些复杂的宏定义...可以真的不知道)。 I would try to recreate the problem in the following manner: 我将尝试通过以下方式重新创建问题:

int main () 
{
       {
           allocating_resource();
           call_other_libs();
           ...
           release_resource();
       }
       return 0;
}

or 要么

  int main()
  {
    mainhelper();
    return 0;
  }

where mainhelper will contain the main code. 其中mainhelper将包含主要代码。

Hopefully after those steps the problem will persist and the logs won't be completely damaged, as you suggest, because the program is terminating. 希望在执行这些步骤之后,该问题将继续存在,并且不会因您的建议而完全损坏日志,因为该程序将终止。

also, try to play with the optimization flags (more like in disabling it) and add -ggdb3 debug flag (assuming gcc here). 另外,尝试使用优化标志(更像是禁用它)并添加-ggdb3调试标志(此处为gcc)。 Maybe it will help you in some bizarre way. 也许它将以某种奇怪的方式帮助您。

Some other posts concerning this matter, if you haven´t checked them out yet: 有关此问题的其他一些帖子(如果您尚未签出):

segmentation fault after main returns 主返回后的分段错误

Program receives SIGSEGV error after return 0 程序返回0后收到SIGSEGV错误

they are all stating more of the same: valgrind should be able to deliver an answer. 它们都说明更多相同点: valgrind应该能够给出答案。

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

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