Access Violation using VC6, SDL-1.1.7

Please bear with me, I’m new to this! From the most basic example SDL code that is on the libsdl.org web site, I made an MSVC project and gave it code that initiliazes SDL_Init, and so on. But when I use SDL_SetVideoMode([…], SDL_SWSURFACE), I get a memory access violation as soon as the program runs.

HOWEVER! If, instead, I also set SDL_FULLSCREEN, my program runs but draws a blank white screen and refuses to die even when I bring up task manager and say ‘end process’. The main loop of the code is just scanf until a newline is read. MSVC is set to kill apps with ctrl+c or ctrl+break but nothing kills the damn thing!

So three questions:

  • Why does non-fullscreen get an access violation? I saw a post on the sdl mailing list archive with the same error, but no one ever responded.
  • How do I kill the damn thing.
  • Where do stdout and stderr go for a graphical app? I mean, I’m used to textmode apps, and those dump all their output to the console!

Here’s the relevent snippet of the code that the example suggests, which dies unless I also specify SDL_FULLSCREEN.
screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE);
if ( screen == NULL ) {
fprintf(stderr, “Unable to set 640x480 video: %s\n”, SDL_GetError());
exit(1);
}

In article <969t6r$ecd$1 at ftp.lokigames.com>, seratonin at io.jtan.com
says…

I get a memory access violation as soon as the program runs.

When you single step it in the debugger where does it actually 

fail?

  • How do I kill the damn thing.
if i get desparate i kick it out with NT's task manager. if you're 

running it in VC’s debugger and it gets stuck, close VC and it will
bring the app down with it.

  • Where do stdout and stderr go for a graphical app?
stdout.txt
stderr.txt

Here’s the relevent snippet of the code that the example suggests,
which dies unless I also specify SDL_FULLSCREEN.
screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE);
if ( screen == NULL ) {
fprintf(stderr, “Unable to set 640x480 video: %s\n”, SDL_GetError());
exit(1);
}

which OS are you using and which video card do you have? also, 

could we see some more complete code to help us reproduce the bug
please?

-dv

Daniel <daniel_v at bigpond.com> wrote:

which OS are you using and which video card do you have? also,
could we see some more complete code to help us reproduce the bug
please?

This is all under win2k with an NVidia GeForce MX, a GeForce2 derivative.

I get a memory access violation as soon as the program runs.
When you single step it in the debugger where does it actually fail?

It says "First-chance exception at 0xC0000005: Access Violation. In the disassembler, if I go to 0xC0000005, all I see is ???. However, there is a yellow arrow on the line

“004013F8 movsx eax,byte ptr [edx]”

Which is inside _SDL_main, or at least, the source annotation shows _SDL_main several assembly lines above this.

In order to get more debugging info than “0xC00005: Access Violation”, I had to link against “Debugging Multithreaded DLL”, though the same crash with the same 0xC0000005 happens with “Multithreaded DLL”.

You’ll please forgive me if I haven’t provided enough info. I haven’t used MSVC’s debugger before, and I can’t seem to get it to step through my program line by line – instead it insists on stepping through each individual Assembly statement…

Here is the complete source of the code. The SetVideoMode call that crashes is active, while the one that just makes an obnoxious blank screen that I can’t break out of (even by using task manager to kill visual studio!) is commented out.

--------code follows
#include “SDL.h”
#include “SDL_endian.h” /* Used for the endian-dependent 24 bpp mode */
#include <stdlib.h>

static int RAND_MID = RAND_MAX / 2;

void DrawPixel(SDL_Surface *screen, Uint8 R, Uint8 G, Uint8 B, int x, int y)
{
Uint32 color = SDL_MapRGB(screen->format, R, G, B);

if ( SDL_MUSTLOCK(screen) ) {
    if ( SDL_LockSurface(screen) < 0 ) {
        return;
    }
}
switch (screen->format->BytesPerPixel) {
    case 1: { /* Assuming 8-bpp */
        Uint8 *bufp;

        bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
        *bufp = color;
    }
    break;

    case 2: { /* Probably 15-bpp or 16-bpp */
        Uint16 *bufp;

        bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
        *bufp = color;
    }
    break;

    case 3: { /* Slow 24-bpp mode, usually not used */
        Uint8 *bufp;

        bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3;
        if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {
            bufp[0] = color;
            bufp[1] = color >> 8;
            bufp[2] = color >> 16;
        } else {
            bufp[2] = color;
            bufp[1] = color >> 8;
            bufp[0] = color >> 16;
        }
    }
    break;

    case 4: { /* Probably 32-bpp */
        Uint32 *bufp;

        bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
        *bufp = color;
    }
    break;
}
if ( SDL_MUSTLOCK(screen) ) {
    SDL_UnlockSurface(screen);
}
SDL_UpdateRect(screen, x, y, 1, 1);

}

short heads() { return abs(rand()) > RAND_MID ? 1 : 0; }

void updateColor(Uint8 *x) {
*x = (*x + (heads() == 1 ? 1 : 2)) % 200;
}

void move(int *x) {
static float inertia = 0.0;
inertia += (heads() == 1 ? 0.1F : - 0.05F);
if (inertia > 0.8F)
inertia = 0.3F;
*x = (inertia > 0.5F ? *x + 2 : *x - 2) % 400;
}

main(int argc, char *argv[])
{
printf(“Initilializing.\n”);
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, “Unable to init SDL: %s\n”, SDL_GetError());
exit(1);
}
atexit(SDL_Quit);

SDL_Surface *screen;

//Crashes:
screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE);
/*Works, but can’t kill it:
screen = SDL_SetVideoMode(640, 480, 16, SDL_FULLSCREEN | SDL_SWSURFACE);
*/
if ( screen == NULL ) {
fprintf(stderr, “Unable to set 640x480 video: %s\n”, SDL_GetError());
exit(1);
}
printf(“Initilialization finished.\n”);
char *in = ‘\0’;
while (scanf("%c", *in) != ‘\n’);
/*int *x = 0, *y = 0;
Uint8 *r = (unsigned char *)0, *g = (unsigned char *)0, *b = (unsigned char *)200;
while (true) {
DrawPixel(screen, *r, *g, *b, *x, *y);
updateColor®;
updateColor(g);
updateColor(b);
move(x);
move(y);

}

*/
return 0;
}

@seth_at_jtan.com wrote:
[snip!]

Well, my own lack of MSVC knowledge combined with not really having used C/C++ in about 3 years is really biting me. Java rotted out my knowledge of scanf/printf apparently, cause once I managed to turn detailed vc debugging info generation on, my error was in
char *in;
scanf("%c", *in) != ‘\n’);

Which I still don’t understand, but of course, the big question is why wasn’t I just doing “SDL_WaitEvent(&event)” ? Because the online docs don’t cover THAT method until a couple pages after the screen init stuff. Time to go re-learn C. Never should have started writing my undergrad Compiler in Java! Back then I knew C and could have kept going, but nooooo, instead I became a Java devotee, left school, did fulltime Java, and now where am I? Can’t even do a simple scanf!

Note to the reader: Wherever you are, it’s 3am where I am, so I apologize for cluttering this great group.