← Back to team overview

widelands-dev team mailing list archive

Re: [Merge] lp:~widelands-dev/widelands/bug-1734748-savegame-filename into lp:widelands

 

This is how pointers work in a nutshell:

datatype* foo points to an address in memory. It also takes ownership of that piece of memory.
dataype& foo is the reference (what the pointer refers to), think of it as the actual data in memory.

Let's have a look at the function:

    std::unique_ptr<SavegameData> LoadOrSaveGame::entry_selected()

So, it's not just a pointer, but a unique_ptr. Those get garbage collected, i.e. they will delete themselves when nobody is using them any more.

Now, with

    const SavegameData& gamedata = *load_or_save_.entry_selected();

we don't store the pointer in a variable, so nobody took ownership of the memory, since the unique_ptr goes cheerie bye at the end of the function. So, some other part of the program snapped it up and overwrote it with whatever.

This didn't come up during the original testing of the new feature, because I was using

    SavegameData* LoadOrSaveGame::entry_selected()

That pointer never got deleted, leaking memory, but it did make sure we didn't get garbage filenames.

@bunnybot merge

-- 
https://code.launchpad.net/~widelands-dev/widelands/bug-1734748-savegame-filename/+merge/334429
Your team Widelands Developers is subscribed to branch lp:~widelands-dev/widelands/bug-1734748-savegame-filename.


References