Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [A5]use custom memory allocator in allegro carefully

This thread is locked; no one can reply to it. rss feed Print
[A5]use custom memory allocator in allegro carefully
vkensou
Member #15,546
March 2014

if you use custom memory allocator to replace allegro's default allocator,plz be careful.

before use the method al_set_memory_interface(), allegro use default allocator to malloc/free.So you need al_set_memory_interface before allegro install.

Even if you do so, or have an opportunity to cause a memory leak. like this:
class String is a wrapper of allegro's string methods. Define a local variables in a cpp file.

#SelectExpand
1#include <xxx.h> 2 3String str("test");

this string's memory is malloc before the main(), use the default malloc. so you must use default free() to free it. that mean you need use al_set_memory_interface() again, after allegro uninstall, to replace your custom allocator with default.

so ,don't use al_init(), that method will automatic call al_uninstall_system after main().
you need do like this:

#SelectExpand
1int main() 2{ 3 al_set_memory_interface(&yourallocator); 4 al_install_system(ALLEGRO_VERSION_INT, 0); 5 6 ...your game codes 7 8 al_uninstall_system(); 9 al_set_memory_interface(0); 10}

hope help you.;D

Thomas Fjellstrom
Member #476
June 2000
avatar

Those are some good points :)

We should probably include them in the documentation, assuming they aren't already.

That point about the statically allocated object is a very good one, as it can affect more than just memory management. If you try to call allegro API functions in that object's constructor, things can go horribly wrong.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

vkensou
Member #15,546
March 2014

thank you for your reply. hope it could help someone else

Gideon Weems
Member #3,925
October 2003

Good points. Thank you for bringing them up.

What's the easiest way to make such a doc contribution from a web browser?

Thomas Fjellstrom
Member #476
June 2000
avatar

Send a patch to the mailing list (via webmail) or attach to a post on the Allegro Developement forum subsection.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Elias
Member #358
May 2000

You can also edit the documentation online at https://github.com/liballeg/allegro5/blob/5.1/docs/src/refman/memory.txt :) (And then submit with "Pull Request".)

--
"Either help out or stop whining" - Evert

Go to: