setPosition and addChild crashes

I have a class which contains a button. When the button is pressed, it is supposed to create a sprite but crashes on lines setPosition and addChild.
Here is the code

auto shoot = Button::create("shoot.png");
shoot->addTouchEventListener([=](Ref* sender, Widget::TouchEventType type){
	switch (type) {
		case Widget::TouchEventType::ENDED:
			if (ammo == 1 && alive) {
				auto bulletPhysics = PhysicsBody::createCircle(5, PhysicsMaterial(0, 1, 0));						bullet = Sprite::create("bullet.png"); 
				bullet->setPosition(player->getPosition()+velocity*20); //this line crashes
				bullet->setPhysicsBody(bulletPhysics);
				bullet->getPhysicsBody()->setVelocity(velocity*BULLETSPEED);
				bullet->getPhysicsBody()->setCollisionBitmask(playerNumber*11);
				bullet->getPhysicsBody()->setContactTestBitmask(true);
				mapBackground->addChild(bullet); //this line crashes
				ammo = 0;
			}
			break;
		default:
			break;
	}
});

Any help would be greatly appreciated.

Is mapbackground out of scope?

mapBackground is declared in the private part of the class but I don’t think that’s the problem as the setPosition line crashes even if I don’t addChild.

Any crash log?

it seems that player is local object.
Try this:
shoot->addTouchEventListener([&, player, mapBackground](Ref* sender, Widget::TouchEventType type){

player and mapBackground are global variables so I don’t think that’s the problem.

--------- beginning of /dev/log/main
F/libc    ( 2518): Fatal signal 11 (SIGSEGV) at 0x8043f941 (code=1), thread 2533 (Thread-218)
I/DEBUG   (   83): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
I/DEBUG   (   83): Build fingerprint: 'hp/t7h/pine:4.1.1/JRO03H/v1.05.18_user:user/release-keys'
I/DEBUG   (   83): pid: 2518, tid: 2533, name: Thread-218  >>> org.cocos2dx.Bulletoon <<<
I/DEBUG   (   83): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 8043f941
I/DEBUG   (   83):     r0 694280b0  r1 00000000  r2 ffffffff  r3 8043f941
I/DEBUG   (   83):     r4 69119b1c  r5 68a414fc  r6 634f9820  r7 69428030
I/DEBUG   (   83):     r8 4422acf7  r9 61ea0f00  sl 62fb9a88  fp 69428174
I/DEBUG   (   83):     ip 69428038  sp 69428030  lr 68914e21  pc 68914e3c  cpsr 80000030
I/DEBUG   (   83):     d0  7b200000350c0002  d1  f8fe0000f2f80000
I/DEBUG   (   83):     d2  ffff0000ffff0000  d3  f2f80000f8fe0000
I/DEBUG   (   83):     d4  3e789e2600000000  d5  3f2aad8340400000
I/DEBUG   (   83):     d6  43f400003e778250  d7  0000000000000000
I/DEBUG   (   83):     d8  416fca0580000000  d9  416fca0555555555
I/DEBUG   (   83):     d10 0000000000000000  d11 0000000000000000
I/DEBUG   (   83):     d12 0000000000000000  d13 0000000000000000
I/DEBUG   (   83):     d14 0000000000000000  d15 0000000000000000
I/DEBUG   (   83):     d16 410adaf000000000  d17 7e37e43c8800759c
I/DEBUG   (   83):     d18 00000000a8000000  d19 3fb33dc61a93ea8e
I/DEBUG   (   83):     d20 3f76cff5716b41da  d21 3fd7ac77a49a45b8
I/DEBUG   (   83):     d22 8000000000000000  d23 0000000000000000
I/DEBUG   (   83):     d24 0000000000000000  d25 8000000000000000
I/DEBUG   (   83):     d26 3ff0000000000000  d27 0000000000000000
I/DEBUG   (   83):     d28 0000000000000005  d29 0000000000000000
I/DEBUG   (   83):     d30 0000000000000000  d31 0000000000000000
I/DEBUG   (   83):     scr 20000013
I/DEBUG   (   83):
I/DEBUG   (   83): backtrace:
I/DEBUG   (   83):     #00  pc 0022ee3c  /data/data/org.cocos2dx.Bulletoon/lib/libMyGame.so (_ZZN6PlayerC4EiiN7cocos2d4Vec2EPNS0_11TMXTiledMapES1_S1_S1_S1_S1_ENKUlPNS0_3RefENS0_2ui6Widget14TouchEventTypeEE_clES5_S8_+483)
I/DEBUG   (   83):     #01  pc 0022ee1d  /data/data/org.cocos2dx.Bulletoon/lib/libMyGame.so (_ZZN6PlayerC4EiiN7cocos2d4Vec2EPNS0_11TMXTiledMapES1_S1_S1_S1_S1_ENKUlPNS0_3RefENS0_2ui6Widget14TouchEventTypeEE_clES5_S8_+452)
I/DEBUG   (   83):
I/DEBUG   (   83): stack:
I/DEBUG   (   83):          69427ff0  694280c8
I/DEBUG   (   83):          69427ff4  694280c8
I/DEBUG   (   83):          69427ff8  694280c8
I/DEBUG   (   83):          69427ffc  694280c8
I/DEBUG   (   83):          69428000  694280c8
I/DEBUG   (   83):          69428004  694280c8
I/DEBUG   (   83):          69428008  694280c8
I/DEBUG   (   83):          6942800c  694280c8
I/DEBUG   (   83):          69428010  6342e228
I/DEBUG   (   83):          69428014  00000020
I/DEBUG   (   83):          69428018  694280c8
I/DEBUG   (   83):          6942801c  6342e228
I/DEBUG   (   83):          69428020  00000020
I/DEBUG   (   83):          69428024  6342e228
I/DEBUG   (   83):          69428028  df0027ad
I/DEBUG   (   83):          6942802c  00000000
I/DEBUG   (   83):     #00  69428030  694280cc
I/DEBUG   (   83):          ........  ........
I/DEBUG   (   83):     #01  69428030  694280cc
I/DEBUG   (   83):          69428034  694280cc
I/DEBUG   (   83):          69428038  694280cc
I/DEBUG   (   83):          6942803c  00000002
I/DEBUG   (   83):          69428040  630aad60
I/DEBUG   (   83):          69428044  630ab05c
I/DEBUG   (   83):          69428048  634fa510
I/DEBUG   (   83):          6942804c  69017ab8  /data/data/org.cocos2dx.Bulletoon/lib/libMyGame.so
I/DEBUG   (   83):          69428050  694280b0
I/DEBUG   (   83):          69428054  694280bc
I/DEBUG   (   83):          69428058  694280b0
I/DEBUG   (   83):          6942805c  694280b0
I/DEBUG   (   83):          69428060  694280b0
I/DEBUG   (   83):          69428064  694280b0
I/DEBUG   (   83):          69428068  694280b0
I/DEBUG   (   83):          6942806c  694280b0
I/DEBUG   (   83):
I/DEBUG   (   83): memory near r0:
I/DEBUG   (   83):     69428090 694280bc 694280bc 00000003 69428080  ..Bi..Bi......Bi
I/DEBUG   (   83):     694280a0 69428074 00000000 3f800000 00000000  t.Bi.......?....
I/DEBUG   (   83):     694280b0 00000000 00000000 00000000 00000000  ................
I/DEBUG   (   83):     694280c0 00000000 00000000 00000021 0000000b  ........!.......
I/DEBUG   (   83):     694280d0 6342e228 3b80964b 4422acf7 69119b1c  (.BcK..;.."D...i
I/DEBUG   (   83):
I/DEBUG   (   83): memory near r3:
I/DEBUG   (   83):     8043f920 ffffffff ffffffff ffffffff ffffffff  ................
I/DEBUG   (   83):     8043f930 ffffffff ffffffff ffffffff ffffffff  ................
I/DEBUG   (   83):     8043f940 ffffffff ffffffff ffffffff ffffffff  ................
I/DEBUG   (   83):     8043f950 ffffffff ffffffff ffffffff ffffffff  ................
I/DEBUG   (   83):     8043f960 ffffffff ffffffff ffffffff ffffffff  ................
I/DEBUG   (   83):
I/DEBUG   (   83): memory near r4:
I/DEBUG   (   83):     69119afc 69117ad8 69117acc 691180e0 691180f4  .z.i.z.i...i...i
I/DEBUG   (   83):     69119b0c 69117a74 6900975f 6900974d 690096c9  tz.i_..iM..i...i
I/DEBUG   (   83):     69119b1c 00000000 00000000 00000000 40120e35  ............5..@
I/DEBUG   (   83):     69119b2c 40120f4d 400a82f5 4013b8e3 4012c5fd  M..@...@...@...@
I/DEBUG   (   83):     69119b3c 40118e9c 4010e711 4010e4a0 40118fcc  ...@...@...@...@
I/DEBUG   (   83):
I/DEBUG   (   83): memory near r5:
I/DEBUG   (   83):     68a414dc 0a000000 ebfb02ed e24bd008 e8bd8810  ..........K.....
I/DEBUG   (   83):     68a414ec 006d8794 ffffe7ac ffffe8b0 ffffecf8  ..m.............
I/DEBUG   (   83):     68a414fc e92d4800 e28db004 e24dd008 e50b0008  .H-.......M.....
I/DEBUG   (   83):     68a4150c e50b100c e51b3008 e1a00003 e51b100c  .....0..........
I/DEBUG   (   83):     68a4151c ebfec2f1 e51b3008 e5d3323d e2233001  .....0..=2...0#.
I/DEBUG   (   83):
I/DEBUG   (   83): memory near r6:
I/DEBUG   (   83):     634f9800 00000000 00000000 00000000 00000000  ................
I/DEBUG   (   83):     634f9810 00000000 00000000 00000000 00000413  ................
I/DEBUG   (   83):     634f9820 69108d60 00000001 000002d9 00000000  `..i............
I/DEBUG   (   83):     634f9830 00000000 00000000 00000000 00000000  ................
I/DEBUG   (   83):     634f9840 00000000 00000000 00000000 00000000  ................
I/DEBUG   (   83):
I/DEBUG   (   83): memory near r7:
I/DEBUG   (   83):     69428010 6342e228 00000020 694280c8 6342e228  (.Bc .....Bi(.Bc
I/DEBUG   (   83):     69428020 00000020 6342e228 df0027ad 00000000   ...(.Bc.'......
I/DEBUG   (   83):     69428030 694280cc 694280cc 694280cc 00000002  ..Bi..Bi..Bi....
I/DEBUG   (   83):     69428040 630aad60 630ab05c 634fa510 69017ab8  `..c\..c..Oc.z.i
I/DEBUG   (   83):     69428050 694280b0 694280bc 694280b0 694280b0  ..Bi..Bi..Bi..Bi
I/DEBUG   (   83):
I/DEBUG   (   83): memory near r8:
I/DEBUG   (   83):     4422acd4 00000000 00000000 00000000 00000000  ................
I/DEBUG   (   83):     4422ace4 00000000 00000000 00000000 00000000  ................
I/DEBUG   (   83):     4422acf4 00000000 00000000 00000000 00000000  ................
I/DEBUG   (   83):     4422ad04 00000000 00000000 00000000 00000000  ................
I/DEBUG   (   83):     4422ad14 00000000 00000000 00000000 00000000  ................
I/DEBUG   (   83):
I/DEBUG   (   83): memory near r9:
I/DEBUG   (   83):     61ea0ee0 00000000 00000000 6318b40c 61ea0f20  ...........c ..a
I/DEBUG   (   83):     61ea0ef0 685e3850 6216c128 00000000 00000000  P8^h(..b........
I/DEBUG   (   83):     61ea0f00 00000000 4404478f 44494dad 61ea0f44  .....G.D.MIDD..a
I/DEBUG   (   83):     61ea0f10 685dfae0 6216c2b8 685e3850 00000000  ..]h...bP8^h....
I/DEBUG   (   83):     61ea0f20 412ef478 00000000 4404478f 44494dad  x..A.....G.D.MID
I/DEBUG   (   83):
I/DEBUG   (   83): memory near sl:
I/DEBUG   (   83):     62fb9a68 00000004 00000000 6342ad08 00000453  ..........BcS...
I/DEBUG   (   83):     62fb9a78 631e313a 61ea0f00 6216c2b8 61e7a000  :1.c...a...b...a
I/DEBUG   (   83):     62fb9a88 412ef478 00000000 69428da8 00000000  x..A......Bi....
I/DEBUG   (   83):     62fb9a98 69428ddc 0000000d 00000000 40770fc0  ..Bi..........w@
I/DEBUG   (   83):     62fb9aa8 00000000 00000000 611bdb70 61e9d300  ........p..a...a
I/DEBUG   (   83):
I/DEBUG   (   83): memory near fp:
I/DEBUG   (   83):     69428154 68944034 bd4b8c87 00000002 630aad60  4@.h..K.....`..c
I/DEBUG   (   83):     69428164 630ab058 69119b1c 00000000 6942818c  X..c...i......Bi
I/DEBUG   (   83):     69428174 68940c40 6942818c 630aad60 630ab058  @..h..Bi`..cX..c
I/DEBUG   (   83):     69428184 630aad60 694281bc 689408c8 00000040  `..c..Bi...h@...
I/DEBUG   (   83):     69428194 69428bc8 63039808 630aad60 015530e0  ..Bi...c`..c.0U.
I/DEBUG   (   83):
I/DEBUG   (   83): memory near ip:
I/DEBUG   (   83):     69428018 694280c8 6342e228 00000020 6342e228  ..Bi(.Bc ...(.Bc
I/DEBUG   (   83):     69428028 df0027ad 00000000 694280cc 694280cc  .'........Bi..Bi
I/DEBUG   (   83):     69428038 694280cc 00000002 630aad60 630ab05c  ..Bi....`..c\..c
I/DEBUG   (   83):     69428048 634fa510 69017ab8 694280b0 694280bc  ..Oc.z.i..Bi..Bi
I/DEBUG   (   83):     69428058 694280b0 694280b0 694280b0 694280b0  ..Bi..Bi..Bi..Bi
I/DEBUG   (   83):
I/DEBUG   (   83): memory near sp:
I/DEBUG   (   83):     69428010 6342e228 00000020 694280c8 6342e228  (.Bc .....Bi(.Bc
I/DEBUG   (   83):     69428020 00000020 6342e228 df0027ad 00000000   ...(.Bc.'......
I/DEBUG   (   83):     69428030 694280cc 694280cc 694280cc 00000002  ..Bi..Bi..Bi....
I/DEBUG   (   83):     69428040 630aad60 630ab05c 634fa510 69017ab8  `..c\..c..Oc.z.i
I/DEBUG   (   83):     69428050 694280b0 694280bc 694280b0 694280b0  ..Bi..Bi..Bi..Bi
I/DEBUG   (   83):
I/DEBUG   (   83): code around pc:
I/DEBUG   (   83):     68914e1c f946f7f2 681b697b 681b689b 681d334c  ..F.{i.h.h.hL3.h
I/DEBUG   (   83):     68914e2c 681b697b 697b689e 681b681b 3358681b  {i.h.h{i.h.h.hX3
I/DEBUG   (   83):     68914e3c 697b681a 681b681b 47901c18 697b60b8  .h{i.h.h...G.`{i
I/DEBUG   (   83):     68914e4c 3328681b 21082264 44bc468c 49764462  .h(3d".!.F.DbDvI
I/DEBUG   (   83):     68914e5c 1c106079 687a1c19 fdeef7ff 2208236c  y`....zh....l#."
I/DEBUG   (   83):
I/DEBUG   (   83): code around lr:
I/DEBUG   (   83):     68914e00 22082384 44bc4694 1c184463 f94ef7f2  .#.".F.DcD....N.
I/DEBUG   (   83):     68914e10 22082378 44bc4694 1c184463 f946f7f2  x#.".F.DcD....F.
I/DEBUG   (   83):     68914e20 681b697b 681b689b 681d334c 681b697b  {i.h.h.hL3.h{i.h
I/DEBUG   (   83):     68914e30 697b689e 681b681b 3358681b 697b681a  .h{i.h.h.hX3.h{i
I/DEBUG   (   83):     68914e40 681b681b 47901c18 697b60b8 3328681b  .h.h...G.`{i.h(3
I/DEBUG   (   83):
I/DEBUG   (   83): memory map around fault addr 8043f941:
I/DEBUG   (   83):     6b166000-6b5a6000 /dev/mali
I/DEBUG   (   83):     (no map for address)
I/DEBUG   (   83):     befde000-befff000 [stack]

mapBackground is likely pointing to invalid memory.

Where is mapBackground initialised? How is it added to the scene/retained?

It’s a really bad idea to store scene object references as static variables as they can destruct when their parent does making the pointer invalid.

mapBackground is declared in the private part of my class but I’ve also tried declaring it in the public part as well. Both times it was initialised in the constructor of my class.

Please post the header and source file where mapBackground is declared and initialised

1 Like

Sorry, I’ve been really busy these last few weeks.
Here’s the file

class Player {
	Sprite* player;
	Sprite* stick;
	Sprite* bullet;

	int playerNumber;
	int lives;
	int ammo;
	float angle;
	Vec2 velocity;
	
	PhysicsBody* playerPhysics;		
public:
	Sprite* joy;
	bool alive;
	int touch;
	
	TMXTiledMap* mapBackground;

	Player();

	Player(int a, int b, Vec2 c, TMXTiledMap* d, Vec2 e, Vec2 f, Vec2 g, Vec2 h, Vec2 i) {
		playerNumber = a;
		lives = b;
		ammo = 1;
		angle = 0;
		velocity = c;
		alive = true;
		
		mapBackground = d;
		playerPhysics = PhysicsBody::createBox(Size(20, 20), PhysicsMaterial(0, 0, 0));
		
		player = Sprite::create("player"+to_string(playerNumber)+".png");
		player->setPosition(e);
		player->addComponent(playerPhysics);
		player->getPhysicsBody()->setCollisionBitmask(playerNumber);
		player->getPhysicsBody()->setContactTestBitmask(true);
		mapBackground->addChild(player);
		
		joy = Sprite::create("joy"+to_string(playerNumber)+".png");
		joy->setAnchorPoint(f);
		joy->setPosition(g);
		mapBackground->getParent()->addChild(joy);
		
		stick = Sprite::create("stick.png");
		stick->setPosition(joy->getContentSize()/2);
		joy->addChild(stick);
		
		auto shoot = Button::create("shoot"+to_string(playerNumber)+".png");
		shoot->setAnchorPoint(h);
		shoot->setPosition(i);
		shoot->addTouchEventListener([=](Ref* sender, Widget::TouchEventType type){
			switch (type) {
				case Widget::TouchEventType::ENDED:
					if (ammo == 1 && alive) {
						auto bulletPhysics = PhysicsBody::createCircle(5, PhysicsMaterial(0, 1, 0));
						bullet = Sprite::create("bullet"+to_string(playerNumber)+".png");
						bullet->setPosition(player->getPosition()+velocity*20); //doesn't work
						bullet->setPosition(Vec2(20, 20));
						bullet->setPhysicsBody(bulletPhysics);
						bullet->getPhysicsBody()->setVelocity(velocity*BULLETSPEED);
						bullet->getPhysicsBody()->setCollisionBitmask(playerNumber*11);
						bullet->getPhysicsBody()->setContactTestBitmask(true);
						mapBackground->addChild(bullet); //doesn't work
						ammo = 0;
					}
					break;
				default:
					break;
			}
		});
		mapBackground->getParent()->addChild(shoot);
	};

I have managed to make my game not crash anymore. However, the code still doesn’t work correctly.

In my .cpp file, I create four instances of my class Player. However, in the shoot lambda, it only registers as the last player, i.e four players is player 4, two players is player 2 etc.

Here are my two files:

.cpp

if (oneText == "-") {
	playersNumber += 1;
	Player one (playersNumber, lives, Vec2(0, 1), mapBackground, Vec2(50, 50), Vec2(1, 0), Vec2(visibleSize.width*2/9, visibleSize.width/9), Vec2(0, 0), Vec2(visibleSize.width*2/9, visibleSize.width/20));
}
if (twoText == "-") {
	playersNumber += 1;
	Player two (playersNumber, lives, Vec2(1, 0), mapBackground, Vec2(50, mapBackground->getContentSize().height-50), Vec2(0, 0), Vec2(visibleSize.width/9, visibleSize.height-visibleSize.width*2/9), Vec2(0, 1), Vec2(visibleSize.width/20, visibleSize.height-visibleSize.width*2/9));
}
if (threeText == "-") {
	playersNumber += 1;
	Player three (playersNumber, lives, Vec2(0, -1), mapBackground, Vec2(mapBackground->getContentSize().width-50, mapBackground->getContentSize().height-50), Vec2(0, 1), Vec2(visibleSize.width*7/9, visibleSize.height-visibleSize.width/9), Vec2(1, 1), Vec2(visibleSize.width*7/9, visibleSize.height-visibleSize.width/20));
}
if (fourText == "-") {
	playersNumber += 1;
	Player four (playersNumber, lives, Vec2(-1, 0), mapBackground, Vec2(mapBackground->getContentSize().width-50, 50), Vec2(1, 1), Vec2(visibleSize.width*8/9, visibleSize.width*2/9), Vec2(1, 0), Vec2(visibleSize.width*19/20, visibleSize.width*2/9));
}

.h

class Player {
	Sprite* stick;
	Sprite* bullet;

	int playerNumber;
	int lives;
	int ammo;
	float angle;
	Vec2 velocity;
	
	PhysicsBody* playerPhysics;
public:
	Sprite* joy;
	bool alive;
	int touch;
	
	Sprite* player;
	TMXTiledMap* mapBackground;

	Player();

	Player(int a, int b, Vec2 c, TMXTiledMap* d, Vec2 e, Vec2 f, Vec2 g, Vec2 h, Vec2 i) {
		playerNumber = a;
		lives = b;
		ammo = 1;
		angle = 0;
		velocity = c;
		alive = true;
		
		mapBackground = d;
		playerPhysics = PhysicsBody::createBox(Size(20, 20), PhysicsMaterial(0, 0, 0));
		
		player = Sprite::create("player"+to_string(playerNumber)+".png");
		player->setPosition(e);
		player->addComponent(playerPhysics);
		player->getPhysicsBody()->setCollisionBitmask(playerNumber);
		player->getPhysicsBody()->setContactTestBitmask(true);
		mapBackground->addChild(player);
		
		joy = Sprite::create("joy"+to_string(playerNumber)+".png");
		joy->setAnchorPoint(f);
		joy->setPosition(g);
		mapBackground->getParent()->addChild(joy);
		
		stick = Sprite::create("stick.png");
		stick->setPosition(joy->getContentSize()/2);
		joy->addChild(stick);
		
		bullet = Sprite::create("bullet"+to_string(playerNumber)+".png");
		CCLOG("%f, %f", player->getPosition().x, player->getPosition().y); //logs position of correct player
		auto shoot = Button::create("shoot"+to_string(playerNumber)+".png");
		shoot->setAnchorPoint(h);
		shoot->setPosition(i);
		shoot->addTouchEventListener([&](Ref* sender, Widget::TouchEventType type){
			switch (type) {
				case Widget::TouchEventType::ENDED:
					if (ammo == 1 && alive) {
						auto bulletPhysics = PhysicsBody::createCircle(5, PhysicsMaterial(0, 1, 0));
						bullet = Sprite::create("bullet"+to_string(playerNumber)+".png");
						CCLOG("%f, %f", player->getPosition().x, player->getPosition().y); //logs position of last player
						CCLOG("%i", playerNumber); //logs number of last player
						bullet->setPosition(player->getPosition()+velocity*20);
						bullet->setPhysicsBody(bulletPhysics);
						bullet->getPhysicsBody()->setVelocity(velocity*BULLETSPEED);
						bullet->getPhysicsBody()->setCollisionBitmask(playerNumber*11);
						bullet->getPhysicsBody()->setContactTestBitmask(true);
						mapBackground->addChild(bullet);
						ammo = 0;
					}
					break;
				default:
					break;
			}
		});
		mapBackground->getParent()->addChild(shoot);
	};