[Solved]Allegro Game taking 300mb of RAM !

Started by
8 comments, last by newbie123 12 years, 12 months ago
hi guys

so i'm making sort of Engine / game kind of thing and i noticed when i open the game it takes about 300MB of RAM !!! WHY ??!!!

if i initialize BITMAP a lot does that take from my RAM ?

also i dont use destroy BITMAP function does that has to do with the high memory usage ?

i know that if you dont destroy bitmap it only takes from the GPU RAM not the main RAM

thankx
Advertisement
Generally speaking, if you allocate dynamic resources that are intended to be freed, but don't free them, that can cause a memory leak. If you're doing this often (e.g. every frame), then it might result in the kind of memory usage you're seeing.

i know that if you dont destroy bitmap it only takes from the GPU RAM not the main RAM[/quote]
Not necessarily true. It depends on the exact combination of lower level APIs in use. Also some graphics cards "steal" RAM to use as video memory (though that will probably show up in a different way).

Generally speaking, if you allocate dynamic resources that are intended to be freed, but don't free them, that can cause a memory leak. If you're doing this often (e.g. every frame), then it might result in the kind of memory usage you're seeing.


i'm sorry but i didn't quite understand, what do you mean by "if you allocate dynamic resources that are intended to be freed, but don't free them"
how can i free resources?
and i noticed that when starting the game it only uses 20MB and after a while it reaches 300MB and more

how can i solve this memory leak ?

thankx for the post :D

how can i free resources?

It depends on the resource.

What language are you programming in? And is BITMAP an Allegro type, or something else?

how can i free resources? how can i solve this memory leak ?


By pairing a delete[] for every new[], a delete for every new, and a destroy_bitmap for every create_bitmap, load_bitmap, and any other Allegro API function that reminds you "that you are responsible for destroying the bitmap when you are finished with it to avoid memory leaks".

[quote name='newbie123' timestamp='1303833642' post='4803115']
how can i free resources?

It depends on the resource.

What language are you programming in? And is BITMAP an Allegro type, or something else?
[/quote]

i'm using C++ and Allegro Graphic Library.
BITMAP is like a function that passed a pointer to a specific file

Example BITMAP *GameBackGround = load_bitmap ("background.bmp",NULL);


[quote name='newbie123' timestamp='1303833642' post='4803115']
how can i free resources? how can i solve this memory leak ?


By pairing a delete[] for every new[], a delete for every new, and a destroy_bitmap for every create_bitmap, load_bitmap, and any other Allegro API function that reminds you "that you are responsible for destroying the bitmap when you are finished with it to avoid memory leaks".
[/quote]

ok thankx i'll try destroy all the bitmaps.
this is going to take a while LOL :D
OK new problem
every time i use "destroy_bitmap(Buffer);" , i get an error "0xC0000005:Access violation reading location 0xfeeefefe"
btw i have a player class which is like this


// player class in hader file called player
class Player
{
public:
Player();
~Player();
void Init();
void Draw(BITMAP *Buffer);
void Health();
void Attake();
void Hurt();
bool Is_Dead();
void Keyboard(BITMAP *Buffer);
void Mouse(BITMAP *Buffer);
bool PlayerDead;

int x , y , MouseX, MouseY , imgx , imgy , PlayerHealth;
BITMAP *Hero_char;
};

------------------------------------------
//then there is a draw class in a new header file called Draw

class Draw
{

public:
Draw();
~Draw();
void Init();
void GameOver(BITMAP *Buffer);
void NPC_fun(BITMAP *Buffer);
BITMAP *Buffer , *GameOverScreen , *NPC;

};


i have both header file and c++ file.
i tried destroy Buffer in Main.cpp but that gave me the error i told you about above
also should i destroy the Buffer in Main.cpp ? in the game loop ? because thats what i di.
also where do i destroy *GameOverScreen , *NPC , *Hero_char; ? should i destroy them also in main or at the end of their own class ?
To begin with, if the BITMAP (or a similar resource) is a member of a class, you should free the resource in the destructor for that class. (There are ways to automate this process, but simply freeing the resource in the destructor would be a good start.)

To begin with, if the BITMAP (or a similar resource) is a member of a class, you should free the resource in the destructor for that class. (There are ways to automate this process, but simply freeing the resource in the destructor would be a good start.)


how stupid of me, how didnt i think of that LOL
great thankx

This topic is closed to new replies.

Advertisement