This post is a portion of the Appendix in my on going series about 2D Game Development using the Allegro 5 library. These posts are in course order. You can see all of the posts in this course by clicking the “2D Game Development” button at the top of this site.

Destroying Objects

As a user pointed out, a very significant step has been missing from my tutorials so far. That step is destroying the objects we have created in our programs. This vital step is necessary because C++ does not come with any form of native, automatic garbage collection. What that means is that objects that are created into memory are never erased. The result is that the memory is never freed up and can be quite detrimental to performance. Such an occurrence is commonly referred to as a memory leak. My apologies for forgetting to include these statements in my tutorials and I will have them from now on. As it would take too much time to re-record the videos, I will just fix the source code to have the correct statements on each tutorial page and link to this appendix page. So far, there are five types of objects that we have created that we will also need destroy statements for: displays (which I have been destroying), fonts, timers, event queues, and bitmaps. A good rule of thumb is that if you create a variable that’s name begins with “ALLEGRO_”, you will need to use a destroy function.

The following is a code sample showing the declaration, creation, and destruction of the five above mentioned variable types.

ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *queue = NULL;
ALLEGRO_TIMER *timer = NULL;
ALLEGRO_BITMAP *image = NULL;
ALLEGRO_FONT *font = NULL;

display = al_create_display(WIDTH, HEIGHT);
queue = al_create_event_queue();
timer = al_create_timer(1.0 / FPS);
image = al_create_bitmap(WIDTH, HEIGHT);
font = al_load_font("arial.ttf", 18, 0);

al_destroy_display(display);
al_destroy_event_queue(queue);
al_destroy_timer(timer);
al_destroy_bitmap(image);
al_destroy_font(font);