有没有办法在进程结束时对静态成员执行某些操作?
我有一个使用 libxml2 的类。它具有静态成员,用于保存模式文件及其解析器的上下文。我正在使用 valgrind,它抱怨内存没有与模式上下文相关联地释放。这是因为您需要自己释放该内存。但是,由于这些上下文变量是静态的,因此我无法释放对象的破坏。有没有办法调用必要的自由函数,或者我应该忽略 valgrind。
I have a class that uses libxml2. It has static members which are used to hold context for a schema file and its parser. I'm using valgrind, and it's complaining that memory is not deallocated in connection with the schema context. This is because you need to free that memory yourself. However, since these context variables are static, I can't free on destruction of the object. Is there a way to call the necessary free functions, or should I just ignore valgrind.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在使用 XML 的类中声明另一个类。在其析构函数中,清理静态成员。现在给外部类另一个内部类类型的静态成员。由于拥有一个不平凡的析构函数,它将在程序退出时被清理,因此您的其他值也将被清理。
在您的 .cpp 文件之一中,在定义其他静态变量的同一位置定义
UseLibXml::static_cleanup
。Declare another class within your XML-using class. In its destructor, clean up your static members. Now give the outer class another static member of the inner class type. By virtue of having a non-trivial destructor, it will get cleaned up as the program exits, and thus your other values will get cleaned up, too.
Define
UseLibXml::static_cleanup
the same place you define the other static variables, in one of your .cpp files.设置一个
atexit
处理程序并在那里释放。或者忽略。Set up an
atexit
handler and free there. Or ignore.如果进程结束时出现 valgrind 错误,那么我不会担心它。为什么上下文变量是静态的?
您可以生成一个抑制文件,使 valgrind 忽略与静态上下文相关的错误。请参阅 valgrind 手册中的此页面:抑制错误
If the valgrind error is showing up when the process ends, then I wouldn't worry about it. Why are the context variables static though?
You can generate a suppressions file that will make valgrind ignore the errors associated with your static contexts. See this page in the valgrind manual: suppressing errors
我认为你可以忽略这个警告,因为它们不是内存泄漏。一旦您的应用程序退出,它们占用的内存将返回操作系统
I think you can ignore this warnings, since they are not memory leaks. They memory occupied by them will return to OS as soon as your application exit
我假设这些静态变量是指针?
假设你有:
我会将其更改为:
I assume these static variables are pointers?
Assuming you have:
I would change it to: