Martin Berger
Martin Berger

Reputation: 1708

Debug Assertion Failed

Using Visual Studio 2010, C++. Programming level: beginner.

I have a code from a book Windows Game Programming Gurus and up until now have managed all problems i have stumbled upon.

But this i don't know what it is.

Here is a screenshot of an error:

enter image description here

That is one nice 8-bit image...

Now, it says File: f:\dd...

In my case f: drive is empty cd-rom...

This is the line where i think error is happening:

_lseek(file_handle, -((int) (bitmap->bitmapinfoheader.biSizeImage)), SEEK_END);   

What is this thing?

Upvotes: 3

Views: 17163

Answers (5)

rauprog
rauprog

Reputation: 243

lseek crashes with the debug assertion failed error because it is a 16bit function. I found this out by looking at a chart of 16bit and 32bit functions on the microsoft website.

Solution is to use _llseek. _llseek is a 32bit function and can run on 64bit computers. You do not need to change any parameters from _lseek to use _llseek.

_lseek(file_handle, -((int) (bitmap->bitmapinfoheader.biSizeImage)), SEEK_END); 

becomes

_llseek(file_handle, -((int) (bitmap->bitmapinfoheader.biSizeImage)), SEEK_END); 

Upvotes: 0

Martin Berger
Martin Berger

Reputation: 1708

Full function code which uses C++ ifstream instead of low-level IO functions. Jonathan and i tried to make _lseek work only to conclude that it doesn't work... Don't know if that is entirely true, maybe there is some way it works correctly. If you (the reader) know, feel free to message me. The function now works, although main program displays image wrongly, but that is beside matter of this question, _lseek thing is solved :)

int Load_Bitmap_File(BITMAP_FILE_PTR bitmap, char *filename)   
{   
    int file_handle = 0;        // the file handle
    int index = 0;                  // looping index   
    int bitmapWidth = 0;
    int bitmapHeight = 0;
    int bitmapSize = 0;
    UCHAR *temp_buffer = NULL; // used to convert 24 bit images to 16 bit   
    streampos pos_cur;

    ifstream bitmapFile = ifstream ();
    bitmapFile.open (filename, ifstream::in);
    if (! bitmapFile.is_open ())
    {
    printError ("Error: OpenFile function failure. ");

        // abort
        return(0);
    }


    // load the bitmap file header:   
    //_lread(file_handle, &(bitmap->bitmapfileheader), sizeof(BITMAPFILEHEADER)); 
    bitmapFile.read ((char *) &(bitmap->bitmapfileheader), sizeof (BITMAPFILEHEADER));

    // test if this is a bitmap file   
    if (bitmap->bitmapfileheader.bfType != BITMAP_ID)   
    {   
        // close the file   
        //_lclose(file_handle);   
        bitmapFile.close ();
        printError ("error: wrong bitmap type");
        cout << "error: wrong bitmap type" << endl;
        // return error   
        return(0);   
    } // end if   

    // now we know this is a bitmap, so read in all the sections.   
    if (bitmap->bitmapinfoheader.biSizeImage == 0)
    printError ("error: biSizeImage equals 0");


    // now the bitmap infoheader:   

    //_lread(file_handle, &bitmap->bitmapinfoheader, sizeof(BITMAPINFOHEADER));   
    bitmapFile.seekg (sizeof (BITMAPFILEHEADER), ios::beg);
    pos_cur = bitmapFile.tellg (); // save current stream position
    bitmapFile.read ((char *) &(bitmap->bitmapinfoheader), sizeof (BITMAPINFOHEADER));

    //cout << bitmap->bitmapinfoheader.biBitCount << endl;


    // now load the color palette if there is one   
    if (bitmap->bitmapinfoheader.biBitCount == 8)   
    {   
        //_lread(file_handle, &bitmap->palette, MAX_COLORS_PALETTE * sizeof(PALETTEENTRY));   
        // not tested: 
        bitmapFile.read ((char *) &(bitmap->palette), MAX_COLORS_PALETTE * sizeof(PALETTEENTRY)); 

        // now set all the flags in the palette correctly and fix the reversed    
        // BGR RGBQUAD data format   
        for (index = 0; index < MAX_COLORS_PALETTE; index++)   
        {   
            // reverse the red and green fields   
            int temp_color                = bitmap->palette[index].peRed;   
            bitmap->palette[index].peRed  = bitmap->palette[index].peBlue;   
            bitmap->palette[index].peBlue = temp_color;   

            // always set the flags word to this   
            bitmap->palette[index].peFlags = PC_NOCOLLAPSE;   
        } // end for index   

    } // end if   

    bitmapWidth = bitmap->bitmapinfoheader.biWidth * (bitmap->bitmapinfoheader.biBitCount / 8);
    bitmapHeight = bitmap->bitmapinfoheader.biHeight; 
    bitmapSize = bitmapWidth * bitmapHeight;


    // finally the image data itself:  
    //_lseek(file_handle, -((int) (bitmap->bitmapinfoheader.biSizeImage)), SEEK_END);
    bitmapFile.seekg (-((int) bitmapSize), ios::end);
    //bitmapFile.seekg (sizeof (BITMAPINFOHEADER) + sizeof (BITMAPFILEHEADER) + MAX_COLORS_PALETTE * sizeof(PALETTEENTRY), ios::beg);

    // now read in the image, if the image is 8 or 16 bit then simply read it   
    // but if its 24 bit then read it into a temporary area and then convert   
    // it to a 16 bit image   

    if (bitmap->bitmapinfoheader.biBitCount == 8  || 
         bitmap->bitmapinfoheader.biBitCount == 16 ||    
         bitmap->bitmapinfoheader.biBitCount == 24)   
    {   
        // delete the last image if there was one   
        if (bitmap->buffer)   
            free(bitmap->buffer);   

        // allocate the memory for the image   
        //if (!(bitmap->buffer = (UCHAR *) malloc (bitmap->bitmapinfoheader.biSizeImage))) // error: biSizeImage == 0 !
        if (!(bitmap->buffer = (UCHAR *) malloc (bitmapSize)))
        {   
            // close the file   
            //_lclose(file_handle);   
            bitmapFile.close ();

            // return error   
            return(0);   
        } // end if   

        // now read it in   
        //_lread(file_handle, bitmap->buffer, bitmap->bitmapinfoheader.biSizeImage);  
        bitmapFile.read ((char *) (bitmap->buffer), bitmapSize);

    } // end if   
    else   
    {   
        // serious problem   
        return(0);   

    } // end else   


    // close the file   
    //_lclose(file_handle);  
    bitmapFile.close ();

    // flip the bitmap   

    Flip_Bitmap(bitmap->buffer,    
                    bitmap->bitmapinfoheader.biWidth * (bitmap->bitmapinfoheader.biBitCount / 8),
                    bitmap->bitmapinfoheader.biHeight);   
    //cout << "biSizeImage: " << bitmap->bitmapinfoheader.biSizeImage << endl;
    //cout << (bitmap->bitmapinfoheader.biWidth * (bitmap->bitmapinfoheader.biBitCount / 8)) * bitmap->bitmapinfoheader.biHeight << endl;

    // return success   
    return(1);   

} // end Load_Bitmap_File   

///////////////////////////////////////////////////////////   

Current full source code: http://pastebin.com/QQ6fMD7P Expiration date is set to never.

Thanks to all people contributing to this question!

Upvotes: 0

MSalters
MSalters

Reputation: 179779

The f:\dd directory is where the source code of the "C Runtime Library" (CRT) was located, when it was built. Since Microsoft built that, it doesn't correspond to your F: drive.

Anyway, the CRT detected that one of the file handles is wrong. You passed it to the CRT, so you should check why it's wrong. If you press Retry, you'll be put in the debugger. There you can see which of your functions put in the wrong file handle.

It won't tell you why the handle is wrong, though. A common reason is that you tried to open a file, and forgot to check if it succeeded. You only get a file handle if the file name is valid, and you're allowed to read that file.

Upvotes: 3

Jonathan Merlet
Jonathan Merlet

Reputation: 285

Looks like your file_handle is wrong. Are you sure the opening of your image succeeded ?

Upvotes: 1

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84149

The assertion happens in the C library. It makes sure you pass valid argument to the lseek() function.

You probably did not check for errors after doing open() or creat() on the file.

Upvotes: 2

Related Questions