943,600 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1933
  • C RSS
Jun 10th, 2005
0

Advanced Question

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
maldini is offline Offline
6 posts
since Jun 2005
Jun 10th, 2005
0

Re: Advanced Question

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"
Reputation Points: 10
Solved Threads: 0
Newbie Poster
maldini is offline Offline
6 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Help, with transforming some numbers
Next Thread in C Forum Timeline: Initialization of Const Arrays by function





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC