Issue #2 resolved

Memory leak by CRT debugger upon when using glxx::load_version_X_X

Anonymous created an issue

when loading glxx via the function glxx::load_version_X_X, upon exiting, the CRT memory leak detector will catch a leak.

This is because the pointer "default_version_3_3_loader" created by load_version_3_3 function is not deleted when the program quits

Might possibly wrap the "default_version_3_3_loader" pointer in a class to ensure memory deallocation of the pointer at shutdown. {{{

!c++

class Version_3_3_Wrapper { public: Version_3_3_Wrapper() : default_version_3_3_loader(nullptr) { }

  ~Version_3_3_Wrapper()
  {
       delete default_version_3_3_loader;
  }

  gl_version_3_3::Loader* operator->()
  {
       return default_version_3_3_loader;
  };

  gl_version_3_3::Loader* default_version_3_3_loader ;

};

Version_3_3_Wrapper version_3_3_wrapper;

bool load_version_3_3() { version_3_3_wrapper.default_version_3_3_loader = new gl_version_3_3::Loader();

return version_3_3_wrapper->isLoaded();

} }}}

regards

Comments (5)

  1. Anonymous

    p.s might create a templated version of the wrapper which can be used for all default_version_x_x_loader pointer. This would possibly allow minimal change in the original code

    e.g

    template<typename ty>
    class VersionWrapper
    {
    public:
        VersionWrapper() : ptr(nullptr) {}
        ~VersionWrapper() { delete ptr; }
        void operator=(T* value) { ptr = value; }
        T* operator->() { return ptr; }
        T* ptr;
    };
    
    VersionWrapper<gl_version_3_3::Loader> default_version_3_3_loader;
    
    ////////gl_version_3_3::Loader* default_version_3_3_loader//////////
    
    bool load_version_3_3() 
    {
        default_version_3_3_loader = new gl_version_3_3::Loader();
        return version_3_3_wrapper->isLoaded();
    }
    
  2. yotis repo owner
    • changed status to open

    You caught me there ;) I will try your last suggestion sometime in the next days.

    This snippet looks like a smart pointer. [will it be ok to bring in a dependency to boost?]

    ps. I was thinking about refactoring the default_loaders concept (email me directly if you want to talk about it)

  3. yotis repo owner

    Commit 337f2959e1c3 fixes this issue.

    The default loader pointers used by glxx::load_.. functions are now auto_ptr<> objects. auto_ptr works as suggested by the report (destructors are called on program exit) without reinventing smart pointers.

    Please confirm the fix to close the issue.

  4. Log in to comment