# Help! How to free memory used by surfaces?

This topic is 4715 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, Here's my problem, let's say that I have bitmaps for enemy_A and enemy_B. Each of them are needed in different stages thus there are no instances in which both are in the same stage. What I want is to use the surface I've previously allocated to load bitmaps of enemy_A to enemy_B. Knowing that neither of them will be needed in memory at the same time, this seems to be a great memory saver since I don't have to make surfaces solely for enemy_B. Here's where it gets really messy, As I continously did this strategy, the game becomes slower and slower. I believe that it's because when I load the bitmaps for enemy_B to the surface previously allocated for enemy_A, the memory consumed by enemy_A isn't really released! Meaning that I'm just adding and adding more memory consuming bitmaps into the surface (that explains why it gets gradually slower and slower). My question is that how do I free memory that is consumed by loading the surface with bitmaps before I load in another set into the same surface? I've tried to use ->Release() but it totally quits the game. I've also tried pointing the surfaces to NULL before loading another bitmap but it totally didn't help. Please, I really need help in this. THANKS VERY MUCH IN ADVANCE!!

##### Share on other sites
I'd be happy to help you. I have a few ideas in my head of what you might be doing wrong but we will need to see some code from the problem area. Also, what language and API are you using?

##### Share on other sites
Sounds like a memory leak alright, without knowing what API and language you're using Im guessing you have code like:

and that you "reuse" it by going:

most API's will allocate a new surface in that case and you'll simply leak the old surface.

Just an educated guess though. Tell us more about your platform and post some code and someone is bound to have a good answer for ya.

##### Share on other sites
Hi again,

I'm really sorry (how stupid of me not mentioning important facts). I'm using C++ and DirectDraw. Here's the function where I load a bitmap to the surface:

//function to load a series of sequenced bitmaps to array of surfacesvoid PutBitmapInSurface(LPDIRECTDRAWSURFACE7 source_surface[],						char *bitmap_name,						DWORD color_key,						int no_of_surfaces){  //loop up to how many surfaces  for(int x = 0; x < no_of_surfaces; x++)  {     //first off, we need to save the bitmap name to buffer     char buffer[100];     sprintf(buffer,"%s%d.bmp",bitmap_name,x+1);      //put bitmap into surface if surface     source_surface[x] = DDLoadBitmap(global_lpdd7,buffer,0,0);     //attach color key if color_key is not NULL     if(color_key != NULL)	ddraw.AttachColorKey(source_surface[x],color_key,                             color_key,DDCKEY_SRCBLT);     //ddraw is just a DirectX 7 class that I made.  }   }

...first I load bitmaps of enemy_A (which is at stage1) then after I'm done with stage1, I'll proceed to stage2 at which point I'm going to load bitmaps of enemy_B:

//like this//for enemy_A PutBitmapInSurface(lpdds_enemy,"/Graphics/Enemy/Enemy ",                                 PINK,                                 PINK,                                 enemy1.max_no_of frames);//SECTION X//after finishing stage 1, i'll then load enemy_B's bitmapsPutBitmapInSurface(lpdds_enemy,"/Graphics/Enemy/Enemy ",                                 PINK,                                 PINK,                                 enemy2.max_no_of frames);

...using same surface lpdds_enemy[]. I've tried adding code in "//SECTION X" where I'll point lpdds_enemy[] to NULL but that doesn't work and also trying to use the release() member function but that exits the game completely! Pls. Help me on this. I really appreciate it. Also, what should I type to put codes in kinda like the box where I type in my message so it's easier to navigate.

[Edited by - KuroKage on March 29, 2005 12:25:18 PM]

##### Share on other sites
SHIT!! The code looks messy! I really need help in putting the code in a box (what do you call that anyway) so that I can edit my previous post.

##### Share on other sites
for short snippets you can use [ code] and [ /code] without the spaces and for longer and syntax highlighted [ source] and [ /source] will do fine. And I havent used the DDLoadBitmap function but I guess it returns a new surface so what you probably need todo is to first free all those surfaces that you're later overwriting.

##### Share on other sites
Hi again,

DigitalDelusion, Thanks a lot! Really, I appreciate it. If you haven't used the DDLoadBitmap function, may I ask what function you use to load bitmaps in surfaces? Also, is there a way I can free the surfaces? It seems that Reinitializing it or using IDirectDrawSurface7::SetSurfaceDesc to change the characteristic of an existing surfaces doesn't free them up! PLEASE HELP AGAIN!!

AGAIN, THANKS IN ADVANCE!! (probably more of this, if this isn't solved)

##### Share on other sites
Mostly I use SDL a free crossplatform library providing graphics and other services. It's a truly great API and very easy to get up 'n' running.

Am sorry to have to point you to the API documentation but that's the best I can do right now since I haven't used barebones DX in a very long time.

Im quite sure that you want to call Release to get it to free its memory.

Sorry that I can't give more details than that :/

##### Share on other sites
Can you show your usage of the release() function? It should simply be a matter of releasing the surface as follows:
surface_pointer->release();surface_pointer = NULL;