SDL_FreeImage doesn't free memory

Hi all,
I?ve got a little problem with SDL_FreeSurface()

The Problem is: it doesn’t free memory.

I often change the Background image of my game, and when I load a new one, I free the old one.
But the used memory of my application just increases.

I am trying to free the Surfaces like this:
//(Images is a selfmade assoziative array and the Surface is stored in it in a .)

		if(Images.Get(BackgroundFilename) != NULL)
		{
			SDL_Surface * pSurface = Images.Get(BackgroundFilename);
			SDL_FreeSurface(pSurface);
			pSurface = NULL;
		}	

just for Info: I’m loading images like this.

	pSurface = SDL_CreateRGBSurface(SDL_SWSURFACE,width,height,bpp,NULL,NULL,NULL,NULL);

	char * buffer = new char[size];
	file.read((char*)buffer,size);

	SDL_LockSurface(pSurface);
	
	memcpy((char*)s->pixels,buffer,size);

	delete[] buffer;

	SDL_UnlockSurface(pSurface);

thank you for your help,

Nils

Hi all,
I?ve got a little problem with SDL_FreeSurface()

The Problem is: it doesn’t free memory.

I often change the Background image of my game, and when I load a new one, I free the old one.
But the used memory of my application just increases.

I am trying to free the Surfaces like this:
//(Images is a selfmade assoziative array and the Surface is stored in it in a .)

Hint: watch out when using STL vectors – at least the implementation
for MSVC6 is buggy IIRC, and not thread safe. I found a memory
deallocation problem when using vector::insert() some months ago
(haven’t dared using STL since then :frowning: )

                    if(Images.Get(BackgroundFilename) != NULL)
                    {
                            SDL_Surface * pSurface = Images.Get(BackgroundFilename);
                            SDL_FreeSurface(pSurface);
                            pSurface = NULL;
                    }

just for Info: I’m loading images like this.

            pSurface = SDL_CreateRGBSurface(SDL_SWSURFACE,width,height,bpp,NULL,NULL,NULL,NULL);

            char * buffer = new char[size];
            file.read((char*)buffer,size);

            SDL_LockSurface(pSurface);

            memcpy((char*)s->pixels,buffer,size);

This is only correct if s->pitch == s->w, that is when the pixel data
of s is one fat array. It is probably true for SDL_SWSURFACE’s in
general, but it is still not the recommended way of copying memory to
a surface. Why not use the SDL_CreateRGBSurfaceFrom(…) API? Should
speed things up a bit too. But be careful – you have to explicitly
delete[] the s->pixels buffer after the SDL_FreeSurface() call later!On Wed, 2 Feb 2005 16:01:32 +0100, Nils Wogatzky wrote:

            delete[] buffer;

            SDL_UnlockSurface(pSurface);

thank you for your help,

Nils


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Hi again,

I now use the SDL_CreateRGBSurfaceFrom(…) and it works fine.

But my SDL_FreeSurface problem is still there.
The memory simply will not free.

The following code is the code to Load an image from a File.
I usually do not free the memory directly after I?ve loaded it, but
I thought this would be a nice test.
But the memory will not free !
I see no error in the following code.

// Allocate dynamic memory
char * buffer = new char[size];

// Fill the memory with RAW-Pixel-data
file.read(buffer,size);

// Free the allocated memory
delete [] buffer;

So I?d like to ask you, if anyone had problems like this before, or if anyone finds an error in this code
or whatever.

My Application is a Point’n’Click-Adventure - Engine an so I have to free the memory of the Background-Image
and the Objects’ Images when a Scene swaps.
But in my case the memory just increases. I?ve “pushed” the memory by manual scene-swapping to 50 MB,
but my Game should just use about 10MB, so I?ve got 40MB Memory-Trash which does not free.

So I hope you have any suggestions or hints or whatever,
but help !!!
:wink:

Nils>On Wed, 2 Feb 2005 16:01:32 +0100, Nils Wogatzky <@Nils_Wogatzky> wrote:

Hi all,
I?ve got a little problem with SDL_FreeSurface()

The Problem is: it doesn’t free memory.

I often change the Background image of my game, and when I load a new one, I free the old one.
But the used memory of my application just increases.

I am trying to free the Surfaces like this:
//(Images is a selfmade assoziative array and the Surface is stored in it in a .)

Hint: watch out when using STL vectors – at least the implementation
for MSVC6 is buggy IIRC, and not thread safe. I found a memory
deallocation problem when using vector::insert() some months ago
(haven’t dared using STL since then :frowning: )

                    if(Images.Get(BackgroundFilename) != NULL)
                    {
                            SDL_Surface * pSurface = Images.Get(BackgroundFilename);
                            SDL_FreeSurface(pSurface);
                            pSurface = NULL;
                    }

just for Info: I’m loading images like this.

            pSurface = SDL_CreateRGBSurface(SDL_SWSURFACE,width,height,bpp,NULL,NULL,NULL,NULL);

            char * buffer = new char[size];
            file.read((char*)buffer,size);

            SDL_LockSurface(pSurface);

            memcpy((char*)s->pixels,buffer,size);

This is only correct if s->pitch == s->w, that is when the pixel data
of s is one fat array. It is probably true for SDL_SWSURFACE’s in
general, but it is still not the recommended way of copying memory to
a surface. Why not use the SDL_CreateRGBSurfaceFrom(…) API? Should
speed things up a bit too. But be careful – you have to explicitly
delete[] the s->pixels buffer after the SDL_FreeSurface() call later!

            delete[] buffer;

            SDL_UnlockSurface(pSurface);

thank you for your help,

Nils


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Just for Info:

I?ve built my project under DEVCPP
and the world smiles with me.>Hi again,

I now use the SDL_CreateRGBSurfaceFrom(…) and it works fine.

But my SDL_FreeSurface problem is still there.
The memory simply will not free.

The following code is the code to Load an image from a File.
I usually do not free the memory directly after I?ve loaded it, but
I thought this would be a nice test.
But the memory will not free !
I see no error in the following code.

// Allocate dynamic memory
char * buffer = new char[size];

// Fill the memory with RAW-Pixel-data
file.read(buffer,size);

// Free the allocated memory
delete [] buffer;

So I?d like to ask you, if anyone had problems like this before, or if anyone finds an error in this code
or whatever.

My Application is a Point’n’Click-Adventure - Engine an so I have to free the memory of the Background-Image
and the Objects’ Images when a Scene swaps.
But in my case the memory just increases. I?ve “pushed” the memory by manual scene-swapping to 50 MB,
but my Game should just use about 10MB, so I?ve got 40MB Memory-Trash which does not free.

So I hope you have any suggestions or hints or whatever,
but help !!!
:wink:

Nils

On Wed, 2 Feb 2005 16:01:32 +0100, Nils Wogatzky <@Nils_Wogatzky> wrote:

Hi all,
I?ve got a little problem with SDL_FreeSurface()

The Problem is: it doesn’t free memory.

I often change the Background image of my game, and when I load a new one, I free the old one.
But the used memory of my application just increases.

I am trying to free the Surfaces like this:
//(Images is a selfmade assoziative array and the Surface is stored in it in a .)

Hint: watch out when using STL vectors – at least the implementation
for MSVC6 is buggy IIRC, and not thread safe. I found a memory
deallocation problem when using vector::insert() some months ago
(haven’t dared using STL since then :frowning: )

                    if(Images.Get(BackgroundFilename) != NULL)
                    {
                            SDL_Surface * pSurface = Images.Get(BackgroundFilename);
                            SDL_FreeSurface(pSurface);
                            pSurface = NULL;
                    }

just for Info: I’m loading images like this.

            pSurface = SDL_CreateRGBSurface(SDL_SWSURFACE,width,height,bpp,NULL,NULL,NULL,NULL);

            char * buffer = new char[size];
            file.read((char*)buffer,size);

            SDL_LockSurface(pSurface);

            memcpy((char*)s->pixels,buffer,size);

This is only correct if s->pitch == s->w, that is when the pixel data
of s is one fat array. It is probably true for SDL_SWSURFACE’s in
general, but it is still not the recommended way of copying memory to
a surface. Why not use the SDL_CreateRGBSurfaceFrom(…) API? Should
speed things up a bit too. But be careful – you have to explicitly
delete[] the s->pixels buffer after the SDL_FreeSurface() call later!

            delete[] buffer;

            SDL_UnlockSurface(pSurface);

thank you for your help,

Nils


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Nils,

I really must ask what you mean by “memory will not free”. Perhaps you
mean that you see no decrease in a memory usage statistic that is reported
by the OS? OS memory usage statistics show the amount of memory allocated
to the process as a whole. Each process has its own internal memory
allocation system.

When you use “new” in C++, you are allocating memory on your program’s
heap. Your program starts up with a default heap size. When the heap
becomes full, another chunk of memory is requested from the OS and added
to the heap. When memory is freed inside your program, the heap is not
automatically shrunk. A portion of the heap will not be freed (and
returned to the OS) if there is an allocated piece of data inside that
portion.

Perhaps what happens in your example is that the file.read() routine is
allocating memory on the heap. This allocation happens AFTER the buffer
is allocated. Thus the heap chunk still has allocated data in it, even
after the buffer is freed.

The heap management code is part of your C library: libc, crt, msvcrt, etc.

I hope this helps.

-Mike_L
Michael Leonhard
SDL_anim at tamale.net

Nils Wogatzky wrote:> Hi again,

I now use the SDL_CreateRGBSurfaceFrom(…) and it works fine.

But my SDL_FreeSurface problem is still there.
The memory simply will not free.

The following code is the code to Load an image from a File.
I usually do not free the memory directly after I?ve loaded it,
but I thought this would be a nice test.
But the memory will not free !
I see no error in the following code.

// Allocate dynamic memory
char * buffer = new char[size];

// Fill the memory with RAW-Pixel-data
file.read(buffer,size);

// Free the allocated memory
delete [] buffer;

Nils Wogatzky wrote:

Hi again,

I now use the SDL_CreateRGBSurfaceFrom(…) and it works fine.

But my SDL_FreeSurface problem is still there.
The memory simply will not free.

That’s documented I think. SDL_CreateRGBSurfaceFrom uses the provided
memory for your surface, and you have to free it yourself after you
destroy the surface.

Stephane