English 中文(简体)
Is there a way to do something to static members on process end?
原标题:

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.

最佳回答

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.

问题回答

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

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:

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;
        }
};




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签