0

Visual Studio Express 2012's debugger says:

Unhandled exception at 0x00B8147F in PFPJ.exe: 0xC0000005: Access violation 
reading location 0x00000008.

Here is the part of the code that the debugger says is the problem:

(Just so you know, width and height are declared globally in main.cpp. All of the functions utilizing these variables are within main.cpp.

SDL_Surface* bmmap = NULL;
bmmap = SDL_LoadBMP( file );
width = bmmap -> w;
height = bmmap -> h;

Specifically the width = bmmap -> w returns the error.

Problem signature:
 Problem Event Name:        APPCRASH
 Application Name:          PFPJ.exe
 Application Version:       0.0.0.0
 Application Timestamp:     51a57a2e
 Fault Module Name:         PFPJ.exe
 Fault Module Version:      0.0.0.0
 Fault Module Timestamp:    51a57a2e
 Exception Code:            c0000005
 Exception Offset:          00001838
 OS Version:                6.1.7601.2.1.0.256.1
 Locale ID:                 1033
 Additional Information 1:  0a9e
 Additional Information 2:  0a9e372d3b4ad19135b953a78882e789
 Additional Information 3:  0a9e
 Additional Information 4:  0a9e372d3b4ad19135b953a78882e789

Every other example I've found for getting width or height out of an SDL_Surface* uses this notation. I don't fully understand what is going on.

What I am certain of is that my own in-built file stream has an output file that shows me where it failed. It fails in a different function when it references width/height in a double for loop. SDL_Surface structs are defined as:

typedef struct SDL_Surface {
 Uint32 flags;                           /* Read-only */
 SDL_PixelFormat *format;                /* Read-only */
 int w, h;                               /* Read-only */
 Uint16 pitch;                           /* Read-only */
 void *pixels;                           /* Read-write */
 SDL_Rect clip_rect;                     /* Read-only */
 int refcount;                           /* Read-mostly */
 } SDL_Surface;

This blows me away. I don't understand where this error comes from, and why this is a problem. It works within the context of the function where I used the -> notation. I've found a number of programs that use surface -> w to get the width, so what's the problem?

(I'm relatively new to C/C++, so any insight is helpful)

4

1 回答 1

0

在我看来这条线失败了

bmmap = SDL_LoadBMP( file );

当它失败时,它返回NULL(即零)。然后,当您取消引用此零地址以获取宽度(在结构中的某个偏移量处 - 我猜它是 8 个字节)时,您会在内存地址 0x00000008 处遇到访问冲突。

添加检查以确保SDL_LoadBMP已成功。

于 2013-05-29T04:39:40.437 回答