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 )
{
// make sure that the memory will be freed at the program end
sm_handlers.DeleteContents(TRUE);
sm_handlers.Append( 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 );
}
@ -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 )
{
// png_structp png_ptr;
// png_infop info_ptr;
// unsigned char *ptr, **lines, *ptr2;
// int transp,bit_depth,color_type,interlace_type;
//png_uint_32 width, height;
//unsigned int i;
// VZ: as this function uses setjmp() the only fool proof error handling
// method is to use goto (setjmp is not really C++ dtors friendly...)
image->Destroy();
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) return FALSE;
unsigned int i;
unsigned char **lines = NULL;
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)
{
png_destroy_read_struct( &png_ptr, (png_infopp) NULL, (png_infopp) NULL );
return FALSE;
}
goto error;
if (setjmp(png_ptr->jmpbuf))
{
png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL );
return FALSE;
}
goto error;
if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
{
png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL );
return FALSE;
}
goto error;
png_set_read_fn( png_ptr, &stream, _PNG_stream_reader);
png_uint_32 width,height;
@ -521,42 +521,36 @@ bool wxPNGHandler::LoadFile( wxImage *image, wxInputStream& stream )
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 );
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_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 );
image->Create( width, height );
if (!image->Ok())
{
png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL );
return FALSE;
}
goto error;
unsigned char **lines = (unsigned char **)malloc( height * sizeof(unsigned char *) );
lines = (unsigned char **)malloc( height * sizeof(unsigned char *) );
if (lines == NULL)
{
image->Destroy();
png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL );
return FALSE;
}
goto error;
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)
{
image->Destroy();
for (unsigned int n = 0; n < i; n++) free( lines[n] );
free( lines );
png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL );
return FALSE;
for ( unsigned int n = 0; n < i; n++ )
free( lines[n] );
goto error;
}
}
// loaded successfully!
{
int transp = 0;
png_read_image( png_ptr, lines );
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 );
if (transp)
@ -627,8 +622,34 @@ bool wxPNGHandler::LoadFile( wxImage *image, wxInputStream& stream )
{
image->SetMask( FALSE );
}
}
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);
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;
}
@ -1380,6 +1401,14 @@ wxBitmap wxImage::ConvertToBitmap() const
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)
{
case 8: