有没有办法在进程结束时对静态成员执行某些操作?

发布于 2024-08-15 06:21:41 字数 163 浏览 8 评论 0原文

我有一个使用 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 技术交流群。

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

发布评论

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

评论(5

温柔一刀 2024-08-22 06:21:41

在使用 XML 的类中声明另一个类。在其析构函数中,清理静态成员。现在给外部类另一个内部类类型的静态成员。由于拥有一个不平凡的析构函数,它将在程序退出时被清理,因此您的其他值也将被清理。

class UseLibXml {
  static int xmlvar;

  struct StaticCleanup {
    ~StaticCleanup() {
      CleanUpLibXmlVar(UseLibXml::xmlvar);
    }
  };

  static StaticCleanup static_cleanup;
};

在您的 .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.

class UseLibXml {
  static int xmlvar;

  struct StaticCleanup {
    ~StaticCleanup() {
      CleanUpLibXmlVar(UseLibXml::xmlvar);
    }
  };

  static StaticCleanup static_cleanup;
};

Define UseLibXml::static_cleanup the same place you define the other static variables, in one of your .cpp files.

听风吹 2024-08-22 06:21:41

设置一个 atexit 处理程序并在那里释放。或者忽略。

Set up an atexit handler and free there. Or ignore.

栖竹 2024-08-22 06:21:41

如果进程结束时出现 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

雨的味道风的声音 2024-08-22 06:21:41

我认为你可以忽略这个警告,因为它们不是内存泄漏。一旦您的应用程序退出,它们占用的内存将返回操作系统

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

娇俏 2024-08-22 06:21:41

我假设这些静态变量是指针?

假设你有:

class X
{
     private:
        static Plop*   staicXData;  // Initialised in the code.
};

我会将其更改为:

Class X
{
    private:
        static Plop&  getStatoc()
        {
            static Plop  data;     // Auto created on first use.
                                   // Destroyed on program exit.
            return data;
        }
};

I assume these static variables are pointers?

Assuming you have:

class X
{
     private:
        static Plop*   staicXData;  // Initialised in the code.
};

I would change it to:

Class X
{
    private:
        static Plop&  getStatoc()
        {
            static Plop  data;     // Auto created on first use.
                                   // Destroyed on program exit.
            return data;
        }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文