Allegro.cc - Online Community

Allegro.cc Forums » Allegro Development » Destroying timer causes crash

This thread is locked; no one can reply to it. rss feed Print
Destroying timer causes crash
Angeljruiz
Member #14,553
September 2012

Every once in awhile after closing this program it crashes when it gets to the destroy timer line - Ive had this happen in several other programs as well, its getting pretty annoying.

#SelectExpand
1// 2// main.cpp 3// Timer 4// 5// Created by Angel on 10/10/12. 6// Copyright (c) 2012 Duos. All rights reserved. 7// 8 9#include <iostream> 10#include "WindowManager.h" //includes primitives 11#include <allegro5/allegro_font.h> 12#include <allegro5/allegro_ttf.h> 13#include <ctime> 14#include "BuildingManager.h" 15 16 17using namespace std; 18 19//////Globals////// 20const int WOOD = 1; 21const int FOOD = 2; 22 23bool Debug = false; 24/////////////////// 25 26 27void UpdateConsole(BuildingManager); 28 29void DrawWindows(WindowManager); 30 31int main(int argc , char** argv) 32{ 33 al_init(); 34 al_install_keyboard(); 35 al_install_mouse(); 36 al_init_primitives_addon(); 37 al_init_font_addon(); 38 al_init_ttf_addon(); 39 40 ALLEGRO_DISPLAY *display = NULL; 41 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 42 ALLEGRO_TIMER *timer = NULL; 43 ALLEGRO_FONT *font = NULL; 44 45 display = al_create_display(640,480); 46 timer = al_create_timer(1.0 / 120); 47 if (!timer) 48 return -1; 49 event_queue = al_create_event_queue(); 50 font = al_load_ttf_font("font.ttf",15,0 ); 51 if (!font) { 52 cout << "Puto font\n"; 53 } 54 55 BuildingManager Buildingmanager; 56 57 Buildingmanager.AddBuilding(WOOD); 58 Buildingmanager.AddBuilding(WOOD); 59 Buildingmanager.AddBuilding(WOOD); 60 Buildingmanager.AddBuilding(WOOD); 61 Buildingmanager.AddBuilding(WOOD); 62 Buildingmanager.LevelBuilding(0); 63 Buildingmanager.LevelBuilding(0); 64 Buildingmanager.LevelBuilding(0); 65 Buildingmanager.AddBuilding(FOOD); 66 Buildingmanager.AddBuilding(FOOD); 67 Buildingmanager.AddBuilding(FOOD); 68 Buildingmanager.AddBuilding(FOOD); 69 70 WindowManager Windowmanager; 71 72 Windowmanager.AddWindow(150, 50, 250, 250); 73 Windowmanager.AddWindow(50, 70, 150, 150); 74 75 76 77 78 bool Exit = false; 79 bool Redraw = true; 80 bool Moving = false; 81 82 int Ticker = 0; 83 int Ticks = 0; 84 int CurWindow = -1; 85 86 UpdateConsole(Buildingmanager); 87 88 al_register_event_source(event_queue, al_get_display_event_source(display)); 89 al_register_event_source(event_queue, al_get_keyboard_event_source()); 90 al_register_event_source(event_queue, al_get_mouse_event_source()); 91 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 92 93 al_start_timer(timer); 94 95 96 while (!Exit) 97 { 98 ALLEGRO_EVENT ev; 99 ALLEGRO_MOUSE_STATE state; 100 101 al_wait_for_event(event_queue, &ev); 102 al_get_mouse_state(&state); 103 104 if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) 105 { 106 CurWindow = Windowmanager.GetWindow(state.x, state.y); 107 Moving = true; 108 } 109 if (Moving) 110 { 111 Windowmanager.MoveWindow(state.x, state.y, CurWindow); 112 Redraw = true; 113 114 } 115 if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) 116 { 117 Moving = false; 118 } 119 120 121 if (ev.type == ALLEGRO_EVENT_TIMER) 122 { 123 Ticker++; 124 if (Ticker == 600) { 125 Buildingmanager.Tick(); 126 Ticker = 0; 127 ++Ticks; 128 Redraw = true; 129 } 130 } 131 132 if (ev.type == ALLEGRO_EVENT_KEY_UP) 133 { 134 switch (ev.keyboard.keycode) 135 { 136 case ALLEGRO_KEY_ESCAPE: 137 Exit = true; 138 break; 139 140 case ALLEGRO_KEY_R: 141 break; 142 } 143 144 } 145 146 if (al_event_queue_is_empty(event_queue) && Redraw) 147 { 148 Redraw = false; 149 al_clear_to_color(al_map_rgb(248, 248, 255)); 150 al_draw_textf(font, al_map_rgb(0, 0, 0), 5, 5, NULL, "Food: %i \t Per Tick: %i", (int)Buildingmanager.GetFood(), Buildingmanager.GetFoodPT()); 151 al_draw_textf(font, al_map_rgb(0, 0, 0), 5, 35, NULL, "Wood: %i \t Per Tick: %i", (int)Buildingmanager.GetWood(), Buildingmanager.GetWoodPT()); 152 al_draw_textf(font, al_map_rgb(0, 0, 0), 640/2, 5, ALLEGRO_ALIGN_CENTRE, "Ticks: %i", Ticks); 153 154 Windowmanager.DrawWindows(); 155 156 157 al_flip_display(); 158 } 159 160 } 161 162 al_destroy_display(display); 163 if(timer) al_destroy_timer(timer); 164 al_destroy_event_queue(event_queue); 165 al_destroy_font(font); 166 167 return 0; 168} 169 170void UpdateConsole(BuildingManager Buildingmanager) 171{ 172 if (!Debug) 173 return; 174 cout << string( 100, '\n' ); 175 cout << "Food: " << Buildingmanager.GetFood() << "\t\tFood/Tick " << Buildingmanager.GetFoodPT() << endl << endl; 176 cout << "Wood: " << Buildingmanager.GetWood() << "\t\tWood/Tick " << Buildingmanager.GetWoodPT() << endl << endl; 177 178}

Arthur Kalliokoski
Second in Command
February 2005
avatar

I think swapping line 164 ("al_destroy_event_queue(event_queue)") and line 163 ("if(timer) al_destroy_timer(timer)") would fix it. In other words, the queue depends on the other stuff, and it's trying to access a destroyed timer, so always destroy the queue first.

They all watch too much MSNBC... they get ideas.

Angeljruiz
Member #14,553
September 2012

I think someone told me to do that before in a previous program but the problem still persisted. Ill let you know if it keeps crashing after switching them

EDIT:Nope its still crashing
http://i.imgur.com/GM69R.png

Elias
Member #358
May 2000

Which version?

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

Angeljruiz
Member #14,553
September 2012

Umm im not sure - I have the 5.0.7 libraries of allegro but i just link the normal -lallegro -lallegro_main etc..

EDIT: Yeahh im just using A5

Peter Wang
Member #23
April 2000

Windows, I presume? Make a small test case and I'll look at it.

Angeljruiz
Member #14,553
September 2012

No, im on Mac Lion

What do you mean make a small test ?

Heres links to my two other threads where destroying the timer causes a crash

http://www.allegro.cc/forums/thread/611140
http://www.allegro.cc/forums/thread/611115

Peter Wang
Member #23
April 2000

I don't have a Mac so unfortunately I can't test there. I did build and run your 611140 program on Linux but couldn't get it to crash. How often does it happen?

Maybe someone else with a Mac could try it?

Go to: