Unhandled exception error thrown while running the program
Following is the source code of my program, there's no compilation error but when i run it i get the exception Runtime error as in the attached image.
Blockquote
First-chance exception at 0x00000000 in all5.1.exe: 0xC0000005: Access violation executing location 0x00000000. Unhandled exception at 0x778F1A91 in all5.1.exe: 0xC0000005: Access violation executing location 0x00000000.
Blockquote
#include <conio.h>
#include <allegro5\allegro.h>
#include <allegro5\allegro_native_dialog.h>
#include <allegro5\allegro_font.h>
#include <allegro5\allegro_ttf.h>
#include <allegro5\allegro_image.h>
#include <allegro5\allegro_primitives.h>
const int SCREEN_H = 690;
const int SCREEN_W = 1350;
class background
{
private: int advt_x = 1000, advt_y = SCREEN_H - 100, nw_m_x = 1350; //data cannot be accessed from outside the class
public: ALLEGRO_FONT *size_20 = al_load_font("digital-7.ttf", 20, 2); //loading font file for advt() and nw_title()
public: ALLEGRO_FONT *size_15 = al_load_font("digital-7.ttf", 15, 1); //loading font file for nw_marquee()
public: void bground()
{
ALLEGRO_BITMAP *bgr = al_load_bitmap("bground.jpg"); //loading the background vertex image
if (bgr == NULL) //checking whether the image loaded successfully or not
{
al_show_native_message_box(NULL, "Error", NULL, "Fatal error: Backgroung Missing from disk!!!", NULL, NULL); //if image didn't loaded then print the error message
}
al_draw_bitmap(bgr, 0, 0, NULL); //printing the background image
}
public: void studio()
{
ALLEGRO_BITMAP *anch = al_load_bitmap("anchor.jpg");
if (anch == NULL) //checking whether the image loaded successfully or not
{
al_show_native_message_box(NULL, "Error", NULL, "Fatal error: Anchor Missing from disk!!!", NULL, NULL); //if image didn't loaded then print the error message
}
al_draw_bitmap(anch, SCREEN_W-300, SCREEN_H-500, NULL); //printing the anchor image
}
public: void news_info()
{
ALLEGRO_BITMAP *nwinfo = al_load_bitmap("news_image.jpg");
if (nwinfo == NULL) //checking whether the image loaded successfully or not
{
al_show_native_message_box(NULL, "Error", NULL, "Fatal error: News Media Missing from disk!!!", NULL, NULL); //if image didn't loaded then print the error message
}
al_draw_bitmap(nwinfo, 0, 200, NULL); //printing the News Media
}
public: void ch_logo()
{
ALLEGRO_BITMAP *chlogo = al_load_bitmap("logo_header.jpg");
if (chlogo == NULL) //checking whether the image loaded successfully or not
{
al_show_native_message_box(NULL, "Error", NULL, "Fatal error: News Media Missing from disk!!!", NULL, NULL); //if image didn't loaded then print the error message
}
al_draw_bitmap(chlogo, SCREEN_W-110, 0, NULL); //printing the News Media
}
public: void nw_title()
{
al_draw_rectangle(0, 0, SCREEN_W-110, 72, al_map_rgb(0, 255, 0), 1.0);
al_draw_text(size_20, al_map_rgb(128, 50, 30), 0, 0, 0, "Violence in a Restaurant over the payment of Bill amounting INR 260."); //printing text
}
public: void nw_marquee()
{
int nw_m_text_len = al_get_text_width(size_15, "All news headlines will be displayed in marquee here.");
if (nw_m_x == (0 - nw_m_text_len))
{
nw_m_x = 1000;
}
al_draw_filled_rectangle(0, SCREEN_H-190, SCREEN_W, SCREEN_H-100, al_map_rgb(0, 255, 0));
al_draw_text(size_15, al_map_rgb(128, 50, 30), nw_m_x, SCREEN_H - 140, 0, "All news headlines will be displayed in marquee here.");
nw_m_x--;
}
public: void advt()
{
al_draw_filled_rectangle(0, SCREEN_H-99, SCREEN_W, SCREEN_H, al_map_rgb(90, 110, 0));
al_draw_text(size_20, al_map_rgb(128, 50, 30), advt_x, SCREEN_H-50, 0, "Ads will be shown here.");
advt_x--;
}
}bg;
int main()
{
ALLEGRO_DISPLAY *display = NULL;
if (!al_init())
{
al_show_native_message_box(NULL, "Init error", NULL, "Allegro failed to initialise!!! Program is exiting.", NULL, NULL);
return -1;
}
display = al_create_display(1350, 690);
al_set_window_position(display, 0, 0);
al_set_window_title(display, "New Window");
al_init_font_addon();
al_init_image_addon();
al_init_primitives_addon();
bg.bground();
bg.studio();
bg.ch_logo();
bg.news_info();
while (1 == 1)
{
bg.nw_title();
bg.advt();
bg.nw_marquee();
al_flip_display(); //print from backBuffer to screen and makes things visible
al_rest(3.0);
al_destroy_display(display);
}
_getch();
return 0;
}
Found the error, right after initializing the font add-on, you need to initialize the ttf add-on with al_init_ttf_addon()
. What happens is that the font add-on alone does not know how to read the different formats. So it was failing silently when you tried to load the *.ttf fonts.
Your code is a mess. The formatting is bad, and there are quite a few rampant memory leaks.
Every time you call one of your void functions you load a bitmap but never destroy it with al_destroy_bitmap
. This means it leaks an entire ALLEGRO_BITMAP
every time. Eventually you will run out of memory and it will return NULL
.
rlam is correct about calling al_init_font_addon
after al_init
. If you're using ttf fonts, you also need to call al_init_ttf_addon
.
Something else to be wary of is global objects that load allegro resources in a constructor. These will be loaded before al_init
is called, and so they will fail.
User contributions licensed under CC BY-SA 3.0