C/C++ - Win32 : how can i find memory leaks?
-
i did a big program. now i have several memory leaks and they are hard to find. i did find several GDI memory leaks and i did MemoryDC and BitmapDC class's for avoid several memory leaks ;)
class MemoryDC
{
private:
HDC memoryDC;public:
MemoryDC ()
{
memoryDC=CreateCompatibleDC(NULL);
}operator HDC() const { return memoryDC; } ~MemoryDC () { DeleteDC(memoryDC); }
};
class BitmapDC
{
private:
MemoryDC hdcbitmap;
HGDIOBJ bitmapold;
HBITMAP bitmapcurrent;
int intwidth;
int intheight;void init(int width, int height) { bitmapcurrent = CreateBitmap(width, height, 1, 32, NULL); bitmapold = SelectObject(hdcbitmap, bitmapcurrent); intwidth=width; intheight=height; } void destroy() { SelectObject(hdcbitmap, bitmapold); DeleteObject(bitmapcurrent); }
public:
BitmapDC(int width, int height) { init(width, height); } BitmapDC() { init(1, 1); } void size(int width, int height) { destroy(); init(width, height); } int Width() { return intwidth; } int Height() { return intheight; } BitmapDC& operator= (const BitmapDC &bitmapsource) { if (this == &bitmapsource) // Same object? return \*this; destroy(); init(bitmapsource.intwidth, bitmapsource.intheight); BitBlt(bitmapsource, 0, 0, intwidth, intheight, bitmapsource, 0, 0, SRCCOPY); return \*this; } BitmapDC& operator= (const HBITMAP &bitmapsource) { destroy(); BITMAP bm; GetObject(bitmapsource,sizeof(bm),&bm); init(bm.bmWidth, bm.bmHeight); DrawHBITMAPtoHDC(bitmapsource,hdcbitmap); return \*this; } //testing the bitmao value if is nullptr bool operator != ( nullptr\_t ) const { return bitmapcurrent != nullptr; } bool operator==(const BitmapDC &other) const { return (other.bitmapcurrent == this->bitmapcurrent); } bool operator!=(const BitmapDC &other) const { return !(\*this == other); } operator HBITMAP() const { return bitmapcurrent; } operator HDC() const { return hdcbitmap; } ~BitmapDC() { destroy(); }
};
like you
-
i did a big program. now i have several memory leaks and they are hard to find. i did find several GDI memory leaks and i did MemoryDC and BitmapDC class's for avoid several memory leaks ;)
class MemoryDC
{
private:
HDC memoryDC;public:
MemoryDC ()
{
memoryDC=CreateCompatibleDC(NULL);
}operator HDC() const { return memoryDC; } ~MemoryDC () { DeleteDC(memoryDC); }
};
class BitmapDC
{
private:
MemoryDC hdcbitmap;
HGDIOBJ bitmapold;
HBITMAP bitmapcurrent;
int intwidth;
int intheight;void init(int width, int height) { bitmapcurrent = CreateBitmap(width, height, 1, 32, NULL); bitmapold = SelectObject(hdcbitmap, bitmapcurrent); intwidth=width; intheight=height; } void destroy() { SelectObject(hdcbitmap, bitmapold); DeleteObject(bitmapcurrent); }
public:
BitmapDC(int width, int height) { init(width, height); } BitmapDC() { init(1, 1); } void size(int width, int height) { destroy(); init(width, height); } int Width() { return intwidth; } int Height() { return intheight; } BitmapDC& operator= (const BitmapDC &bitmapsource) { if (this == &bitmapsource) // Same object? return \*this; destroy(); init(bitmapsource.intwidth, bitmapsource.intheight); BitBlt(bitmapsource, 0, 0, intwidth, intheight, bitmapsource, 0, 0, SRCCOPY); return \*this; } BitmapDC& operator= (const HBITMAP &bitmapsource) { destroy(); BITMAP bm; GetObject(bitmapsource,sizeof(bm),&bm); init(bm.bmWidth, bm.bmHeight); DrawHBITMAPtoHDC(bitmapsource,hdcbitmap); return \*this; } //testing the bitmao value if is nullptr bool operator != ( nullptr\_t ) const { return bitmapcurrent != nullptr; } bool operator==(const BitmapDC &other) const { return (other.bitmapcurrent == this->bitmapcurrent); } bool operator!=(const BitmapDC &other) const { return !(\*this == other); } operator HBITMAP() const { return bitmapcurrent; } operator HDC() const { return hdcbitmap; } ~BitmapDC() { destroy(); }
};
like you
-
i did a big program. now i have several memory leaks and they are hard to find. i did find several GDI memory leaks and i did MemoryDC and BitmapDC class's for avoid several memory leaks ;)
class MemoryDC
{
private:
HDC memoryDC;public:
MemoryDC ()
{
memoryDC=CreateCompatibleDC(NULL);
}operator HDC() const { return memoryDC; } ~MemoryDC () { DeleteDC(memoryDC); }
};
class BitmapDC
{
private:
MemoryDC hdcbitmap;
HGDIOBJ bitmapold;
HBITMAP bitmapcurrent;
int intwidth;
int intheight;void init(int width, int height) { bitmapcurrent = CreateBitmap(width, height, 1, 32, NULL); bitmapold = SelectObject(hdcbitmap, bitmapcurrent); intwidth=width; intheight=height; } void destroy() { SelectObject(hdcbitmap, bitmapold); DeleteObject(bitmapcurrent); }
public:
BitmapDC(int width, int height) { init(width, height); } BitmapDC() { init(1, 1); } void size(int width, int height) { destroy(); init(width, height); } int Width() { return intwidth; } int Height() { return intheight; } BitmapDC& operator= (const BitmapDC &bitmapsource) { if (this == &bitmapsource) // Same object? return \*this; destroy(); init(bitmapsource.intwidth, bitmapsource.intheight); BitBlt(bitmapsource, 0, 0, intwidth, intheight, bitmapsource, 0, 0, SRCCOPY); return \*this; } BitmapDC& operator= (const HBITMAP &bitmapsource) { destroy(); BITMAP bm; GetObject(bitmapsource,sizeof(bm),&bm); init(bm.bmWidth, bm.bmHeight); DrawHBITMAPtoHDC(bitmapsource,hdcbitmap); return \*this; } //testing the bitmao value if is nullptr bool operator != ( nullptr\_t ) const { return bitmapcurrent != nullptr; } bool operator==(const BitmapDC &other) const { return (other.bitmapcurrent == this->bitmapcurrent); } bool operator!=(const BitmapDC &other) const { return !(\*this == other); } operator HBITMAP() const { return bitmapcurrent; } operator HDC() const { return hdcbitmap; } ~BitmapDC() { destroy(); }
};
like you
For GDI memory leaks see the MSDN blog Debugging a GDI Resource Leak[^]. You should also check the rest of your program for non-GDI memory leaks. Don't forget to check for library and Windows API functions that may allocate memory.
-
For GDI memory leaks see the MSDN blog Debugging a GDI Resource Leak[^]. You should also check the rest of your program for non-GDI memory leaks. Don't forget to check for library and Windows API functions that may allocate memory.
how can check the rest of your program for non-GDI memory leaks, if i don't know how? :( what i can do, like i did with joystick, is comment some code, until i see where is it ;)
-
Member 11545824 wrote:
can anyone advice me?
Why are you duplicating what is already provided by the Windows GDI+ classes[^]?
why???? i never used GDI+ ;)
-
why???? i never used GDI+ ;)
-
how can check the rest of your program for non-GDI memory leaks, if i don't know how? :( what i can do, like i did with joystick, is comment some code, until i see where is it ;)
Finding memory leaks is a difficult task. First you need to detect memory leaks. This can be done by adding some checks to your application (usually in debug builds). See the MSDN articles Finding Memory Leaks[^] and Using UMDH to Find a User-Mode Memory Leak[^]. There are also some tools to help you. An example is this CP article: Memory(-Leak) and Exception Trace (CRT and COM Leaks)[^]. Then execute your application and start specific parts to see where the leaks occur. This should give you the information which source files may contain a leak. Finally check the source files.
-
Finding memory leaks is a difficult task. First you need to detect memory leaks. This can be done by adding some checks to your application (usually in debug builds). See the MSDN articles Finding Memory Leaks[^] and Using UMDH to Find a User-Mode Memory Leak[^]. There are also some tools to help you. An example is this CP article: Memory(-Leak) and Exception Trace (CRT and COM Leaks)[^]. Then execute your application and start specific parts to see where the leaks occur. This should give you the information which source files may contain a leak. Finally check the source files.
sorry i use GNU compiler
-
how can check the rest of your program for non-GDI memory leaks, if i don't know how? :( what i can do, like i did with joystick, is comment some code, until i see where is it ;)
Member 11545824 wrote:
how can check the rest of your program for non-GDI memory leaks, if i don't know how? :(
One way is to replace operators new(), delete(), etc. with operators that log memory allocations. For example, an pseudo-code implementation of new() and delete() could look like this:
std::map< void*, std::size_t > allocations;
void* operator new( std::size_t size )
{
void* p = std::malloc( size );if ( p != nullptr )
{
allocations.insert( make_pair( p, size ) );
return p;
}throw std::bad_alloc();
}void operator delete( void* p )
{
if ( allocations.find( p ) == allocations.end() )
{
// handle case of deallocation of unallocated memory
}std::free( p ); allocations.erase( allocations.find( p ) );
}
Note that the code will probably not compile; I just wrote it quickly as an example. An additional function, called when your program exits, can ensure that the allocations map is empty. If it is not, the size(s) of the blocks may give you an idea as to what blocks were not freed.
If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill