EXC_BAD_ACCESS (code=2, address=0x0) when initializing a CCTMXTiledMap

Hi guys,

I’m trying to create a game with a tile map and I’m getting the below error:

EXC_BAD_ACCESS (code=2, address=0x0) in the line setContentSize(CCSizeZero); of the method CCTMXTiledMap::initWithTMXFile(const char *tmxFile)

I’m creating the tile map in this way:
@
tileMap~~>initWithTMXFile;
this~~>background = tileMap->layerNamed(“Background”);
@
Somebody knows what it’s happening?

Please, could you help me?

Ok, I’ve found the problem. Just a little of theory:

EXC_BAD_ACCESS code=2 means that the pointer is corrupt and this could happen because:

  • The pointer could have never been initialized.
  • The pointer could have been accidentally written over because you overstepped the bounds of an array.
  • The pointer could be part of an object that was casted incorrectly, and then written to.
  • Any of the above could have corrupted a different pointer that now points at or near this pointer, and using that one corrupts this one (and so on).

So, in my case, I thought that initWithTMXFile was good enough to initialize the tileMap pointer but I was wrong. The solution is that first thing before initWithTMXFile, tileMap must be initialized. So, the correct block of code is:

this->tileMap = new CCTMXTiledMap();
this->tileMap->initWithTMXFile("TileMap.tmx");
this->background = tileMap->layerNamed("Background");
this->addChild(tileMap);

I hope it helps you.

dont you see anything else in the console log? maybe something like the file could not be found?

Hi Jesus,

Thanks for answering but luckily I found the fix. The console log shown no error nor exceptions so that’s why I was lost. I double checked that the file exists but it was not the problem.

Thank you anyway :slight_smile: