2012-08-30 42 views
0

我創建box2d多邊形,我想創建夾具附着到身體的方法之外,但是當我這樣做,我得到一個訪問衝突。Box2d C++ AccessViolation使用b2fixture出方法

我已經通過了代碼,box2d肯定是獲取夾具對象。

當我將燈具附着到身體內部的方法,它工作正常。當我在調用方法嘗試它引發訪問衝突錯誤

b2FixtureDef* LineSegment::GenerateFixture(b2Vec2 vertices[4],b2Body* body,b2World* world){ 
    b2BodyDef bodyDef; 
    //bodyDef.type = b2_kinematicBody; 
    bodyDef.position.Set(_currentStepStartPosition.x,_currentStepStartPosition.y); 
    body =world->CreateBody(&bodyDef); 
    int32 count = 4; 
    b2PolygonShape polygon; 
    polygon.Set(vertices, count); 
    b2FixtureDef fixtureDef2; 
    fixtureDef2.shape = &polygon; 
    fixtureDef2.density = 1.0f; 
    fixtureDef2.friction = 0.3f; 
    body->CreateFixture(&fixtureDef2);/// works if i attatch the fixture here here 
    return &fixtureDef2; 
} 

當我在此附上夾具失敗調用方法

bool LineSegment::GenerateNextBody(b2Body* retBody){ 
    b2BodyDef bodyDef; 
    bodyDef.type = b2_staticBody; 
    bodyDef.position.Set(_currentStepStartPosition.x,_currentStepStartPosition.y); 
    retBody =world->CreateBody(&bodyDef); 
    _currentStep++; 
    b2Vec2 vertices[4]; 
    if(_inclinePerStep != 0){ 
    GetVertsInclineSquare(vertices,_stepWidth,_thickness,_inclinePerStep); 
    }else{ 
     GetVertsSquare(vertices,_stepWidth,_thickness); 
    } 
    if(_currentStep == 1){ 
     _GameWorldVerticies[0] =b2Vec2((_currentStepStartPosition.x+vertices[0].x)*PTM_RATIO,(_currentStepStartPosition.y+vertices[0].y)*PTM_RATIO); 
     _GameWorldVerticies[3] =b2Vec2((_currentStepStartPosition.x+vertices[3].x)*PTM_RATIO,(_currentStepStartPosition.y+vertices[3].y)*PTM_RATIO); 

    } 
    b2FixtureDef* fixture = GenerateFixture(vertices,retBody,world); 

    //retBody->CreateFixture(fixture); throws a access violation if i attatch the fixture here 
    _currentStepStartPosition.x += vertices[2].x+(vertices[2].x); 
    _currentStepStartPosition.y +=(_inclinePerStep/2); 
    if(_steps <= _currentStep){ 
     _GameWorldVerticies[1] =b2Vec2((_currentStepStartPosition.x+vertices[1].x)*PTM_RATIO,(_currentStepStartPosition.y+vertices[1].y)*PTM_RATIO); 
     _GameWorldVerticies[2] =b2Vec2((_currentStepStartPosition.x+vertices[2].x)*PTM_RATIO,(_currentStepStartPosition.y+vertices[2].y)*PTM_RATIO); 
     return false; 
    } 
    return true; 
} 

UPDATE

新片段

 b2FixtureDef* fixtureDef2 = new b2FixtureDef(); 
    fixtureDef2->shape = &polygon; 
    fixtureDef2->density = 1.0f; 
    fixtureDef2->friction = 0.3f; 
    //body->CreateFixture(&fixtureDef2);/// works if i attatch the fixture here here 
    return fixtureDef2; 
+0

您是否嘗試過在調試器中運行程序?然後你可以捕捉到錯誤並檢查變量,看看它們是否全部看起來沒問題(即沒有「NULL」指針等)。 –

+0

我知道程序崩潰的地方,我沒有看到任何可能導致空指針的東西。我應該粘貼調試數據(屏幕截圖和崩潰在box2d lib) – Billybonks

回答

2

問題是你返回一個指向局部變量GenerateFixture,這是未定義的行爲。變量fixtureDef2位於堆棧上,並且當該函數返回該堆棧的區域不再有效時。當您遲到時調用CreateFixture函數,指針現在將指向的堆棧區域函數。

要解決這個問題,你可以在堆上使用例如new

+0

所以我嘗試使用新的關鍵字,它給了我同樣的問題。但從你的回答來看,我所做的並不是最佳實踐。將燈具對象作爲參數傳遞,讓數據通過被調用方法填充可能會更好?而不是返回? – Billybonks

+0

@Billybonks它不接近「最佳實踐」,返回指向局部變量的指針(或引用)_將會遲早地導致不好的事情發生。通過引用作爲參數也是一個可接受的解決方案。 –

+0

@Billybonks在您的新代碼中,您將_still_指針指向局部變量,並帶有'polygon'變量。 –