SDL access violation

Started by
2 comments, last by rip-off 10 years, 12 months ago

I have a big program that is causing me access violation when using sdl_rwops. So I decided to create a small program just to test sdl_rwops and it is causing me the same access violation. For what I could see it is being caused because of the fopen_s() part but I could not figure out why. Maybe you guys can find what I am missing. Here is the code:


#include "SDL.h"
#include < stdio.h >

int main(int argc, char *argv[]) 
{
  FILE *file;
  SDL_Surface *screen;
  SDL_Surface *one;
  SDL_Rect rect;
  errno_t err;

/* This is the RWops structure we'll be using */

  SDL_RWops *rw;
  SDL_Init(SDL_INIT_VIDEO);
  atexit(SDL_Quit);
  screen = SDL_SetVideoMode(640, 480, 16, SDL_DOUBLEBUF);

/* Here we get a file pointer to a bitmap */

  if ( (err = fopen_s(&file,"penguin.bmp", "r")) != 0)
  {
     printf("Couldn't load penguin.bmp\n");
     exit(1);
  }

/* This is where we actually create our RWops structure.  Here we're
 instructing SDL to get it from the file pointer we just opened */

rw = SDL_RWFromFP(file, 0);

/* SDL_LoadBMP_RW is the RWops version of the old standby,
 SDL_LoadBMP.  This will get the image from the RWops you passed
 in; in this case the file you've opened */

one = SDL_LoadBMP_RW(rw, 0); // This line calls fopen.c and causes the crash

/* Clean up after ourselves */
SDL_FreeRW(rw);
fclose(file);

/* Haphazard way of getting stuff to the screen */
rect.x = rect.y = 20;
rect.w = one -> w;
rect.y = one -> h;
SDL_BlitSurface(one, NULL, screen, &rect);

SDL_Flip(screen);

SDL_Delay(3000);
}

And here is the portion of fopen.c that causes the crash of the program:


errno_t __cdecl _tfopen_s (
FILE ** pfile,
const _TSCHAR *file,
const _TSCHAR *mode)
{

_VALIDATE_RETURN_ERRCODE((pfile != NULL), EINVAL);

*pfile = _tfsopen(file, mode, _SH_SECURE); // This line causes the access violation

if(*pfile != NULL)
   return 0;

return errno;
}

The line


one = SDL_LoadBMP_RW(rw, 0);
jumps to fopen.c and the line

*pfile = _tfsopen(file, mode, _SH_SECURE);

in that file makes it crash.

I am using Visual Studio 2012 and the picture is in the same folder as the executable. The SDL.dll and even SDL_image.dll are there too. I found a post in google of the same issue and the person said that when he put the whole path (instead of just penguin.bmp") it would not crash. It did not work for me, crashed anyway. I am starting to think that it may be some SDL initialization issue but I did anything I could find on google and nothing worked. I tryed running in Multi-threaded DLL (/Md), in Multi-threaded Debug Dll (/MDd), tryed running in 64 bits, tyred changing the Subsystem to Windows and to console... everything resulted in the same crash.

The error message is:

Unhandled exception at 0x77888C39 (ntdll.dll) in testrwops.exe: 0xC0000005: Access violation writing location 0x00000014.

Which makes me think of a null pointer. So I even tryed to allocate memory for file and rw with new and malloc but it didnt work also. I pretty much used the same code as this site but maybe it is out of date.

Thanks for any help.

Advertisement

Very confusing. SDL's documentation for SDL_RWFromFP says:

This is not available under Win32, since files opened in an application on that platform cannot be used by a dynamically linked library

They should make it so that on Win32, this function either doesn't appear to be available, or at the very least have an alternate code path that returns an error.

The weird thing is that I got SDL_Rwops to work on MinGW compiler but it always crashes on Visual Studio.

My interest in SDL_Rwops is because I was trying to create a packfile to put all my images, sounds, fonts, etc. And for that I was following the code from an old game called I have no Tomatoes and, as I said, it works on MinGW. The problem is that Visual Studio is much faster to write code and much easier to debug. The only little detail is that it does not work tongue.png .

Besides, to use Visual Studio I had to change some things in the code like fopen() to fopen_s(), sprintf() to sprintf_s() and I believe that these comands don't work on MinGW.

Anyway, enough of punching on a rock...

Could you recomend anything to look up to create my own packfile? I think zlib does that but I am not sure if it is not a little too old. And I know that it is impossible to avoid people from hacking your resources but I wanted a packfile that could not be opened in a simple Winrar or Winzip. If you could recomend any library in C++ to do that, it would be great.

Thank you very much for your time and interest.

That is really a separate question, and you'll probably get more responses if you started a new thread. That said, PhysFS is one library I've often heard used to achieve this. If you really don't want your packfile to be trivially opened using simple tools, a quick XOR obfuscation pass will probably suffice.

This topic is closed to new replies.

Advertisement