The game crashes after “third” is logged to the console and after a bit of tinkering, I found out that accessing the variable “player” is what crashes the game. Also, if I commented out that line the game would also crash after “eight” when accessing “mapBackground”.
Both variable “player” and “mapBackground” are declared in the public part of my header file so I have no idea why this is crashing. Also, accessing the other variables does not cause the game to crash.
@sheshmoshen I need to capture by reference so copying is not an option. @sevent7 I tested player and mapBackground and they are not NULL. @amin13a I tried changing [&] to [this] but it still crashed in the same place as before.
Do you suppose we will solve your memory read access violation errors without you even linking the code to the variables (ammo, alive, playerNumber, mapBackground) that are captured by reference in the lambda?
A hint: forget capture by reference, especially capturing all variables by reference ( [&] ). If you need similar behavior use a variable on a more global scope, or maybe something like this:
#include <memory>
#include <string>
auto alive = std::make_shared<bool>(false);
auto lambda = [alive](){ *alive = !*alive };
lambda();
cocos2d::log((std::string("First, it was alive, now it is ") + (*alive ? "still alive" : "dead") ).c_str());
It should output it’s dead; the alive boolean is preserved at least until the lambda is not destroyed.
Global variables to lambda???.. You can only capture function-level variables to lambdas. Retaining here is irrelevant here as well. Most likely only mapBackground is derived from Ref and that is internally retained by the addChild method.
@MatrixAndrew you did couple of errors in your code.
At first compile error - missing ; in lambda body.
Second - initial value of alive should be true.
Full working code:
#include <memory>
#include <string>
auto alive = std::make_shared<bool>(true);
auto lambda = [alive](){ *alive = !*alive; };
lambda();
cocos2d::log((std::string("First, it was alive, now it is ") + (*alive ? "still alive" : "dead")).c_str());
@amin13a That wouldn’t work as player is a child of mapBackground and accessing mapBackground also crashes.
@MatrixAndrew The variables “ammo” and “alive” are working fine, only “player” and “mapBackground” are causing the game to crash.
@sheshmoshen My variables are created globally. They are under the public part of my class. Could you elaborate a bit more on retaining all Ref derived objects as I’m pretty new to C++.
C++ as a language forces the programmer to manage memory.
Therefore when you create a cocos2d-x object that is allocated by calling
the new keyword that will go on the heap which is basically objects that are in a
certain region in memory that obligates me to manage it.
Any Ref derived object (Node, Sprite, Action and many others) need to be retained so that the local cocos2d-x memory manager won’t delete that object rather it will retain it until I release it.
If I add my Ref derived object to another Ref retained object than I don’t have to call retain on my own.
But if I don’t want my Ref derived object to be added in init() I need to specifically retain it to tell cocos2d-x “HEY DON’T DELETE MY OBJECT BECAUSE I WILL USE IT LATER!”