Unhandled exception error thrown while running the program
以下是我的程序的源代码,没有编译错误但是当我运行它时,我得到异常运行时错误,如附图中所示。
大段引用
all5.1.exe中0x00000000的第一次机会异常:0xC0000005:访问冲突执行位置0x00000000。 all5.1.exe中0x778F1A91处的未处理异常:0xC0000005:访问冲突执行位置0x00000000。
大段引用
#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;
}
发现错误,在初始化字体加载项后,您需要使用al_init_ttf_addon()
初始化ttf加载项。会发生什么是单独的字体加载项不知道如何读取不同的格式。因此,当您尝试加载* .ttf字体时,它会无声地失败。
你的代码很乱。格式化很糟糕,并且存在相当多的内存泄漏。
每次调用一个void函数时,都会加载一个位图,但绝不会使用al_destroy_bitmap
将其破坏。这意味着它每次泄漏整个ALLEGRO_BITMAP
。最终你将耗尽内存,它将返回NULL
。
关于在al_init_font_addon
之后调用al_init
,rlam是正确的。如果你使用的是ttf字体,你还需要调用al_init_ttf_addon
。
还需要注意的是在构造函数中加载allegro资源的全局对象。这些将在调用al_init
之前加载,因此它们将失败。