[A5]use custom memory allocator in allegro carefully
vkensou

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

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.

vkensou

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

Gideon Weems

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

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

Elias

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".)

Thread #614745. Printed from Allegro.cc