Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Access violation reading location???

Credits go to Niunio for helping out!
This thread is locked; no one can reply to it. rss feed Print
Access violation reading location???
Ze_Dude
Member #13,495
September 2011

My program keeps crashing when I try to run it >:(

When I debugged it, it gave an error, Unhandled exception at 0x1002ff69 in shooter.exe: 0xC0000005: Access violation reading location 0x00000014.

The problem seems to be with the draw_bitmap function.

Call stack is as follows:

allegro-5.0.4-monolith-md-debug.dll!1002ff69()
[Frames below may be incorrect and/or missing, no symbols loaded for allegro-5.0.4-monolith-md-debug.dll]
allegro-5.0.4-monolith-md-debug.dll!10030004()
> shooter.exe!movement::drawimage(ALLEGRO_BITMAP * drawbmp) Line 74 + 0x28 bytes C++
shooter.exe!movement::moveleft() Line 106 C++
shooter.exe!main(int argc, char * * argv) Line 90 C++
shooter.exe!__tmainCRTStartup() Line 555 + 0x19 bytes C
shooter.exe!mainCRTStartup() Line 371 C
kernel32.dll!7c817077()

code is:

#SelectExpand
1 2//movement.cpp 3#include "movement.h" 4 5#include <stdio.h> 6 7 8 9void movement::imageloadright() {// I've entered the filenames to load in this function, but I removed the filenames themselves here to make the whole thing smaller. 10 11 12checkbitmap(); // checks if bitmaps are loaded 13} 14 15void movement::imageloadleft() { // consult imageloadright 16 17 18 checkbitmap(); 19} 20 void movement::setup() { 21 22 23 24 imageloadright(); 25 imageloadleft(); 26 27 28 al_clear_to_color(al_map_rgb(255, 255, 255)); 29 al_flip_display(); 30 31 32 x = 150; 33 y = 320; 34 35 36} 37 38 int movement::getX() { 39 return x; 40} 41 42 int movement::getY() { 43 return y; 44} 45 46 void movement::drawimage(ALLEGRO_BITMAP *drawbmp) { 47 al_draw_bitmap(drawbmp, x, y, 0); 48 al_flip_display(); 49 } 50 51 void movement::eraseimage() { 52 al_clear_to_color(al_map_rgb(255, 255, 255)); 53} 54 55 void movement::moveright() { 56 i = 0; 57 58 while (x <= 600 ) { 59 60 eraseimage(); 61 drawimage(bitmap[i]); 62 x = x + 2; 63 i++; 64 65 66 67 } 68 } 69 70 71 void movement::moveleft() { 72 i = 16; 73 74 while (x >= 5 ) { 75 76 77 eraseimage(); 78 drawimage(bitmap[i]); 79 x = x - 2; 80 i++; 81 82 83 84 85 } 86} 87 88 89void movement::destroybitmap() { 90for (loop = 0;loop<= 31;loop++) { 91 al_destroy_bitmap(bitmap[loop]); 92 } 93} 94 95void movement::checkbitmap() {//if one bitmap is loaded, chances are all of them are loaded if they are in the same folder and have similiar dimensions...right? 96if (bitmap[0] = NULL) { 97 fprintf(stderr, "failed to load bitmaps!\n"); 98 exit(-1); 99 100} 101}

And the header file:

#SelectExpand
1//movement.h 2#ifndef MOVEMENT_H 3#define MOVEMENT_H 4 5 6#include <allegro5/allegro.h> 7#include <allegro5/allegro_image.h> 8 9 10 11 12class movement { 13public: 14 15 void setup(); 16 void moveright(); 17 void moveleft(); 18 int getX(); 19 int getY(); 20 void imageloadleft(); 21 void imageloadright(); 22 void eraseimage(); 23 void drawimage(ALLEGRO_BITMAP *drawbmp); 24 void destroybitmap(); 25 void checkbitmap(); 26 27 28 29private: 30 int i; 31 ALLEGRO_BITMAP *bitmap[31] ; 32 33 34 35 36 int loop; 37 38 int x; 39 int y; 40 41 42 43}; 44 45 46#endif

I think the main file has nothing to do with this so I left it out. :P

Niunio
Member #1,975
March 2002
avatar

while (x >= 5  ) {
  eraseimage();
  drawimage(bitmap[i]);
  x = x - 2;
  i++;
}

The "bitmap[i]" isn't a correct pointer (i.e. it is NULL or not valid pointer). I suspect that there are less bitmaps in "bitmap" that you spect.

Check the value for "i" and for "bitmap[i]".

-----------------
Current projects: Allegro.pas | MinGRo

Ze_Dude
Member #13,495
September 2011

Thanks. It wasn't just about that, the increment operator could cause the value of i to go over 33 which caused it to crash because bitmap[34] didn't exist.

Neil Walker
Member #210
April 2000
avatar

Also, this:

58  while (x <= 600  ) {
  59
  60      eraseimage();
  61      drawimage(bitmap[i]);
  62      x = x + 2;
  63      i++;
  64            
  65
  66
  67    }
  68  }

You only have 31 bitmaps but are tryign to draw 600.

Also, you are writing C++, move away from arrays for your storage and use something like std::vector.

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

Dizzy Egg
Member #10,824
March 2009
avatar

Even though you've got an 'A-HA' avatar, I have to point out that:

but are tryign to draw 600.

is a shame because:

A) tryign

and

B) he adds 2 to x each iteration.

And that, is all.

EDIT:

I bet Neil says something about still being out of range....Neil?

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

Go to: