Advanced Question

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2005
Posts: 6
Reputation: maldini is an unknown quantity at this point 
Solved Threads: 0
maldini maldini is offline Offline
Newbie Poster

Advanced Question

 
0
  #1
Jun 10th, 2005
Hi

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




  1. HGLOBAL hGlobal = GlobalAlloc( GMEM_FIXED, aJpegSize );
  2. void* p = GlobalLock(hGlobal);
  3. memcpy((char *)p,imageptr,aJpegSize);
  4. GlobalUnlock(hGlobal);
  5. IStream* pStream = NULL;
  6.  
  7. if ( CreateStreamOnHGlobal( hGlobal, TRUE, &pStream ) == S_OK);
  8. {
  9. if ( aPicture == NULL )
  10. delete aPicture;
  11.  
  12. aPicture = Image::FromStream(pStream,0);
  13. }
  14. pStream->Release();
  15. 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:


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

I'm really stuck, any idea could help!
Thanks in advance,
maldini
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 6
Reputation: maldini is an unknown quantity at this point 
Solved Threads: 0
maldini maldini is offline Offline
Newbie Poster

Re: Advanced Question

 
0
  #2
Jun 10th, 2005
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"
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC