Assertion Failure

This is a discussion on Assertion Failure within the Windows Programming forums, part of the Platform Specific Boards category; Anybody here know anything about assertion failures? I can't figure it out, I'm using MSVC++. I tried the debug thing ...

  1. #1
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    Assertion Failure

    Anybody here know anything about assertion failures? I can't figure it out, I'm using MSVC++. I tried the debug thing but all it led me to was fgets.c and a line of code about a string and the null thingy. _ASSERTE(str != NULL); If anyone can help me I'd greatly appreciate it, thanks.
    Attached Files Attached Files

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I hate to state the apparently obvious, but it appears one of the pointers you passed to fgets() was NULL. Maybe you should double check your code surrounding the use of that function.

    Alternatively, step through the code with a debuger.

    Last, check that the fopen() worked.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    ..

    Well, I've narrowed it down to here by finding the uses of the fget function:

    Code:
    void readstr(FILE *f,char *string)
    {
    if (string == NULL) { MessageBox(HWND_DESKTOP, "Ahem", "Error!", MB_OK|MB_ICONERROR); return; }
    	do
    	{
    		fgets(string, 255, f);
    	} while ((string[0] == '/') || (string[0] == '\n'));
    	return;
    }
    any hints on what I should do?

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>fgets(string, 255, f);

    1 - Determine if it is definately this call that is causing the problem. To do this, maybe put a messagebox call in before and after calling fgets(). This way you can see if you're failing here or not.

    2 - Determine if string and f contain valid values at this point. Prove to yourself that they are (don't presume)

    string is a reserved name, btw.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    ..

    Ok so I put a message box right above and in the part wherer I thought went wrong and it brought up a window. I took out the message box above the part where it went wrong and executed again and it came up with the assertion failure. I've narrowed it down to that spot. Here's the code:

    Code:
    void readstr(FILE *f,char *string)
    {
    MessageBox(NULL, TEXT("Check!"), TEXT("Test:"), 0);
    if (string == NULL) 
    { MessageBox(HWND_DESKTOP, "Ahem", "Error!", MB_OK|MB_ICONERROR); return; }
    	do
    	{
    		fgets(string, 255, f);
    	} while ((string[0] == '/') || (string[0] == '\n'));
    	return;
    }
    Crack'in Down!!

  6. #6
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    ..

    Here we go, I followed the function and the variables connected to it to this:

    Code:
                    FILE *filein;
    	char oneline[255];
    	filein = fopen("World.txt", "rt");				// File To Load World Data From
    
    	readstr(filein,oneline);

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Use your debugger to:
    2 - Determine if string and f contain valid values at this point. Prove to yourself that they are (don't presume)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    ?

    I have no idea how to run the MSVC++ Debugger but from what I can tell it's all doing what it should, although it isn't. I have no idea what to do. I've limited where the problem is but these variables run through the entire program, and I didn't even create the code(it came from nehe.gamedev.net)so I have no idea what does what yet. ::bangs head on table::

  9. #9
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    ...

    I ran it all through again with what I know. The bug's in there or in fgets.c which I doubt.

    Code:
    if (chk == NULL) 
    { MessageBox(HWND_DESKTOP, "Ahem", "Error!", MB_OK|MB_ICONERROR); return; }
    	do
    	{
    		fgets(chk, 255, f);
    	} while ((chk[0] == '/') || (chk[0] == '\n'));
    	return;
    }
    
    void SetupWorld()
    {
    	float x, y, z, u, v;
    	int numtriangles;
    	FILE *filein;
    	char oneline[255];
    	filein = fopen("World.txt", "rt");				// File To Load World Data From
    
    	readstr(filein,oneline);

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    char buffer[1024];
    sprintf(buffer, "%p %p", string, f);
    This will create a variable buffer that holds the pointer addresses. Use messagebox to print buffer, and see the values of the pointers. If one is 0x00000000, that means it's NULL and you'll have to track backwards to find out why.

    Also, just to double check, put another message box AFTER the fgets(), just to prove that you never get to it (in other words, the assert kicks in first).

    [edit]
    You're still not checking the fopen(). Make sure it's return isn't NULL.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    ..

    I checked after fgets and nothing pops up. The buffer messagebox comes up as 0066FA6C 00000000. And as for the fgets.c thing, I checked it. It runs a function that checks to see if there are less than 0 characters otherwise it returns NULL.

    Code:
    _ASSERTE(str != NULL);
    
            if (count <= 0)
                    return(NULL);
    I'm not sure about that fgets.c thing though it was kind'a confusing but thats where the debug ran to and thats what I think it does.

  12. #12
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    Unhappy ..

    I know bumping is a bad habit but I've been working to solve this problem for 3 days now; asking forums, checking code and reading in books; and now I'm closer than ever to solving this problem. Please help.

  13. #13
    Unregistere21
    Guest
    Do the fopen like this
    Code:
    if ((filein = fopen("World.txt", "rt")) == NULL)
    {
        /* Error occured notify user */
        return 1;
    }
    Your file pointer appears to be NULL, so your fopen probably isn't working.

  14. #14
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    ..

    Heres the code for fgets.c

    Code:
    /***
    *fgets.c - get string from a file
    *
    *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
    *
    *Purpose:
    *       defines fgets() - read a string from a file
    *
    *******************************************************************************/
    
    #include <cruntime.h>
    #include <stdio.h>
    #include <dbgint.h>
    #include <file2.h>
    #include <internal.h>
    #include <mtdll.h>
    #include <tchar.h>
    
    /***
    *char *fgets(string, count, stream) - input string from a stream
    *
    *Purpose:
    *       get a string, up to count-1 chars or '\n', whichever comes first,
    *       append '\0' and put the whole thing into string. the '\n' IS included
    *       in the string. if count<=1 no input is requested. if EOF is found
    *       immediately, return NULL. if EOF found after chars read, let EOF
    *       finish the string as '\n' would.
    *
    *Entry:
    *       char *string - pointer to place to store string
    *       int count - max characters to place at string (include \0)
    *       FILE *stream - stream to read from
    *
    *Exit:
    *       returns string with text read from file in it.
    *       if count <= 0 return NULL
    *       if count == 1 put null string in string
    *       returns NULL if error or end-of-file found immediately
    *
    *Exceptions:
    *
    *******************************************************************************/
    
    #ifdef _UNICODE
    wchar_t * __cdecl fgetws (
    #else  /* _UNICODE */
    char * __cdecl fgets (
    #endif  /* _UNICODE */
            _TSCHAR *string,
            int count,
            FILE *str
            )
    {
            REG1 FILE *stream;
            REG2 _TSCHAR *pointer = string;
            _TSCHAR *retval = string;
            int ch;
    
            _ASSERTE(string != NULL);
            _ASSERTE(str != NULL);
    
            if (count <= 0)
                    return(NULL);
    
            /* Init stream pointer */
            stream = str;
    
            _lock_str(stream);
    
            while (--count)
            {
    #ifdef _UNICODE
                    if ((ch = _getwc_lk(stream)) == WEOF)
    #else  /* _UNICODE */
                    if ((ch = _getc_lk(stream)) == EOF)
    #endif  /* _UNICODE */
                    {
                            if (pointer == string) {
                                    retval=NULL;
                                    goto done;
                            }
    
                            break;
                    }
    
                    if ((*pointer++ = (_TSCHAR)ch) == _T('\n'))
                            break;
            }
    
            *pointer = _T('\0');
    
    /* Common return */
    done:
            _unlock_str(stream);
            return(retval);
    }
    I looked through it but found nothing of interest... The debug points to _ASSERTE(str != NULL); but I didn't know what to do with it, if anything. Anybody notice anything wrong here?

  15. #15
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You've already been told the answer... the fopen() isn't working. Within fgets the str variable is the FILE* that you passed as the third parameter. Use the revised fopen() code already posted.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File I/O Assertion Failure in VS2008
    By clegs in forum C Programming
    Replies: 5
    Last Post: 12-25-2008, 03:47 AM
  2. Debug Assertion failure problem
    By uldaman in forum C++ Programming
    Replies: 8
    Last Post: 01-21-2008, 01:22 AM
  3. debug assertion failure
    By talz13 in forum Windows Programming
    Replies: 2
    Last Post: 07-20-2004, 11:23 AM
  4. Assertion failure while creating window
    By roktsyntst in forum Windows Programming
    Replies: 0
    Last Post: 02-10-2003, 07:18 PM
  5. Assertion Failure
    By drdroid in forum Game Programming
    Replies: 9
    Last Post: 01-04-2003, 06:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21