ios - Bad Access using NSMutableArray with ARC -
at moment creating game cocos2d v3.1 using box2d, have use .mm extensions source files indicate using c++. game contains „endless terrain“ consists of terrain-key-points. created class saves key points named terrainelement. @ beginning of game create nsmutablearray called _elements array , fill random terrainelements.
within game loop check iterate on elements, , draw elements visible. app crashes after rather random amount of time.
header
@interface terrainbase : ccnode{ nsmutablearray * _elements; }
part of game loop:
while (((terrainelement*)[_elements objectatindex:tokeypoint]).startpoint.x < _currentx+winsize.width*9/8) { //crashes here exc_bad_access, _elements array null terrainelement *element = (terrainelement*)[_elements objectatindex:tokeypoint]; [element drawelement]; tokeypoint++; }
i not able figure out why nsmutablearray null. iterate on elements in each loop, , not touch them basically. made property out of _elements array(nonatomic,strong), did not either. decided use normal array instead of nsmutablearray. did not change code @ all, except indexing of array:
@interface terrainbase : ccnode{ terrainelement *_elements[4000]; }
gameloop:
while ( _elements[tokeypoint].startpoint.x< _currentx+winsize.width*9/8) { [_elements[tokeypoint] drawelement]; tokeypoint++; }
this time works, , array never null. mystery me why second solution works first keeps crashing. prefer use nsmutablearray gives me more flexibility. can help?
you allowed send messages nil
(null
) not reason crash on own (you nil
in case).
i'm not sure have given enough information determine actual cause. please post stacktrace , exceptions. posted code contain happening in loop?
your loop condition looks flawed logic me in couple of ways:
- unless array sorted
startpoint.x
stop drawing terrain encounter first element >=_currentx+winsize.width*9/8
- as stated above, can send messages
nil
,nil
(ornull
or0
or whatever want call it) value returned. means have infinite loop whenever_elements
nil
(because[_elements objectatindex:tokeypoint]).startpoint.x
evaluate0
<_currentx+winsize.width*9/8
presumably.
i need more information further.
Comments
Post a Comment