Thread: Debug Assertion failure problem

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    8

    Debug Assertion failure problem

    I am trying to open a 24-bit bitmap file to use as a simple texture in an OpenGL scene, but I cant get past this debug assertion failure that comes up on fseek or fread.

    Code:
    void loadBMP(char *filename, byte* BitmapP, int* owidth, int* oheight, int max) {
    	byte curnt[3];
    	int width, height;
    
    	FILE* file1;
    	file1 = fopen(filename, "rb");
    	fseek(file1, 18, SEEK_SET);
    	fread(&width, sizeof(int), (size_t)1, file1);
    	fseek(file1, 22, SEEK_SET);
    	fread(&height, sizeof(int), (size_t)1, file1);
    	*owidth = width;
    	*oheight = height;
    	fseek(file1, 54, SEEK_SET);
    	for(int y=1; y < height + 1; y++){
    		for(int x=1; x < width + 1; x++){
    			fread((BitmapP + x*(max*3) + y*3), sizeof(byte), (size_t)3, file1);
    		}
    		fseek(file1, width % 4, SEEK_CUR);
    	}
    	fclose(file1);
    }
    I took most of this code from an online tutorial, but I understand what it's doing and can't figure out why its throwing this error. But i think I have a general idea of where its going wrong. There seems to be a bad pointer remaining after fopen thats causing the first fseek to fail.

    I followed the debugger into
    Code:
    file1 = fopen(filename, "rb");
    vs shows the variable file with the proper data, which was "metalt.bmp"
    then I followed it into fopen.c, line 124,
    Code:
    return( _tfsopen(file, mode, _SH_DENYNO) );
    which jumps back up to line 50, the _tfsopen function. After these declarations
    Code:
    REG1 FILE *stream=NULL;
    REG2 FILE *retval=NULL;
    retval has all its data, but stream does not. It's got the scary
    stream 0x00000000 {_ptr=??? _cnt=??? _base=??? ...} _iobuf *
    and all of its data "cannot be evaluated."

    Long story short, it successfully completes fopen and all its internal functions even though its now saying "bad pointer" all over the place, then runs
    Code:
    fseek(file1, 18, SEEK_SET);
    and then it breaks in fseek.c on line 100 which is
    Code:
    _VALIDATE_RETURN( (stream != NULL), EINVAL, -1);
    seemingly because stream, which is a FILE* does not have my bitmap file likes it's supposed to. Any suggestions?

    I'm running VS2005 with a win32 console app if that makes a difference.

    thanks in advance

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It means you're not checking for success.
    Code:
    file1 = fopen(filename, "rb");
    if ( file1 != NULL ) {
      // do the stuff which reads the file.
    } else {
      perror("Can't open file");
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    8
    Well yes I know that. file1 is never NULL, so this just allows it to run and not open the file. I guess my question would be better if I asked, what can I do to correct the bad pointer problem?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The assertion is telling you that it is NULL.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    8
    So why would fopen return NULL to file1?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If a file with that name cannot be found? ETA: That is to say, in the directory from which you are running the file. Is your .bmp in a different directory?

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    8
    its a resource in my project, in my project directory, and I've also tried the full path. Same results

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    8
    Oh wow I just double checked that and I found I accidently dragged the bmp into the Debug folder in my project. Well I guess that explains that. The texture isnt showing up however, haha. But thats more of a hair-pulling opengl problem which will probably be easier to figure out than debug errors.
    Thanks guys

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > So why would fopen return NULL to file1?
    Well if you'd had proper error checking to begin with (and not a presumption of success), and a call to perror(), it would have told you right off the bat what was wrong.

    Tedious and laborious it may be, but when your programs get out into the real world, all sorts of things can go wrong. Say some miscreant decided to feed your program a handful of 10MB .bmp files for textures, what would happen then?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. debug release conf problem
    By fighter92 in forum Game Programming
    Replies: 6
    Last Post: 03-20-2009, 04:39 PM
  2. File I/O Assertion Failure in VS2008
    By clegs in forum C Programming
    Replies: 5
    Last Post: 12-25-2008, 04:47 AM
  3. Debug Assertion Failed!
    By IndioDoido in forum C Programming
    Replies: 31
    Last Post: 03-25-2008, 11:07 AM
  4. Debug Assertion Error
    By Artist_of_dream in forum C++ Programming
    Replies: 18
    Last Post: 11-21-2004, 04:04 PM
  5. Debug Assertion error when freeing memory
    By foniks munkee in forum C Programming
    Replies: 2
    Last Post: 02-28-2002, 05:57 PM