Konstruktorcode wird ignoriert
-
Hi, ich habe 2 Objekte von 2 verschiedenen Klassen die eine wird im Konstruktor der anderen erstellt.
das ganze sieht so aus:Map::Map(int mapWidth, int mapHeight, int tileWidth, int tileHeight, Tileset* tileset, bf::path projectPath) : mapWidth(mapWidth), mapHeight(mapHeight), tileWidth(tileWidth), tileHeight(tileHeight), tileset(NULL) { tileset = new shared::Tileset(bf::path("C:/Main/Tools/RMXP/RTP/Graphics/Tilesets/001-Grassland01.png"), projectPath); }Im source, der Header sieht so aus:
class Map { public: Map(int mapWidth, int mapHeight, int tileWidth, int tileHeight, Tileset* tileset, bf::path); ~Map(); //Accessors void SetMapSize(int mapWidth, int mapHeight){this->mapWidth = mapWidth, this->mapHeight = mapHeight;} void GetMapSize(int& mapWidth, int& mapHeight){mapWidth = this->mapWidth, mapHeight = this->mapHeight;} int GetMapWidth() const {return mapWidth;} int GetMapHeight() const {return mapHeight;} int GetMapWidthInPx()const {return mapWidth * tileWidth;} int GetMapHeightInPx() const {return mapHeight * tileHeight;} void SetTileSize(int tileWidth, int tileHeight){this->tileWidth = tileWidth, this->tileHeight = tileHeight;} void GetTileSize(int& tileWidth, int& tileHeight) const {tileWidth = this->tileWidth, tileHeight = this->tileHeight;} int GetTileWidth() const {return tileWidth;} int GetTileHeight() const {return tileHeight;} void SetTileset(Tileset* tileset){this->tileset = tileset;} const Tileset* GetTileset() const {return tileset;} private: int mapWidth, mapHeight; //In tiles int tileWidth, tileHeight; //In pixels Tileset* tileset; };Erstellt wird das Objekt wie folgt:
mp = boost::shared_ptr<shared::Map>(new shared::Map(20, 15, 32, 32, NULL, CONFIGURATION->GetProjectPath()));Wenn ich den Debugger frage zeigt tileset am Ende des Konstruktors korrekt auf das Objekt, aber wenn ich den Debugger direkt nach dem Konstruktor(also nach dem mp = boost::shared_ptr u.s.w.) zeigt tileset auf NULL, alle anderen Dinge sind korrekt, da stellt sich mir doch die Frage...
warum?
-
JustAnotherNoob schrieb:
Map::Map(int mapWidth, int mapHeight, int tileWidth, int tileHeight, Tileset* tileset, bf::path projectPath) : mapWidth(mapWidth), mapHeight(mapHeight), tileWidth(tileWidth), tileHeight(tileHeight), tileset(NULL) { tileset = new shared::Tileset(bf::path("C:/Main/Tools/RMXP/RTP/Graphics/Tilesets/001-Grassland01.png"), projectPath); }tileset ist bei dir sowohl eine member-variable als auch ein argument für den konstruktor. das new bezieht sich auf dieses argument und nicht auf die member-variable.
da tileset keine referenz, sondern eine lokale kopie eines pointers ist und nur innerhalb des konstruktors lebt, kannst du nach dem konstruktoraufruf nicht mehr darauf zugreifen, weil die adresse 'verloren' geht.
-
Wah scheiße du hast Recht, das hab ich gar net gesehen.
Ich bin zwar jetzt wieder beim Fehler davor-.- aber danke soweit...
-
mach this->tileset draus im Ctor-Rumpf, dann sollte es klappen. Allerdings frag ich mich warum du ein tileset pointer als Argument übergibst wenn du es nicht benutzt...
Und wenn wir schon dabei sind: da du eh mit shared_ptr arbeitest, wäre es eventuell sinnvoll, den tileset member auch davon profitieren zu lassen.
-
mach this->tileset draus im Ctor-Rumpf
Darauf bin ich auch noch gekommen.
Allerdings habe ich ein Problem beim löschen eines sf::Images in diesem Tileset (Accessviolation, egal ob Raw-, Smart- oder gar kein Pointer) und versuche verzweifelt die Ursache dafür zu finden