[C++] Wyciek Pamięci
Ostatnio zmodyfikowano 2024-09-24 19:52
tBane Temat założony przez niniejszego użytkownika |
[C++] Wyciek Pamięci » 2024-09-24 18:09:35 Witam. W moim programie leci pamięć. Z tego co rozumiem to usuwanie starych prefabów powinno załatwić sprawę, ale jednak nie działa. Gdzie robię błąd ? for( auto & prefab: prefabsToPaint ) { delete prefab; }
GameObject * prefabToPaint; std::vector < GameObject * > prefabsToPaint; TerrainPrefab * ter = nullptr; FloorPrefab * flo = nullptr;
void generateBrush() { int baseX = int( worldMousePosition.x ) / int( tileSide ) * int( tileSide ); int baseY = int( worldMousePosition.y ) / int( tileSide ) * int( tileSide ); int brushHalfWdt = brush.size() / 2; int brushHalfHgh = brush.size() / 2; for( int y = 0; y < brush.size(); y++ ) { for( int x = 0; x < brush[ y ].size(); x++ ) { if( brush[ y ][ x ] == true ) { int xx = baseX +( x - brushHalfWdt ) * tileSide; int yy = baseY +( y - brushHalfHgh ) * tileSide; if( prefabToPaint->type == gameObjectType::Terrain ) { ter = new TerrainPrefab( prefabToPaint->name, dynamic_cast < TerrainPrefab * >( prefabToPaint )->ttype ); ter->position = sf::Vector2f( xx + 8, yy + 8 ); ter->collider->shape->setFillColor( sf::Color( 129, 48, 48, 128 ) ); ter->collider->shape->setPosition( sf::Vector2f( xx, yy ) ); prefabsToPaint.push_back( ter ); } if( prefabToPaint->type == gameObjectType::Floor ) { flo = new FloorPrefab( prefabToPaint->name, dynamic_cast < FloorPrefab * >( prefabToPaint )->ftype ); flo->position = sf::Vector2f( xx + 8, yy + 8 ); flo->collider->shape->setFillColor( sf::Color( 129, 48, 48, 128 ) ); flo->collider->shape->setPosition( sf::Vector2f( xx, yy ) ); prefabsToPaint.push_back( flo ); } } } } }
void painterUpdate() { for( auto & prefab: prefabsToPaint ) { delete prefab; } prefabsToPaint.clear(); if( GUIwasHover || GUIwasClicked ) { return; } else if( tool == toolType::Cursor && selection_state == true ) { mouseSelection(); } else if( prefabToPaint != nullptr ) { if( prefabToPaint->type == gameObjectType::Terrain || prefabToPaint->type == gameObjectType::Floor ) { if( tool == toolType::Brush ) { generateBrush(); } } } I wszędzie zdefiniowałem destruktory: ~Collider() { delete shape; }
GameObject( string name ) { this->name = name; position.x = 0; position.y = 0; collisioning = true; mouseIsOver = false; collider = new Collider( 16, 16, 16, true ); collider->shape = new sf::RectangleShape( sf::Vector2f( 16, 16 ) ); createTextname(); toDelete = false; isVisible = false; }
~GameObject() { delete collider; }
|
|
tBane Temat założony przez niniejszego użytkownika |
» 2024-09-24 19:20:59 Błąd miałem jednak w konstruktorze GameObjectu : GameObject( string name ) { this->name = name; position.x = 0; position.y = 0; collisioning = true; mouseIsOver = false; collider = new Collider( 16, 16, 16, true ); collider->shape = new sf::RectangleShape( sf::Vector2f( 16, 16 ) ); createTextname(); toDelete = false; isVisible = false; }
Poprawiłem kod i już nie ma wycieku pamięci: GameObject( string name ) { this->name = name; position.x = 0; position.y = 0; collisioning = true; mouseIsOver = false; collider = new Collider( 16, 16, 16, true ); dynamic_cast < sf::RectangleShape * >( collider->shape )->setOrigin( sf::Vector2f( 0, 0 ) ); dynamic_cast < sf::RectangleShape * >( collider->shape )->setSize( sf::Vector2f( 16, 16 ) ); dynamic_cast < sf::RectangleShape * >( collider->shape )->setOutlineThickness( 0 ); createTextname(); toDelete = false; isVisible = false; }
|
|
« 1 » |