C++ static classes & shared_ptr memory leaks

后端 未结 3 864
执念已碎
执念已碎 2021-01-12 00:19

I can\'t understand why does the following code produce memory leaks (I am using boost::shared_ptr with static class instance). Could someone help me?



        
3条回答
  •  心在旅途
    2021-01-12 00:25

    This is a memory leak. You are initializing a static instance of myclass called myclass_instance. You are also initializing the "shared_ptr myclass::ptr".

    According to Stroustrup[3], statics are initialized in the order that they are defined. Therefore you have the static definition of myclass_instance, which initializes the internal ptr on construction. However, you then have the definition of the static myclass::ptr, which invokes the default constructor for shared_ptr.

    This is an example of the classic statics ordering problem. The compiler thinks that myclass::ptr wasn't actually initialized, so there's no destruction of the original shared_ptr. Instead, it is just leaked.

    You'll need a bare pointer of some kind. If you're using C++11, you can do the Nifty Counter Technique with a ternary assignment statement which does a move to itself if you determine that the object has already been initialized. It's pretty rough, but it works.

    Here's how I'd do it in C++11:

    #include 
    #include 
    using std;
    
    #define _CRTDBG_MAP_ALLOC
    #define NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
    
    // Note that the count could also be a field in an initializer static used in the Nifty Counter
    // Technique covered in many texts.
    static int count = 0; // This gets implicitly initialized to 0 by the executable load into memory.
    static struct myclass {
       static shared_ptr ptr;
    
       myclass() {
          if (count++ == 0) {
             ptr = make_shared(0); //initialization
          }
       }          
    } myclass_instance;
    
    shared_ptr myclass::ptr = count == 0 ? make_shared(0) : move(myclass::ptr);
    
    int main() {
       _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF    | _CRTDBG_LEAK_CHECK_DF |
                      _CRTDBG_CHECK_ALWAYS_DF | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG));
       return 0;
    }
    

    See the following for more information:

    1. Lakos, J, 1996, Large-Scale C++ Software Design. Section 7.8.1.3, Addison Wesley, Reading, Massachusetts.
    2. Meyers, S, 2005, Effective C++, Third Edition. Item 4: Make sure that objects are initialized before they're used. Addison Wesley, Reading, Massachusetts.
    3. Stroustrup, B, 2000, The C++ Programming Language Special Edition. Section 10.4.9, Addison Wesley, Reading, Massachusetts.

提交回复
热议问题