c++ - How can I properly release a ID3D11ShaderResourceView** to avoid memory leaks? -
i have private member of class.
id3d11shaderresourceview** texture_pool; i set null in class constructor this:
texture_pool = null; then initialize in class initialization:
texture_pool = new id3d11shaderresourceview*[texture_count]; (int n = 0; n < texture_count; n++) texture_pool[n] = null; and in class destructor release this:
for (int n = 0; n < texture_count; n++) safe_release(texture_pool[n]); safe_delete_array(texture_pool); but program crashes when exit , debbuger point @ line cause of crash:
for (int n = 0; n < texture_count; n++) safe_release(texture_pool[n]); if remove line works smooth, i'm worried possible memory leaks if dont release array.
so, can remove line , cleaned porperly?
these defined lines release , delete instructions:
#define safe_release(p) { if ( (p) ) { (p)->release(); (p) = 0; } } #define safe_delete(a) if( (a) != null ) delete (a); (a) = null; #define safe_delete_array(a) if( (a) != null ) delete[] (a); (a) = null;
you have shown this:
#define safe_release(p) { if ( (p) ) { (p)->release(); (p) = 0; } } #define safe_delete(a) if( (a) != null ) delete (a); (a) = null; #define safe_delete_array(a) if( (a) != null ) delete[] (a); (a) = null; try instead:
#ifndef safe_release #define safe_release(p) { if(p) { (p)->release(); (p) = null; } } #endif #ifndef safe_delete #define safe_delete(p) { if(p) { delete (p); (p) = nullptr; } } #endif #ifndef safe_delete_array #define safe_delete_array(p) { if(p) { delete[](p); (a) = nullptr; } } #endif when working macros need careful uses of "()" & "{}".
you not need (p) != nullptr check since when have if(p) next statement if check returns true indicating pointer valid , not null.
you missing #ifndef , #endif around macro definitions.
if compiler not support nullptr can use null instead, nullptr cleaner , readable.
Comments
Post a Comment