gcc、c++:静态字符串成员变量导致堆损坏/分段错误

发布于 2024-09-15 15:15:36 字数 823 浏览 13 评论 0原文

我有一个使用动态加载库的大型应用程序。在程序结束时终止它,要么出现段错误,要么发出一条消息“glibc 检测到损坏的双链表”。看看 valgrind 的输出,我认为情况是这样的: 假设我们有三个文件:

utilities.c        - compiled with -fPIC and used ar and ranlib to create utilities.a.
dynamicallyloaded.c- compiled with -fPIC and -shared and linked with utlities.a to generate dynamicallyloaded.so 
main.c             - compiled with -fPIC and linked with utilities.a to create main. main dynamically loads and uses dynamicallyloaded.so .
utilities.h        - delclared a class IfTrackerFile with AubFileName as a static string member like   static string          AubFileName;

utilities.cpp      - defines the static variable: string IfTrackerFile::AubFileName;

valgrind out 表示线路上存在无效的 free/delete/delete : 字符串 IfTrackerFile::AubFileName;

我不知道发生了什么事。 非常感谢这方面的任何帮助/指导。

I have a big application that uses the dynamically loaded libraries. At the end of program while terminating it either segfaults or spits a message "glibc detected corrupted double-linked list". Looking at the valgrind output I think this is what the case is:
let say we have three files:

utilities.c        - compiled with -fPIC and used ar and ranlib to create utilities.a.
dynamicallyloaded.c- compiled with -fPIC and -shared and linked with utlities.a to generate dynamicallyloaded.so 
main.c             - compiled with -fPIC and linked with utilities.a to create main. main dynamically loads and uses dynamicallyloaded.so .
utilities.h        - delclared a class IfTrackerFile with AubFileName as a static string member like   static string          AubFileName;

utilities.cpp      - defines the static variable: string IfTrackerFile::AubFileName;

valgrind out says that there was invalid free/delete/delete on the line :
string IfTrackerFile::AubFileName;

i have no clue what's going on.
truly appreciate any help/direction in this regard.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静水深流 2024-09-22 15:15:36

我的猜测是,您最终得到了 IfTrackerFile::AubFileName 的两个不同副本。一种是从 utilities.a 直接将其拉入程序中,另一种是动态加载 dynamicallyloaded.so 时。我猜测这会混淆程序关闭时销毁所有静态和全局对象的系统,并且您最终调用了析构函数两次。

我认为您不应该以这种方式混合 .a 和 .so 文件。基本上,一个好的经验法则是永远不要将 .so 文件链接到 .a,即使您将 -fPIC 代码放入.a

My guess is that you ended up with two different copies of IfTrackerFile::AubFileName. One by pulling it in directly in your program from utilities.a and another when you dynamically loaded dynamicallyloaded.so. I'm guessing that this confused the system for destructing all static and global objects on program shutdown and you ended up calling a destructor twice.

I don't think you should be mixing .a and .so files in this manner. Basically, a good rule of thumb is to never link a .so file against a .a, even if you put -fPIC code in the .a.

零度℉ 2024-09-22 15:15:36

这是一个盲目的尝试,但问题可能是全局对象。将类实例化为全局变量意味着该变量在调用 main() 之前实例化。这意味着构造函数在 main() 开始之前调用,析构函数在 main 完成之后调用。构造函数和析构函数的调用顺序也是不确定的。

我的建议是将所有全局对象(不是普通旧数据 - POD - 类型的全局变量)转换为指针,该指针在 main 开头实例化并在 main 末尾销毁。

This is a shot in the dark but the issue could be global objects. Instantiating a class as a global variable means that the variable is instantiated before main() is called. This means the constructor is called before main() starts and the destructor is called after main has completed. The order the constructor and destructor are called is also indeterminate.

My advice is to turn all global objects (global variables that are not plain old data - POD - types) into pointer which are instantiated at the beginning of main and destroyed at the end of main.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文