# Help! How to free memory used by surfaces?

## Recommended Posts

##### 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;

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628305
• Total Posts
2981967

• 9
• 13
• 11
• 12
• 11