Antonio: Memory Leak in DipWrite

Antonio: Memory Leak in DipWrite

Postby byte-one » Tue Nov 12, 2013 3:43 pm

Antonio, i found, that in this function pDip should released with
Code: Select all  Expand view
hb_xfree( ( char * ) pDib );
on 3 positions. I write 1000 BMPs and then the computer-Memory are full in my case. I searched for the reason and found this.

Code: Select all  Expand view
BOOL DibWrite( LPSTR szFileName, HGLOBAL hDIB )
{

   long lSize;
   char * pDib;
   int hBmp;

   pDib = DibToStr( hDIB, &lSize );

   #ifndef UNICODE
   if( ( hBmp = _lcreat( szFileName, 0 ) ) != HFILE_ERROR )
   {
      _hwrite( hBmp, ( const char * ) pDib, lSize );
      _lclose( hBmp );
      GlobalUnlock( hDIB );
    hb_xfree( ( char * ) pDib );
      return TRUE;
   }
   #else
   if( ( hBmp = hb_fsCreate( ( unsigned char * ) szFileName, 0 ) ) != ( int ) INVALID_HANDLE_VALUE )
   {
      hb_fsWriteLarge( hBmp, ( unsigned char * ) pDib, lSize );
      hb_fsClose( hBmp );
      GlobalUnlock( hDIB );
    hb_xfree( ( char * ) pDib );
      return TRUE;
   }
   #endif
   else
   {
      GlobalUnlock( hDIB );
    hb_xfree( ( char * ) pDib );
      return FALSE;
   }
}
Regards,
Günther
---------------------------------
office@byte-one.com
User avatar
byte-one
 
Posts: 1048
Joined: Mon Oct 24, 2005 9:54 am
Location: Austria

Re: Antonio: Memory Leak in DipWrite

Postby Antonio Linares » Tue Nov 12, 2013 6:03 pm

Günther,

You are totally right. Many thanks! :-)

Included for next FWH build.
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42098
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Antonio: Memory Leak in DipWrite

Postby byte-one » Wed Nov 13, 2013 10:29 am

Antonio, all is ok now. Maybe you should check all hb_xalloc() and hb_xgrab() for corresponding hb_xfree(). Specially in C-functions, which returns only a pointer and this pointer are to released in the calling function.
Regards,
Günther
---------------------------------
office@byte-one.com
User avatar
byte-one
 
Posts: 1048
Joined: Mon Oct 24, 2005 9:54 am
Location: Austria

Re: Antonio: Memory Leak in DipWrite

Postby Antonio Linares » Wed Nov 13, 2013 11:10 am

Günther,

I constantly do it, though sometimes we may miss a case, like this one. thanks,
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42098
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 39 guests