Help fixing a pointer bug

Started by
6 comments, last by Aternus 17 years, 5 months ago
This one's really starting to get to me, since I can *not* find out what's causing it. I know the exact line it crashes and everything, but I don't know why. Basically it's for my OpenGL based GUI and has to do with input handling. Whenever I press an alphabetical key the program basically crashes. But what I don't get is why it's screwing up the getWindow function, which returns a pointer to a window class. Well... this is the getWindow function:
RUIwindow* RUI::getWindow( int id )
{
    for( vWit = vWindow.begin(); vWit!=vWindow.end(); vWit++ )
    {

        if( (*vWit)->ID == id )
        {
            return (*vWit);
        }
    }

    return NULL;
}
I'm assuming something is wrong with the vector since the program still crashes even when there are no windows (which means it shouldn't loop at all, and according to some printf tests, it doesn't). This function is called a couple times during each render. It works fine, no matter how many windows are up... that is until I press a key. I'm using GLFW here, so whenever a key is pressed this is what happens:
void keyboardNormal( int key, int action )
{
   gui->handleInput( RUI_INPUT_KB_CHAR, key, action );
}
void RUI::handleInput( int INPUT_TYPE, int a, int b )
{
    switch( INPUT_TYPE )
    {
        case RUI_INPUT_KB_CHAR:
            addLetter( a, b ); //This is the one getting called
            break;

        case RUI_INPUT_KB_SPECIAL:
            addSpecial( a, b );
            break;

        case RUI_INPUT_MOUSE_PRESS:
            onMousePress( a, b );
            break;

        case RUI_INPUT_MOUSE_MOVE:
            onMouseMove( a, b );
            break;
    }

    return;
}
void RUI::addLetter( unsigned char letter, unsigned char state )
{
    if( hwndFocus != 255 && editFocus != 255 )
    {
        if( letter == RUI_KEY_BACKSPACE && getWindow( hwndFocus )->vEditBox[editFocus]->data.length() > 0 )
        {
            if( getWindow( hwndFocus )->vEditBox[editFocus]->cursorPos > 0 )
            {
                getWindow( hwndFocus )->vEditBox[editFocus]->data.erase( getWindow( hwndFocus )->vEditBox[editFocus]->cursorPos-1, 1 );
                getWindow( hwndFocus )->vEditBox[editFocus]->cursorPos--;
            }
        }

        if( letter == RUI_KEY_DELETE && getWindow( hwndFocus )->vEditBox[editFocus]->data.length() > 0 )
        {
            if( getWindow( hwndFocus )->vEditBox[editFocus]->cursorPos < getWindow( hwndFocus )->vEditBox[editFocus]->data.length() )
            {
                getWindow( hwndFocus )->vEditBox[editFocus]->data.erase( getWindow( hwndFocus )->vEditBox[editFocus]->cursorPos, 1 );
            }
        }

        else if( ( letter != RUI_KEY_BACKSPACE && letter != RUI_KEY_DELETE ) && getWindow( hwndFocus )->vEditBox[editFocus]->data.length() < getWindow( hwndFocus )->vEditBox[editFocus]->size )
        {
            std::string ltr = ""; ltr += letter;
            getWindow( hwndFocus )->vEditBox[editFocus]->data.insert( getWindow( hwndFocus )->vEditBox[editFocus]->cursorPos, ltr.c_str() );
            getWindow( hwndFocus )->vEditBox[editFocus]->cursorPos++;
        }
    }
    printf("TEST"); //This manages to get printed, so I know it's not in the function
    return;
}
And after that everything returns... I assume it's not handleInput since everything works fine when mouse movement, special keys, and mouse button input is handled. Sorry to have to come to the forums with such a question that I should be able to fix myself, but I really can't. I've been stumped on this for almost 3 days now with absolutely no progress. It's times like this I wish I had a working debugger. In fact, I spent a good few hours yesterday trying to get GDB working in Code::Blocks for me to no avail. Seems PSAPI.dll is missing an export to the NtAllocateMemory function in NT.DLL... I assume this has to do with me running with Windows 98 since past NT.DLL errors seem to have been platform specific. So that's driving me nuts, too... but I guess it's an unrelated matter. Anyways, thanks to any and all who attempt to help me out here! >_O
Advertisement
At a guess your RUI object isn't valid, vWindow.begin() is the first time you actually access any member variables, so if the 'this' pointer is screwed it'll die there.

Try scattering something like:
printf("This:0x%x \n", this); // Will show you the 'this' pointerprintf("Size: %d \n", vWindow.size()); // Will die if the 'this' pointer isn't valid.

around your code.
I think its because your window is not found(for some reason??maybe OrangyTang is correct) but you enter the world of hurt by returning NULL. Personally I would throw in this instance.

RUIwindow* RUI::getWindow( int id ){    for( vWit = vWindow.begin(); vWit!=vWindow.end(); vWit++ )    {        if( (*vWit)->ID == id )        {            return (*vWit);        }    }    throw std::runtime_error("window not found!");}


edit: forgot to post the line which maybe to fault in the seg fault. It has no check if the window pointer is valid.
[source        else if( ( letter != RUI_KEY_BACKSPACE && letter != RUI_KEY_DELETE ) && getWindow( hwndFocus )->vEditBox[editFocus]->data.length() < getWindow( hwndFocus )->vEditBox[editFocus]->size )
Ah, yes... that's a good idea. It's still strange, though... I mean I stuck it in the getWindow function and it works fine giving a valid pointer and the size of 1 (there's 1 window) up until I press a key... then the size turns to 833, but the pointer's address remains the same. I must be... somehow deleting the RUI pointer? This oughta be interesting... I'll edit back with results after I scatter around what you offered.

Edit: Ah, sorry CmpDev. Seems you posted while I was typing up my reply so I didn't acknowledge you. I did have some checking for NULL before, actually, but it seemed to be crashing before it got the chance to return. Anyways, it seems the problem is even more confusing than initially thought...

THIS was the problem... which doesn't even make any sense since addSpecial shouldn't be getting called.
void RUI::addSpecial( unsigned char key, unsigned char state ){    specialKeys[key] = 1 - specialKeys[key];//<---    return;}
I guess key is larger than the array size or sommat... but it's strange since this is only called when I pressed shift, F1, ctrl, or whatever. Anyways, I guess I was editting stuff that was out of the array's allocated area and was screwing up the rest of the class, since commenting this out fixed the problem. Anywho, I can get this working on my own accord. Thanks for the help, both of you! I wouldn't have been able to find it without your assistance, even if you were both off (though that was due to my poor thinking of what was causing the bug, I mean really you couldn't have known since I didn't even mention addSpecial). Ahhhh! I can finally get back to work. Once again, thank you!
Hmm, the OP said it was dying within the getWindow call though. Although the return value isn't ever checked, so if it's not dying in getWindow then the NULL return is probably the cause.
Some extra comments about your code:
why don't you change the function signature
Quote:RUIwindow* RUI::getWindow( int id )

to
RUIwindow* const RUI::getWindow( int id )

this would throw a compile time error if you try and delete or move the pointer. This would mean some of your code needs changing, but for the better.


Check the window pointer once for the function.
Quote:
void RUI::addLetter( unsigned char letter, unsigned char state )
{
if( hwndFocus != 255 && editFocus != 255 )
{
if( letter == RUI_KEY_BACKSPACE && getWindow( hwndFocus )->vEditBox[editFocus]->data.length() > 0 )
{
if( getWindow( hwndFocus )->vEditBox[editFocus]->cursorPos > 0 )
{...


to


void RUI::addLetter( unsigned char letter, unsigned char state )
{
if( hwndFocus != 255 && editFocus != 255 )
{
RUIwindow* const w;
try{w = getWindow( hwndFocus )}
catch(std::runtime_error const& e)
{...handle and maybe rethrow...}

if( letter == RUI_KEY_BACKSPACE && w->vEditBox[editFocus]->data.length() > 0 )
{
if( w->vEditBox[editFocus]->cursorPos > 0 )
{...

I didn't see your post either lol.
Quote:
specialKeys[key] = 1 - specialKeys[key];//<---


Is specialkeys defined as unsigned char specialKeys[RANGE]? If so 1 - SOME_VALUE would cause an out of bounds error.
Alright, I will. Thanks.

Edit: Ah, no, that array is a boolean. The problem was my definitions in the file were 256 higher than they should be (for other purposes)... so doing "specialKeys[key-256] = 1 - specialKeys[key-256];" fixed it.

This topic is closed to new replies.

Advertisement