PNG loading & multiple heaps (WIN2000) - solved

Basically the fix involves adding a function the SDL_image API that
frees a surface created by the IMG_Load functions.

who needs fast solution right now, here comes reparkit:

------------------ add to SDL_image.h
extern DECLSPEC void IMG_FreeSurfacePNG(SDL_Surface *surface);
------------------ cut here

------------------ add to IMG_png.c in section with defined PNG_LOAD
/* Free a surface created by IMG_LoadPNG function only !

IMG_LoadPNG_RW functions allocates surface pixels this way:
surface->pixels = malloc(surface->h*surface->pitch);
other allocations for surface is done by SDL functions

Under WinNT & Win2000 each module has its own heap.
All memory allocated in the module must be freed in that module too !

(surface->pixels) allocation causes that calling SDL_FreeSurface
on returned surface from IMG_LoadPNG_RW fails.
Reason:
SDL_FreeSurface resides in SDL module
and calls free() on memory allocated
by IMG_LoadPNG_RW in SDL_image module.

this is temporary hack, problem occurs only when loading PNGs
other IMG_LoadXXX code is “heap-safe”
*/
void IMG_FreeSurfacePNG(SDL_Surface surface)
{
/
Deallocate surface memory, which resides in the SDL_images’s heap /
if ( surface->pixels &&
((surface->flags & SDL_PREALLOC) != SDL_PREALLOC) ) {
free(surface->pixels);
surface->pixels = NULL;
}
/
Free anything that’s not NULL, and not the screen surface */
SDL_FreeSurface(surface);
}
------------------- cut here

my buggy code should be fixed to:
SDL_Surface* LoadImage(SDL_RWops *src)
{
SDL_Surface *s;
SDL_Surface *temp;
temp = IMG_LoadPNG_RW(src);
if (!temp) return NULL;
s = SDL_DisplayFormat(temp);
IMG_FreeSurfacePNG(temp); // here is change
return s;
}

works on my W2k
better solution could be letting SDL do pixels allocation instead of
SDL_image’s PNG loader

thanks
Antonin Hildebrand aka Woid
[@Antonin_Hildebrand]

Under WinNT & Win2000 each module has its own heap.
All memory allocated in the module must be freed in that module too !

do you know is there is a way for modules to share a single memory 

manager that does all the alloc/free/garbage collection for everyone
linked?

-dv

If by module, you mean a separate library linked into an executable or as a
DLL, then yes, you can do that. The trick is to use a DLL for the runtime
library.

In Visual C++, Go to Project | Settings | C/C++ | Code Generation. In the
"Use run-time library" box chose “Debug Multithreaded DLL”. Do this for all
the libraries and DLLs and the main executable.

In my case, I only needed to do this in Debug mode, as it was just the debug
heap moaning that I hadn’t freed the memory when it was really being freed.
Release was fine with the statically linked runtime library.

Francis> -----Original Message-----

From: daniel_v at bigpond.com
Sent: 12 April 2000 16:07
To: sdl at lokigames.com
Subject: [SDL] Re: PNG loading & multiple heaps (WIN2000) - solved

Under WinNT & Win2000 each module has its own heap.
All memory allocated in the module must be freed in that
module too !

do you know is there is a way for modules to share a
single memory
manager that does all the alloc/free/garbage collection for everyone
linked?

-dv