0

Hi

Does anyone know why the following code causes me a memory leak:

HGLOBAL hGlobal = GlobalAlloc( GMEM_FIXED, aJpegSize );
void* p = GlobalLock(hGlobal);
memcpy((char *)p,imageptr,aJpegSize);	
GlobalUnlock(hGlobal);
IStream* pStream = NULL;

if ( CreateStreamOnHGlobal( hGlobal, TRUE, &pStream ) == S_OK);
			{
				if ( aPicture == NULL )
					delete aPicture;
			
				aPicture = Image::FromStream(pStream,0); 	
			}	
		pStream->Release();
		GlobalFree(hGlobal);

<< moderator edit: added [code][/code] tags >>

NOTE: the imageptr is a pointer to my jpeg image, and aJpegSize is the size of the image. I later go on to do:

Graphics graphics( dc );
graphics.DrawImage( w->aPicture,  0, 0, 320, 240 );

I'm really stuck, any idea could help!
Thanks in advance,
maldini

1
Contributor
1
Reply
2
Views
12 Years
Discussion Span
Last Post by maldini
0

Found my own answer:::::::


For those interested,,,,, the pointer aPicture ( which is of type Image ) must be delete at some point, or else it never call the destructor, which in tern never deallocates its memory.

Therefore I now have this somewhere in my code:


if ( w->aPicture != NULL )
{
delete w->aPicture; // Relese the memory allocated by aPicture
w->aPicture = 0;
}

Cheers
maldini

"What does'nt kill you, makes you stronger"

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.