Cocos2d iphone 世界->;触摸时破坏关节(鼠标点):EXC_坏访问

Cocos2d iphone 世界->;触摸时破坏关节(鼠标点):EXC_坏访问,cocos2d-iphone,box2d,box2d-iphone,Cocos2d Iphone,Box2d,Box2d Iphone,我有多个物理身体的精灵。用户一次移动一个精灵。将创建鼠标关节以支持移动。使用cocos2dv2.0 在查询abb(&callback,aabb)报告夹具后,在cctouchesStart中初始化鼠标关节。下面三种方法之间的逻辑重新鼠标连接 在接口内 b2World* world; GLESDebugDraw *m_debugDraw; b2MouseJoint *mouseJoint; b2Body* groundBody; float

我有多个物理身体的精灵。用户一次移动一个精灵。将创建鼠标关节以支持移动。使用cocos2dv2.0

在查询abb(&callback,aabb)报告夹具后,在cctouchesStart中初始化鼠标关节。下面三种方法之间的逻辑重新鼠标连接

在接口内

b2World* world;             
GLESDebugDraw *m_debugDraw; 

b2MouseJoint *mouseJoint;
b2Body* groundBody;              

float _boxHeight;
float _boxWidth;
int sectionW;

b2MouseJoint *m_mouseJoint;
实施

大多数情况下,我会在触摸结束时在上面一行上获得EXC_BAD_访问,xcode会在线程上显示以下内容 线程1

在上面的0步处,它显示在右侧窗口中 错误:地址不包含指向对象文件中节的节

到目前为止,我一直遵循提供的建议,但没有成功,此时无法看到是什么导致了这种情况,在测试时,我倾向于认为我销毁对象的方式导致了问题(即,如果我禁用了销毁对象的代码),则不存在这种错误。任何帮助都将不胜感激

完整代码如下

-(id) init
{
if( (self=[super init])) {

    self.isTouchEnabled = YES;

        // Load physics file
        [[GB2ShapeCache sharedShapeCache] addShapesWithFile:@"imagesphysics.plist"];

        // Add number images to cache
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"images.plist"];

        // init physics
        [self initPhysics];

        // Tags for the options are based on 500
        int numberOfOptions = 2;
        CGSize s = [CCDirector sharedDirector].winSize;

        sectionW = s.width / numberOfOptions;

        for (int i = 0; i < 2; i++) { 

            // Add target matching Sprites
            CCSprite *targetSprite = [CCSprite spriteWithSpriteFrameName:[NSString stringWithFormat:@"%d.png",i+1]];
            targetSprite.position = CGPointMake(i * sectionW + (sectionW/2.0)  ,s.height * 0.75); 
            [targetSprite runAction:[CCTintBy actionWithDuration:0.2f red:50 green:50 blue:40]];
            targetSprite.scale = 0.6f;
            [self addChild:targetSprite z:30 tag:i+1];

            // Add source matching physics sprites
            [self addNewSpriteAtPosition:CGPointMake(i * sectionW + (sectionW/2.0)  ,s.height * 0.35) number:i bodyType:b2_dynamicBody];
        }

        [self scheduleUpdate];
    }
    return self;
}

// Add Sprites

-(void) addNewSpriteAtPosition:(CGPoint)p number:(int)number bodyType:(b2BodyType)bodyType
{
    CCLOG(@"Add sprite %0.2f x %02.f",p.x,p.y);

    CCSprite *sprite = [CCSprite spriteWithSpriteFrameName:[NSString stringWithFormat:@"%d.png",number+1]];
    sprite.scale = 0.6f;

    sprite.position = ccp(p.x,p.y);
    [self addChild:sprite z:35 tag:(number+1)+100];     

    // Define the dynamic body.
    b2BodyDef bodyDef;
    bodyDef.type = bodyType; 
    bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO);

    bodyDef.userData = sprite;

    b2Body *body = world->CreateBody(&bodyDef);

    // Load the fixture using the vertices file generated by Physics Editor
    [[GB2ShapeCache sharedShapeCache] addFixturesToBody:body forShapeName:[NSString stringWithFormat:@"%d",number+1] forSprite:sprite];
    [sprite setAnchorPoint:
     [[GB2ShapeCache sharedShapeCache] anchorPointForShape:[NSString stringWithFormat:@"%d",number+1]]];

}

//Update the physics

    -(void) update: (ccTime) dt
    {

        int32 velocityIterations = 8;
        int32 positionIterations = 1;

        CGSize s = [CCDirector sharedDirector].winSize;

        world->Step(dt, velocityIterations, positionIterations);
        world->ClearForces();   

        // Store objects to be destroyed
        std::vector<b2Body *>toDestroy;

        CCSprite *currentSprite;
        CCSprite *targetSprite;
        int currentTag;

        for (b2Body *b = world->GetBodyList(); b; b=b->GetNext()) {

            if (b->GetUserData() != NULL) {

                CCSprite *obj = (CCSprite*)b->GetUserData();
                obj.position = CGPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO);
                obj.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());

                // Calculate the bounding box for this sprite
                _boxHeight = obj.boundingBox.size.height;
                _boxWidth = obj.boundingBox.size.width;

                currentSprite = (CCSprite *)b->GetUserData();
                currentTag = currentSprite.tag;
                targetSprite = (CCSprite *)[self getChildByTag:currentTag - 100];

                // SPECIFIC - matching sprite is tag + 100 of current userdata sprite for b object
                float distX = b->GetPosition().x * PTM_RATIO - targetSprite.position.x;
                float distY = b->GetPosition().y * PTM_RATIO - targetSprite.position.y; 

                if (distX * distX + distY * distY < (_boxWidth * _boxHeight) && b->GetType() == b2_dynamicBody) {
                    // Destroy object later
                    toDestroy.push_back(b);
                }

            } // if b-getuserdata

        }

        // Destroy objects

        std::vector<b2Body *>::iterator pos2;
        for(pos2 = toDestroy.begin(); pos2 != toDestroy.end(); ++pos2) {
            b2Body *body = *pos2;
            if (body->GetUserData() != NULL) {

                // Remove target matching
                CCSprite *sprite = (CCSprite *) body->GetUserData();
                currentTag = currentSprite.tag;
                targetSprite = (CCSprite *)[self getChildByTag:currentTag - 100];
                [self removeChild:sprite cleanup:YES];

                // Remove physics body associated with the Sprite
                world->DestroyBody(body);

    // This line has been commented  then test and the error persist!!!!
                    [self addNewSpriteAtPosition:CGPointMake(targetSprite.position.x  ,s.height * 0.75) number:targetSprite.tag-1 bodyType:b2_staticBody];
                }



        }

    }

//Init physics

-(void) initPhysics
{

    CGSize s = [[CCDirector sharedDirector] winSize];

    b2Vec2 gravity;
    gravity.Set(0.0f, -4.81f);
    world = new b2World(gravity);


    // Do we want to let bodies sleep?
    world->SetAllowSleeping(true);

    world->SetContinuousPhysics(true);

    m_debugDraw = new GLESDebugDraw( PTM_RATIO );
    //world->SetDebugDraw(m_debugDraw);

    uint32 flags = 0;
    flags += b2Draw::e_shapeBit;
            flags += b2Draw::e_jointBit;
            flags += b2Draw::e_aabbBit;
            //flags += b2Draw::e_pairBit;
            //flags += b2Draw::e_centerOfMassBit;
    m_debugDraw->SetFlags(flags);


    // Define the ground body.
    b2BodyDef groundBodyDef;
    groundBodyDef.position.Set(0, 0); // bottom-left corner

    // Call the body factory which allocates memory for the ground body
    // from a pool and creates the ground box shape (also from a pool).
    // The body is also added to the world.

    groundBody = world->CreateBody(&groundBodyDef);

    // Define the ground box shape.
    b2EdgeShape groundBox;

    // bottom

    groundBox.Set(b2Vec2(0,0), b2Vec2(s.width/PTM_RATIO,0));
    groundBody->CreateFixture(&groundBox,0);

    // top
    groundBox.Set(b2Vec2(0,s.height/PTM_RATIO), b2Vec2(s.width/PTM_RATIO,s.height/PTM_RATIO));
    groundBody->CreateFixture(&groundBox,0);

    // left
    groundBox.Set(b2Vec2(0,s.height/PTM_RATIO), b2Vec2(0,0));
    groundBody->CreateFixture(&groundBox,0);

    // right
    groundBox.Set(b2Vec2(s.width/PTM_RATIO,s.height/PTM_RATIO), b2Vec2(s.width/PTM_RATIO,0));
    groundBody->CreateFixture(&groundBox,0);
}

//Touches handling

-(void)registerWithTouchDispatcher {

    [[[CCDirector sharedDirector] touchDispatcher] addStandardDelegate:self priority:0];
}

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (mouseJoint != NULL) return;

    UITouch *myTouch = [touches anyObject];
    CGPoint location = [myTouch locationInView:[myTouch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];
    b2Vec2 p = b2Vec2(location.x / PTM_RATIO, location.y / PTM_RATIO);

    // Make a small box.
    b2AABB aabb;
    b2Vec2 d;
    d.Set(0.001f, 0.001f);
    aabb.lowerBound = p - d;
    aabb.upperBound = p + d;

    // Query the world for overlapping shapes.
    QueryCallback callback(p);
    world->QueryAABB(&callback, aabb);

    if (callback.m_fixture)
    {
        b2Body* body = callback.m_fixture->GetBody();
        b2MouseJointDef md;
        md.bodyA = groundBody;
        md.bodyB = body;
        md.target = p;
        md.maxForce = 1500.0f * body->GetMass();

        mouseJoint = nil;
        mouseJoint = (b2MouseJoint*)world->CreateJoint(&md);
        pointer = &mouseJoint;
        NSLog(@"Pointer: %p", *pointer);
        //mouseJoint = (b2MouseJoint*)world->CreateJoint(&md);
        body->SetAwake(true);
    }
    [self ccTouchesMoved:touches withEvent:event];
}

-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (mouseJoint == NULL) return;

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView: [touch view]];
    location = [[CCDirector sharedDirector] convertToGL: location];
    b2Vec2 locationWorld = b2Vec2(location.x / PTM_RATIO, location.y / PTM_RATIO);

    mouseJoint->SetTarget(locationWorld);

}

-(void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (mouseJoint) {
        world->DestroyJoint(mouseJoint);  // TODO INVESTIGATE WHY THIS CAUSES A BAD ACCESS ERROR
        mouseJoint = nil;

    }
}

-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{


    if (mouseJoint) {

        //CCLOG(@"WORLD is nil %d",world != nil);
        //CCLOG(@"MOUSEJOINT is nil %d",mouseJoint != nil);

        NSLog(@"Pointer: %p", *pointer);

        mouseJoint->SetTarget(b2Vec2_zero);
        world->DestroyJoint(mouseJoint);  // TODO INVESTIGATE WHY THIS CAUSES A BAD ACCESS ERROR
        mouseJoint = NULL;
        CCLOG(@"MOUSE JOINT WAS DESTROYED SUCCESSFULLY!!!!!");
    }
}

//Query Callback


    class QueryCallback : public b2QueryCallback
    {
    public:
        QueryCallback(const b2Vec2& point)
        {
            m_point = point;
            m_fixture = NULL;
        }

        bool ReportFixture(b2Fixture* fixture)
        {
            b2Body* body = fixture->GetBody();
            if (body->GetType() == b2_dynamicBody)
            {
                bool inside = fixture->TestPoint(m_point);
                if (inside)
                {
                    m_fixture = fixture;

                    // We are done, terminate the query.
                    return false;
                }
            }

            // Continue the query.
            return true;
        }

        b2Vec2 m_point;
        b2Fixture* m_fixture;
    };
-(id)init
{
if((self=[super init])){
self.isTouchEnabled=是;
//加载物理文件
[[GB2ShapeCache sharedShapeCache]addShapesWithFile:@“imagesphysics.plist”];
//将数字图像添加到缓存
[[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:@“images.plist”];
//初始物理
[自始物理];
//选项的标记基于500
int numberOfOptions=2;
CGSize=[CCDirector sharedDirector].winSize;
截面W=s.宽度/数量选项;
对于(int i=0;i<2;i++){
//添加目标匹配精灵
CCSprite*targetSprite=[CCSprite spritewithpriteframename:[NSString stringWithFormat:@“%d.png”,i+1]];
targetSprite.position=CGPointMake(i*sectionW+(sectionW/2.0),s.height*0.75);
[targetSprite runAction:[CCTintBy Action,持续时间:0.2f红色:50绿色:50蓝色:40];
targetSprite.scale=0.6f;
[self-addChild:targetSprite z:30标签:i+1];
//添加源匹配物理精灵
[self-addNewSpriteAtPosition:CGPointMake(i*sectionW+(sectionW/2.0),s.height*0.35)编号:i车身类型:b2_dynamicBody];
}
[自计划更新];
}
回归自我;
}
//添加雪碧
-(void)addNewSpriteAtPosition:(CGPoint)p number:(int)number bodyType:(b2BodyType)bodyType
{
CCLOG(@“添加雪碧%0.2f x%02.f”,p.x,p.y);
CCSprite*sprite=[CCSprite SpriteWithPriteFrameName:[NSString stringWithFormat:@“%d.png”,数字+1]];
sprite.scale=0.6f;
sprite.position=ccp(p.x,p.y);
[self addChild:sprite z:35标签:(数字+1)+100];
//定义动态实体。
b2BodyDef-bodyDef;
bodyDef.type=bodyType;
车身定义位置设置(p.x/PTM_比率、p.y/PTM_比率);
bodyDef.userData=sprite;
b2Body*body=world->CreateBody(&bodyDef);
//使用物理编辑器生成的顶点文件加载设备
[[GB2ShapeCache sharedShapeCache]addFixturesToBody:body for ShapeName:[NSString stringWithFormat:@“%d”,number+1]for sprite:sprite];
[sprite setAnchorPoint:
[[GB2ShapeCache sharedShapeCache]形状的主播点:[NSString stringWithFormat:@“%d”,数字+1]];
}
//更新物理学
-(无效)更新:(ccTime)dt
{
int32速度迭代=8;
int32=1;
CGSize=[CCDirector sharedDirector].winSize;
世界->步长(dt、速度迭代、位置迭代);
世界->清洁力量();
//存储要销毁的对象
std::矢量编码;
CCSprite*当前sprite;
CCSprite*targetSprite;
int-currentTag;
对于(b2Body*b=world->GetBodyList();b;b=b->GetNext()){
如果(b->GetUserData()!=NULL){
CCSprite*obj=(CCSprite*)b->GetUserData();
obj.position=CGPointMake(b->GetPosition().x*PTM_比率,b->GetPosition().y*PTM_比率);
obj.rotation=-1*CC_弧度到_度(b->GetAngle());
//计算此精灵的边界框
_boxHeight=obj.boundingBox.size.height;
_boxWidth=obj.boundingBox.size.width;
currentSprite=(CCSprite*)b->GetUserData();
currentTag=currentSprite.tag;
targetSprite=(CCSprite*)[self-getChildByTag:currentTag-100];
//特定-匹配的精灵是b对象的当前userdata精灵的标记+100
float distX=b->GetPosition().x*PTM_RATIO-targetSprite.position.x;
float distY=b->GetPosition().y*PTM_RATIO-targetSprite.position.y;
if(distX*distX+distY*distY<(\u-boxWidth*\u-boxHeight)&&b->GetType()==b2\u-dynamicBody){
//稍后销毁对象
toDestroy.向后推(b);
}
}//如果b-getuserdata
}
//破坏物体
std::vector::迭代器pos2;
for(pos2=toDestroy.begin();pos2!=toDestroy.end();+pos2){
b2Body*body=*pos2;
如果(body->GetUserData()!=NULL){
//删除目标匹配
CCSprite*sprite=(CCSprite*)body->GetUserData();
currentTag=currentSprite.tag;
targetSprite=(CCSprite*)[self-getChildByTag:currentTag-100];
[自移除儿童:精灵清理:是];
//移除与精灵关联的物理实体
世界->毁灭身体(身体);
//该行已被注释,然后测试,错误仍然存在!!!!
[self-addNewSpriteAtPosition:CGPointMake(targetSprite.position.x,s.height*0.75)编号:targetSprite.tag-1 bodyType:b2_staticBody];
}
}
}
//初始物理
-(空)物理
{
CGS大小=[
-(id) init
{
if( (self=[super init])) {

    self.isTouchEnabled = YES;

        // Load physics file
        [[GB2ShapeCache sharedShapeCache] addShapesWithFile:@"imagesphysics.plist"];

        // Add number images to cache
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"images.plist"];

        // init physics
        [self initPhysics];

        // Tags for the options are based on 500
        int numberOfOptions = 2;
        CGSize s = [CCDirector sharedDirector].winSize;

        sectionW = s.width / numberOfOptions;

        for (int i = 0; i < 2; i++) { 

            // Add target matching Sprites
            CCSprite *targetSprite = [CCSprite spriteWithSpriteFrameName:[NSString stringWithFormat:@"%d.png",i+1]];
            targetSprite.position = CGPointMake(i * sectionW + (sectionW/2.0)  ,s.height * 0.75); 
            [targetSprite runAction:[CCTintBy actionWithDuration:0.2f red:50 green:50 blue:40]];
            targetSprite.scale = 0.6f;
            [self addChild:targetSprite z:30 tag:i+1];

            // Add source matching physics sprites
            [self addNewSpriteAtPosition:CGPointMake(i * sectionW + (sectionW/2.0)  ,s.height * 0.35) number:i bodyType:b2_dynamicBody];
        }

        [self scheduleUpdate];
    }
    return self;
}

// Add Sprites

-(void) addNewSpriteAtPosition:(CGPoint)p number:(int)number bodyType:(b2BodyType)bodyType
{
    CCLOG(@"Add sprite %0.2f x %02.f",p.x,p.y);

    CCSprite *sprite = [CCSprite spriteWithSpriteFrameName:[NSString stringWithFormat:@"%d.png",number+1]];
    sprite.scale = 0.6f;

    sprite.position = ccp(p.x,p.y);
    [self addChild:sprite z:35 tag:(number+1)+100];     

    // Define the dynamic body.
    b2BodyDef bodyDef;
    bodyDef.type = bodyType; 
    bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO);

    bodyDef.userData = sprite;

    b2Body *body = world->CreateBody(&bodyDef);

    // Load the fixture using the vertices file generated by Physics Editor
    [[GB2ShapeCache sharedShapeCache] addFixturesToBody:body forShapeName:[NSString stringWithFormat:@"%d",number+1] forSprite:sprite];
    [sprite setAnchorPoint:
     [[GB2ShapeCache sharedShapeCache] anchorPointForShape:[NSString stringWithFormat:@"%d",number+1]]];

}

//Update the physics

    -(void) update: (ccTime) dt
    {

        int32 velocityIterations = 8;
        int32 positionIterations = 1;

        CGSize s = [CCDirector sharedDirector].winSize;

        world->Step(dt, velocityIterations, positionIterations);
        world->ClearForces();   

        // Store objects to be destroyed
        std::vector<b2Body *>toDestroy;

        CCSprite *currentSprite;
        CCSprite *targetSprite;
        int currentTag;

        for (b2Body *b = world->GetBodyList(); b; b=b->GetNext()) {

            if (b->GetUserData() != NULL) {

                CCSprite *obj = (CCSprite*)b->GetUserData();
                obj.position = CGPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO);
                obj.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());

                // Calculate the bounding box for this sprite
                _boxHeight = obj.boundingBox.size.height;
                _boxWidth = obj.boundingBox.size.width;

                currentSprite = (CCSprite *)b->GetUserData();
                currentTag = currentSprite.tag;
                targetSprite = (CCSprite *)[self getChildByTag:currentTag - 100];

                // SPECIFIC - matching sprite is tag + 100 of current userdata sprite for b object
                float distX = b->GetPosition().x * PTM_RATIO - targetSprite.position.x;
                float distY = b->GetPosition().y * PTM_RATIO - targetSprite.position.y; 

                if (distX * distX + distY * distY < (_boxWidth * _boxHeight) && b->GetType() == b2_dynamicBody) {
                    // Destroy object later
                    toDestroy.push_back(b);
                }

            } // if b-getuserdata

        }

        // Destroy objects

        std::vector<b2Body *>::iterator pos2;
        for(pos2 = toDestroy.begin(); pos2 != toDestroy.end(); ++pos2) {
            b2Body *body = *pos2;
            if (body->GetUserData() != NULL) {

                // Remove target matching
                CCSprite *sprite = (CCSprite *) body->GetUserData();
                currentTag = currentSprite.tag;
                targetSprite = (CCSprite *)[self getChildByTag:currentTag - 100];
                [self removeChild:sprite cleanup:YES];

                // Remove physics body associated with the Sprite
                world->DestroyBody(body);

    // This line has been commented  then test and the error persist!!!!
                    [self addNewSpriteAtPosition:CGPointMake(targetSprite.position.x  ,s.height * 0.75) number:targetSprite.tag-1 bodyType:b2_staticBody];
                }



        }

    }

//Init physics

-(void) initPhysics
{

    CGSize s = [[CCDirector sharedDirector] winSize];

    b2Vec2 gravity;
    gravity.Set(0.0f, -4.81f);
    world = new b2World(gravity);


    // Do we want to let bodies sleep?
    world->SetAllowSleeping(true);

    world->SetContinuousPhysics(true);

    m_debugDraw = new GLESDebugDraw( PTM_RATIO );
    //world->SetDebugDraw(m_debugDraw);

    uint32 flags = 0;
    flags += b2Draw::e_shapeBit;
            flags += b2Draw::e_jointBit;
            flags += b2Draw::e_aabbBit;
            //flags += b2Draw::e_pairBit;
            //flags += b2Draw::e_centerOfMassBit;
    m_debugDraw->SetFlags(flags);


    // Define the ground body.
    b2BodyDef groundBodyDef;
    groundBodyDef.position.Set(0, 0); // bottom-left corner

    // Call the body factory which allocates memory for the ground body
    // from a pool and creates the ground box shape (also from a pool).
    // The body is also added to the world.

    groundBody = world->CreateBody(&groundBodyDef);

    // Define the ground box shape.
    b2EdgeShape groundBox;

    // bottom

    groundBox.Set(b2Vec2(0,0), b2Vec2(s.width/PTM_RATIO,0));
    groundBody->CreateFixture(&groundBox,0);

    // top
    groundBox.Set(b2Vec2(0,s.height/PTM_RATIO), b2Vec2(s.width/PTM_RATIO,s.height/PTM_RATIO));
    groundBody->CreateFixture(&groundBox,0);

    // left
    groundBox.Set(b2Vec2(0,s.height/PTM_RATIO), b2Vec2(0,0));
    groundBody->CreateFixture(&groundBox,0);

    // right
    groundBox.Set(b2Vec2(s.width/PTM_RATIO,s.height/PTM_RATIO), b2Vec2(s.width/PTM_RATIO,0));
    groundBody->CreateFixture(&groundBox,0);
}

//Touches handling

-(void)registerWithTouchDispatcher {

    [[[CCDirector sharedDirector] touchDispatcher] addStandardDelegate:self priority:0];
}

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (mouseJoint != NULL) return;

    UITouch *myTouch = [touches anyObject];
    CGPoint location = [myTouch locationInView:[myTouch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];
    b2Vec2 p = b2Vec2(location.x / PTM_RATIO, location.y / PTM_RATIO);

    // Make a small box.
    b2AABB aabb;
    b2Vec2 d;
    d.Set(0.001f, 0.001f);
    aabb.lowerBound = p - d;
    aabb.upperBound = p + d;

    // Query the world for overlapping shapes.
    QueryCallback callback(p);
    world->QueryAABB(&callback, aabb);

    if (callback.m_fixture)
    {
        b2Body* body = callback.m_fixture->GetBody();
        b2MouseJointDef md;
        md.bodyA = groundBody;
        md.bodyB = body;
        md.target = p;
        md.maxForce = 1500.0f * body->GetMass();

        mouseJoint = nil;
        mouseJoint = (b2MouseJoint*)world->CreateJoint(&md);
        pointer = &mouseJoint;
        NSLog(@"Pointer: %p", *pointer);
        //mouseJoint = (b2MouseJoint*)world->CreateJoint(&md);
        body->SetAwake(true);
    }
    [self ccTouchesMoved:touches withEvent:event];
}

-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (mouseJoint == NULL) return;

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView: [touch view]];
    location = [[CCDirector sharedDirector] convertToGL: location];
    b2Vec2 locationWorld = b2Vec2(location.x / PTM_RATIO, location.y / PTM_RATIO);

    mouseJoint->SetTarget(locationWorld);

}

-(void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (mouseJoint) {
        world->DestroyJoint(mouseJoint);  // TODO INVESTIGATE WHY THIS CAUSES A BAD ACCESS ERROR
        mouseJoint = nil;

    }
}

-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{


    if (mouseJoint) {

        //CCLOG(@"WORLD is nil %d",world != nil);
        //CCLOG(@"MOUSEJOINT is nil %d",mouseJoint != nil);

        NSLog(@"Pointer: %p", *pointer);

        mouseJoint->SetTarget(b2Vec2_zero);
        world->DestroyJoint(mouseJoint);  // TODO INVESTIGATE WHY THIS CAUSES A BAD ACCESS ERROR
        mouseJoint = NULL;
        CCLOG(@"MOUSE JOINT WAS DESTROYED SUCCESSFULLY!!!!!");
    }
}

//Query Callback


    class QueryCallback : public b2QueryCallback
    {
    public:
        QueryCallback(const b2Vec2& point)
        {
            m_point = point;
            m_fixture = NULL;
        }

        bool ReportFixture(b2Fixture* fixture)
        {
            b2Body* body = fixture->GetBody();
            if (body->GetType() == b2_dynamicBody)
            {
                bool inside = fixture->TestPoint(m_point);
                if (inside)
                {
                    m_fixture = fixture;

                    // We are done, terminate the query.
                    return false;
                }
            }

            // Continue the query.
            return true;
        }

        b2Vec2 m_point;
        b2Fixture* m_fixture;
    };
-(void)touchBegin:(LHTouchInfo*)info {
    CGPoint location = [[CCDirector sharedDirector] convertToGL: [info.touch locationInView: [info.touch view]]];
    _lastBrickTouched = info.sprite;
    _mouseJoint = [lh mouseJointForBody: _lastBrickTouched.body touchPoint: location];
}

-(void)touchMoved:(LHTouchInfo*)info{
    CGPoint touchZone = [info.touch locationInView:[info.touch view]];
    touchZone = [[CCDirector sharedDirector] convertToGL:touchZone];
    b2Vec2 p;
    p.Set(touchZone.x/PTM_RATIO, touchZone.y/PTM_RATIO);

    if (_mouseJoint != nil)
    {
        _mouseJoint->SetTarget(p);
    }
}

-(void)touchEnded:(LHTouchInfo*)info {
    if (_mouseJoint != nil) {
        world->DestroyJoint(_mouseJoint);
        _mouseJoint = nil;
    }
}
- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event 
{
    if (mouseJoint != NULL)
    {
        world->DestroyJoint(mouseJoint);
        mouseJoint = NULL;
    }
}
-(void)destroyActor:(ObjectView *)objectView
{
    b2Body* b = (b2Body*)objectView.tag;

    // Destroy mouseJoint if attached to body b

    b2JointEdge* jl = b->GetJointList();

    while (jl)
    {
        b2Joint* j = jl->joint;

        if (j == mouseJoint)
        {
            world->DestroyJoint(j);
            mouseJoint = NULL;
            break;
        }

        jl = jl->next;
    }

    world->DestroyBody(b);
    [objectView removeFromSuperview];
    [objectView release];
}
Thread 1, Queue : com.apple.main-thread
error: address doesn't contain a section that points to a section in a object file
class MouseJointHelper
{
  Body fictionalBody;

  MouseJoint createJoint(...) //here we create joint, using fictional body
  void destroyJointAndBodies(MouseJoint joint) //destroy joint and bodyA, assuming that bodyB is the same for all (fictional body)
  void dispose() //in destructor method destroy fictional body
};