Memory leak in ref class that has member variable of native type pointer


  • Here is the definition of the class

    public ref class Direct3DImageSource sealed : Windows::UI::Xaml::Media::Imaging::SurfaceImageSource
      virtual ~Direct3DImageSource() { free(m_teximagepixels); }
      Direct3DImageSource(unsigned char*texels, int pixelWidth, int pixelHeight, bool isBlurred, bool isOpaque);
      unsigned char* m_teximagepixels;

    Memory pointed by member variable m_teximagepixels is dynamically allocated at runtime. However, the deconstructor is never called, even if the objects of Direct3DImageSource is out of life time scope. Of couse, I can free the memory somewhere in my code, but what is the more elegant way to do this?

    Friday, November 30, 2012 2:11 PM