|
Allgro Memory Effeciency |
InfiniteLoop
Member #8,434
March 2007
|
Hello all, I have noticed that I have programs written with Allegro that crash after allotting too much memory. I thought that this was odd because I used all proper delete and destroy statements. So I made a little test program:
After I ran this, I watched my system memory quickly becoming used up and unreleased. Is there any way to better manage memory than the destroy_bitmap() function? Am I doing something wrong? Thank you. EDIT* Also, I tried using delete on my pointer after destroy but it throws an exception, so that isn't it. |
Kris Asick
Member #1,424
July 2001
|
Remove this line: ptrBitmap = new BITMAP; It is the cause of all your problems. --- Kris Asick (Gemini) --- Kris Asick (Gemini) |
InfiniteLoop
Member #8,434
March 2007
|
Well, good call. That certainly fixed the problem of my test program. I am still at odds though since I only use the "new" operator in my actual game once and I am pretty sure I handle it right. I pose another query then, how would you go about deleting/destroying this:
Essentially what is happening is that I have to make two 2D arrays and one 1D array dynamically since I wont know the size. Then I assign bitmaps to them. |
Kris Asick
Member #1,424
July 2001
|
stares blankly at that code You sir, are abusing your pointers! Basically, I have two tips for you that should help you figure this out yourself: 1. Always delete pointers in the opposite order you created them in with new. 2. Allegro's bitmap loading, creation, and destruction routines do all the memory allocation for you. (So you should never call new and delete on BITMAP objects.) Same goes for any other Allegro routines that have a destroy command. (Datafiles, samples and fonts spring to mind, there might be others.) --- Kris Asick (Gemini) --- Kris Asick (Gemini) |
tobing
Member #5,213
November 2004
|
You have to specify which of these should be responsible for holding the allocated memory. I guess that grabFrame allocates the bitmap it returns? Then it would seem that ptrTile should be responsible for memory, because ptrSolution and ptrGrid only have pointers to memory that's already held by ptrTile, is that right? So, if that's what I guess, then only ptrTile should destroy the bitmap and delete the allocated memory, the other arrays should just be freed what they have allocated themselves before. I would put ptrSolution and ptrGrid in a class (they seem to be identical, so that's two instances of the same class, implementing a two-dimensional array). |
Jonatan Hedborg
Member #4,886
July 2004
|
I'm betting you are doing something wonky with the arrays... Try using simple 1D STL::vectors instead.
|
InfiniteLoop
Member #8,434
March 2007
|
All I am doing with the new statements is creating dynamic multidimensional arrays. If I take out the bitmap loading parts, my cleanUp code prevents any memory leaks. The problem arrizes when I throw bitmaps into the mix since you cant call delete on a variable (even though it used new) after it has had destroy_bitmap() called on it. Also, I dont wish to use vectors because I am not interested in figuring out multidimensional vectors. |
Tobias Dammers
Member #2,604
August 2002
|
Quote: All I am doing with the new statements is creating dynamic multidimensional arrays.
Don't. Quote: If I take out the bitmap loading parts, my cleanUp code prevents any memory leaks. I doubt that. Plus, memory leaks are the least of your concerns right now. Quote: The problem arrizes when I throw bitmaps into the mix since you cant call delete on a variable (even though it used new) after it has had destroy_bitmap() called on it. I repeat: NEVER call new or delete on BITMAPs. You can use new to allocate one or more POINTERS to BITMAPS, but not BITMAPs directly. This has nothing to do with whether or not you can call delete on a variable (of course you call it on a variable, what else?). Memory allocation for BITMAPs is done exclusively by allegro's load_bitmap(), create_bitmap() and destroy_bitmap() (and their companions). Quote: Also, I dont wish to use vectors because I am not interested in figuring out multidimensional vectors.
You don't have to. index_into_vector = x + vector_width * y; Of course, you should add some range checking there. Then; from the code you gave, I get the impression that you are using BITMAP* pointers in your game logic, which doesn't sound like a good idea to me (remember, separate logic from drawing). --- |
Kris Asick
Member #1,424
July 2001
|
Just to explain to you what was happening with your initial code, InfiniteLoop: 1. ptrBitmap = new BITMAP;
2. ptrBitmap = create_bitmap(600, 600);
create_bitmap() returns a newly created pointer to a BITMAP structure, but that BITMAP structure also points to the actual contents of the bitmap you created. So, with that in mind, watch this: 1. ptrBitmap = create_bitmap(600, 600);
2. delete ptrBitmap;
Aren't pointers fun? --- Kris Asick (Gemini) --- Kris Asick (Gemini) |
InfiniteLoop
Member #8,434
March 2007
|
Yeah, the original problem was already figured out. Using new in that project was an oversight on my part. It was merely a test to replicate the real problem that I am having. So I decided to suck it up and use vectors. Problem solved. Thanks Jonatan |
|