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

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"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.