Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » OpenLayer, ol::Bitmap::destroy()

This thread is locked; no one can reply to it. rss feed Print
OpenLayer, ol::Bitmap::destroy()
mengemeto
Member #8,672
May 2007

I have been searching the forums a couple of days now and cannot find similar topic, excuse me if there is any which I missed.

I am having really hard time releasing the memory allocated by the Bitmap objects in my code. Tried to test new/delete functions with the following program:

1#include <allegro.h>
2#include <OpenLayer.hpp>
3 
4int main()
5{
6 ol::Setup::SetupProgram(true,true,true);
7 ol::Setup::SetupScreen( 800, 600, !FULLSCREEN, 32);
8 
9 ol::Bitmap* b = NULL;
10 for(;;)
11 {
12 b = new ol::Bitmap("Main.tga");
13 b->Destroy();
14 delete b;
15 }
16 return 0;
17}
18END_OF_MAIN()

I do not know how to check memory usage of the program after executing it (Windows Task Manager seems pointless), but it is more than obvious the program is leaking memory. Using big image file (something like 4096x4096) and leaving the program running for several minutes leads to never ending hard disk read/write (swap file i guess) and few more minutes leads to system windows message "The system is low on virtual memory".

Maybe I am doing something terribly wrong, but I cant figure what.

Any comments will be greatly appreciated. Thanks in advance!

P.S. Excuse my poor English, it is not my native :-[

piccolo
Member #3,163
January 2003
avatar

Task Manager is not pointless. A good way to use it is to execute the program and open task manager. Look for the program process if the mem useage keep on going up when the program is idel that means you have a mem leak. all you have to do is comment out the parts in your code block by block so you sane find the leak. Most of the time it is the founchion the program in preforming that will by the problem.

wow
-------------------------------
i am who you are not am i

Dustin Dettmer
Member #3,935
October 2003
avatar

Looks like a clear case of memory leakage. :-/

BAF
Member #2,981
December 2002
avatar

Why do you want to load and delete in a loop like that?

ixilom
Member #7,167
April 2006
avatar

Quote:

Why do you want to load and delete in a loop like that?

I'm quite sure that was a narrowed down test too see why his game/app keeps growing in memory usage. Sort of a proof of concept :P

___________________________________________
Democracy in Sweden? Not since 2008-Jun-18.
<someone> The lesbians next door bought me a rolex for my birthday.
<someone> I think they misunderstood when I said I wanna watch...

Ceagon Xylas
Member #5,495
February 2005
avatar

Declare that you're using the OpenLayer namespace so you don't have to scope ol:: almost every line. using namespace ol;

I can reproduce the error, even with a much smaller image.

Infact, it has nothing to do with the pointer.

while(true) {
  Bitmap b;
  b.Load("test.png");
  b.Destroy();
}

Has the same effect.

mengemeto
Member #8,672
May 2007

To answer the question about my stupid new/delete loop, I have done it after few hours of pure frustration being unable to find where the heck my program is leeching memory.
Anyway, yesterday I downloaded and compiled openlayer-svn-snapshot-20070526.tar.gz and it seems the Bitmap object is no longer leeching memory.

Probably I need to ask in a new thread, but I will take a shot here.

My game has background image (different sizes depending on zone the character is in, but generally something like 3000x3000, 4000x2000 etc.), which is split into several 512x512 images. When the character is moving the program decide which tiles needs to be "active" at the moment and convert them into ol::Bitmap object so they can be visualized. The ol::Bitmap objects are destroyed when they are no longer needed.
The problem is converting memory bitmaps (allegro BITMAP*) to OpenLayer bitmaps (ol::Bitmap) seems to take way too much time. For a tile 512x512x32 it takes about 40ms which is not acceptable considering I am doing it in the main game loop. Do I need to revamp my login so I can do the conversion in a separate thread, or there is a workaround which can speed up the conversion?

Thanks!

Fladimir da Gorf
Member #1,565
October 2001
avatar

I'd keep everything as Bitmaps. Using separate threads might help as well, but generally uploading images to video card is slow.

About the memory leak, either the latest changes to how the memory version of the image is handled has prevented Bitmap from releasing the memory image or the OpenGL implementation leaks memory (doesn't sound likely). I'll take a look when I get a chance...

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

Richard Phipps
Member #1,632
November 2001
avatar

The memory leak may be due to a malloc not being freed in an old version of the image ->memory card function. I remember pointing it out to you Flad and you fixed it..

mengemeto
Member #8,672
May 2007

Quote:

I'd keep everything as Bitmaps. Using separate threads might help as well, but generally uploading images to video card is slow.

Keeping everything as ol:Bitmap objects works great indeed, unfortunately the memory needed for all graphics is about 300MB. There are too many NPCs visible at the same time in certain zones and all of them have 50-60 frames Idle loop and a small collection of Ambient animations (3-4 ambients 50-100 frames each). The animations are being kept in the memory compressed (frame by frame) and decompress/visualize on demand.

I am trying to run the images decompressing/uploading to video memory in a separate thread, but for some strange reason it fails to create a new ol::Bitmap object if the call is not in the main thread. The reason according to openlayer.log is:

Loading bitmap from an Allegro OL_MEMORY_IMG (Alpha channel: Yes, Convert Magic Pink: No)
No OpenGL extensions were found.

I cant find anything in OpenLayer's docs about multi-thread usage. Can it be used at all or I am working in a VERY wrong direction. :-/

Dustin Dettmer
Member #3,935
October 2003
avatar

Quote:

Using separate threads might help as well ...

I couldn't imagine seperate threads making this faster.

mengemeto
Member #8,672
May 2007

Quote:

I couldn't imagine seperate threads making this faster.

It wont make it faster of course. It would hide the images preparation delay from player though. For example having a NPC animated in an idle loop, and we want to start an ambient animation for it. Decompressing the new animation images takes, lets say 10ms (it is quite simple compression), uploading them into ol::Bitmap objects (multiple objects to minimize memory losses due to power of 2 limitation) takes, lets say 200ms (even more for some animations :( ). Thats 210ms when main game loop is stalled. It is perfectly acceptable starting the ambient animation to wait for a different thread to prepare the images before actually starting it (keeping the old animation for this NPC playing meanwhile).

Thats pretty much the idea I am working on, but being unable to load a ol::Bitmap object in a different thread tends to be a problem >:(

Dustin Dettmer
Member #3,935
October 2003
avatar

OpenGL doesn't support multiple threads :-X. You're pretty much screwed (unless ofcourse you use D3D, but saying that is grounds to get shot around here :P)

Richard Phipps
Member #1,632
November 2001
avatar

Go to: