memory leak plugged

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@1387 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
1999-01-12 23:48:14 +00:00
parent e29b83a455
commit 4698648ff8

View File

@@ -362,11 +362,17 @@ bool wxImage::SaveFile( wxOutputStream& stream, int type )
void wxImage::AddHandler( wxImageHandler *handler ) void wxImage::AddHandler( wxImageHandler *handler )
{ {
// make sure that the memory will be freed at the program end
sm_handlers.DeleteContents(TRUE);
sm_handlers.Append( handler ); sm_handlers.Append( handler );
} }
void wxImage::InsertHandler( wxImageHandler *handler ) void wxImage::InsertHandler( wxImageHandler *handler )
{ {
// make sure that the memory will be freed at the program end
sm_handlers.DeleteContents(TRUE);
sm_handlers.Insert( handler ); sm_handlers.Insert( handler );
} }
@@ -482,37 +488,31 @@ static void _PNG_stream_writer( png_structp png_ptr, png_bytep data, png_size_t
bool wxPNGHandler::LoadFile( wxImage *image, wxInputStream& stream ) bool wxPNGHandler::LoadFile( wxImage *image, wxInputStream& stream )
{ {
// png_structp png_ptr; // VZ: as this function uses setjmp() the only fool proof error handling
// png_infop info_ptr; // method is to use goto (setjmp is not really C++ dtors friendly...)
// unsigned char *ptr, **lines, *ptr2;
// int transp,bit_depth,color_type,interlace_type;
//png_uint_32 width, height;
//unsigned int i;
image->Destroy(); image->Destroy();
png_structp png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING, unsigned int i;
(voidp) NULL, (png_error_ptr) NULL, (png_error_ptr) NULL ); unsigned char **lines = NULL;
if (!png_ptr) return FALSE; png_infop info_ptr = NULL;
png_infop info_ptr = png_create_info_struct( png_ptr ); png_structp png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING,
(voidp) NULL,
(png_error_ptr) NULL,
(png_error_ptr) NULL );
if (!png_ptr)
goto error;
info_ptr = png_create_info_struct( png_ptr );
if (!info_ptr) if (!info_ptr)
{ goto error;
png_destroy_read_struct( &png_ptr, (png_infopp) NULL, (png_infopp) NULL );
return FALSE;
}
if (setjmp(png_ptr->jmpbuf)) if (setjmp(png_ptr->jmpbuf))
{ goto error;
png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL );
return FALSE;
}
if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA) if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
{ goto error;
png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL );
return FALSE;
}
png_set_read_fn( png_ptr, &stream, _PNG_stream_reader); png_set_read_fn( png_ptr, &stream, _PNG_stream_reader);
png_uint_32 width,height; png_uint_32 width,height;
@@ -521,42 +521,36 @@ bool wxPNGHandler::LoadFile( wxImage *image, wxInputStream& stream )
png_read_info( png_ptr, info_ptr ); png_read_info( png_ptr, info_ptr );
png_get_IHDR( png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, (int*) NULL, (int*) NULL ); png_get_IHDR( png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, (int*) NULL, (int*) NULL );
if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_expand( png_ptr ); if (color_type == PNG_COLOR_TYPE_PALETTE)
png_set_expand( png_ptr );
png_set_strip_16( png_ptr ); png_set_strip_16( png_ptr );
png_set_packing( png_ptr ); png_set_packing( png_ptr );
if (png_get_valid( png_ptr, info_ptr, PNG_INFO_tRNS)) png_set_expand( png_ptr ); if (png_get_valid( png_ptr, info_ptr, PNG_INFO_tRNS))
png_set_expand( png_ptr );
png_set_filler( png_ptr, 0xff, PNG_FILLER_AFTER ); png_set_filler( png_ptr, 0xff, PNG_FILLER_AFTER );
image->Create( width, height ); image->Create( width, height );
if (!image->Ok()) if (!image->Ok())
{ goto error;
png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL );
return FALSE;
}
unsigned char **lines = (unsigned char **)malloc( height * sizeof(unsigned char *) ); lines = (unsigned char **)malloc( height * sizeof(unsigned char *) );
if (lines == NULL) if (lines == NULL)
{ goto error;
image->Destroy();
png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL );
return FALSE;
}
for (unsigned int i = 0; i < height; i++) for (i = 0; i < height; i++)
{ {
if ((lines[i] = (unsigned char *)malloc(width * (sizeof(unsigned char) * 4))) == NULL) if ((lines[i] = (unsigned char *)malloc(width * (sizeof(unsigned char) * 4))) == NULL)
{ {
image->Destroy(); for ( unsigned int n = 0; n < i; n++ )
for (unsigned int n = 0; n < i; n++) free( lines[n] ); free( lines[n] );
free( lines ); goto error;
png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL );
return FALSE;
} }
} }
// loaded successfully!
{
int transp = 0; int transp = 0;
png_read_image( png_ptr, lines ); png_read_image( png_ptr, lines );
png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL ); png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL );
@@ -616,7 +610,8 @@ bool wxPNGHandler::LoadFile( wxImage *image, wxInputStream& stream )
} }
} }
for (unsigned int j = 0; j < height; j++) free( lines[j] ); for ( unsigned int j = 0; j < height; j++ )
free( lines[j] );
free( lines ); free( lines );
if (transp) if (transp)
@@ -627,8 +622,34 @@ bool wxPNGHandler::LoadFile( wxImage *image, wxInputStream& stream )
{ {
image->SetMask( FALSE ); image->SetMask( FALSE );
} }
}
return TRUE; return TRUE;
error:
wxLogError(_("Couldn't load a PNG image - probably file is corrupted."));
if ( image->Ok() )
{
image->Destroy();
}
if ( lines )
{
free( lines );
}
if ( png_ptr )
{
if ( info_ptr )
{
png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL );
free(info_ptr);
}
else
png_destroy_read_struct( &png_ptr, (png_infopp) NULL, (png_infopp) NULL );
}
return FALSE;
} }
@@ -702,7 +723,7 @@ bool wxPNGHandler::SaveFile( wxImage *image, wxOutputStream& stream )
free(data); free(data);
png_write_end( png_ptr, info_ptr ); png_write_end( png_ptr, info_ptr );
png_destroy_write_struct( &png_ptr, (png_infopp)NULL ); png_destroy_write_struct( &png_ptr, (png_infopp)&info_ptr );
} }
return TRUE; return TRUE;
} }
@@ -1380,6 +1401,14 @@ wxBitmap wxImage::ConvertToBitmap() const
gdk_image_put_pixel( mask_image, x, y, 0 ); gdk_image_put_pixel( mask_image, x, y, 0 );
} }
if (HasMask())
{
if ((r == r_mask) && (b == b_mask) && (g == g_mask))
gdk_image_put_pixel( mask_image, x, y, 1 );
else
gdk_image_put_pixel( mask_image, x, y, 0 );
}
switch (bpp) switch (bpp)
{ {
case 8: case 8: