b2RevoluteJoint Causes Segment Fault on Deletion

I want to make a Temporary Joint for swinging on a chain rope and on the Space bar's release its supposed to delete the Joint and drop the player.
But instead the entire program crashes and the debugger says this on the stack:

Code:
#0 BAADF00D   ?? () (??:??)
#1 004156DB   b2Joint::Destroy(b2Joint*, b2BlockAllocator*) () (??:??)
#2 0040B7E3   b2World::DestroyJoint(b2Joint*) () (??:??)
#3 004042C7   Player::Update(this=0x2768a0) (C:\Mine\Project Red\Player.cpp:283)
#4 0040274C   _fu0___ZN2sf5Color5BlackE() (C:\Mine\Project Red\Game.cpp:130)
#5 00402AC0   Game::OnInit(this=0x28fefc) (C:\Mine\Project Red\Game.cpp:182)
#6 004020B4   Game::Game(this=0x28fefc) (C:\Mine\Project Red\Game.cpp:38)
#7 0040364A   main(argc=1, argv=0xb334d0) (C:\Mine\Project Red\Main.cpp:6)




I got a small byte of code here for you guys to help solve this.

Code:
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && RopePTR != NULL)
        {
            b2RevoluteJointDef jd;
            jd.collideConnected = false;

            b2Vec2 anchor(m_body->GetWorldCenter().x, m_body->GetWorldCenter().y);
            jd.Initialize(m_body, RopePTR, anchor);
            Joint = (b2RevoluteJoint*)m_body->GetWorld()->CreateJoint(&jd);

        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space) == false && GrabbingRope == true)
        {
            GrabbingRope = false;
            m_body->GetWorld()->DestroyJoint((b2RevoluteJoint*)Joint);
        }


The collision and the segment of the the Chain rope is found with a Collision Listener.
I'm sure there is a better way to do all the code here but I'v tried so much here and I keep Modifying the code to the point where I don't care about what it looks like.

Using Box2D version 2.2.1 if this helps even a little bit... In case there is a version I'm not aware of.
Oh and Hello to everyone... First post here.
0xBAADF00D : Used by Microsoft's LocalAlloc(LMEM_FIXED) to mark uninitialised allocated heap memory

Does that isKeyPressed function get called every time step, like 60 times per second or something?
You might want to safeguard against deleting the joint more than once, for example by setting Joint to NULL after deleting, and checking it's not NULL before attempting to delete it again. Although you are checking the GrabbingRope variable, maybe the Joint pointer is getting messed up somewhere else. You could set a data breakpoint to watch for the value of Joint being changed.
I'l try what you said and post back. Do you think soon you can make a tut on temporary joints and/or welds... I'm sure you could come up with a better way to do this.
Well yes, I do see this kind of problem happening every now and then but it's a general programming thing, rather than something specific to Box2D.
You just need to be careful when using pointers. If you want to make sure a pointer doesn't get messed up, you could check that some simple conditions are correct before modifying it.

As a rough outline:
1. Set the pointer to NULL when the program starts. This will give you a value to compare with (NULL) to check if the pointer is in use.
2. Before assigning a value to the pointer, make sure it is NULL. If it's not, then you will be leaking memory or in this case losing a reference to an existing joint.
3. After releasing the pointer, set it back to NULL again so you know it's ok to be re-used.

For 2. and 3. in this case where you are trying to have only one joint existing at a time...
Code:
if ( someInputConditionToCreateJoint ) {
    //check joint does NOT already exist
    assert( myjoint == NULL );
    if ( myjoint == NULL )
        myjoint = CreateJoint( ... );
}

if ( someInputConditionToDestroyJoint ) {
    //check joint DOES already exist
    assert( myjoint != NULL );
    if ( myjoint != NULL ) {
        DestroyJoint( myjoint );
        myjoint = NULL;
    }
}
Asserts are handy to draw your attention to problems immediately during development, but it may be better to use a preprocessor macro so that you can disable them for a release build.

Original thread: https://box2d.org/forum/viewtopic.php?t=9051