Accessing variable in lambda crashes game

Accessing variable in lambda crashes game
0.0 0

#21

I don’t see problem in Player class.
But better not to use & in lambda, use ‘this’ instead:

shoot->addTouchEventListener([this](Ref* sender, Widget::TouchEventType type){

Please post code how you create and hold player object.
Maybe problem there.


#22

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.


#23

Sorry for the extremely late reply, I’ve been extremely busy these last few weeks.

I create my player objects in my .cpp file add them into a vector.

Player one (playersNumber, Vec2(50, 50), mapBackground, lives, Vec2(0, 1), Vec2(1, 0), Vec2(visibleSize.width*2/9, visibleSize.width/9), Vec2(0, 0), Vec2(visibleSize.width*2/9, visibleSize.width/20));
players.push_back(one);

#24

How would I make sure it’s retained?
Also, is there something like address sanitizer for Windows?


#25

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:

player->retain();

#27

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++


#28

Using push_back will copy the object and the original Player will still be destroyed.

Solution:
Use std::unique_ptr:

std::vector<std::unique_ptr<Player>> players;
...
auto one = std::unique_ptr<Player>(new Player(playersNumber, Vec2(50, 50), mapBackground, lives, Vec2(0, 1), Vec2(1, 0), Vec2(visibleSize.width*2/9, visibleSize.width/9), Vec2(0, 0), Vec2(visibleSize.width*2/9, visibleSize.width/20)));
players.push_back(std::move(one));

Or emplace_back:

players.emplace_back(playersNumber, Vec2(50, 50), mapBackground, lives, Vec2(0, 1), Vec2(1, 0), Vec2(visibleSize.width*2/9, visibleSize.width/9), Vec2(0, 0), Vec2(visibleSize.width*2/9, visibleSize.width/20));

#29

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