If Player is destroyed then accessing player (which is a member of Player) will cause a segment fault.
If you use Xcode, turning on address sanitizer will help a lot.
Maybe you can set a breakpoint on Player’s destructor. If it breaks there, it has been deleted (thus not retained). If this is the case and you don’t want to delete it, call:
After more testing, I realised it wasn’t the player variable that was causing the crash but rather the bullet variable.
Once I retain the bullet variable, the game no longer crashes but instead, a new problem occurs.
I have four different players but no matter which player’s shoot button I press, it is always player 4 who shoots. I’m pretty sure this has something to do with the fact that it is the last player initialised but I have no idea why.
Any help would be appreciated.
EDIT:
I think what I need is something like this but using C++
I tried using std::unique+ptr but now I am no longer able to access the variables under it when looping. For example, I have the following code:
for (auto &j : players) {
if (j.alive) { //some more stuff
and it gives the error error: 'class std::__ndk1::unique_ptr<Player>' has no member named 'alive'.
EDIT: I solved by replacing j with (*j) like what this link said
It also turns out that this was the whole problem all along. I now no longer need to physically retain any variables anymore. Thank you to everyone who helped me solve this problem