Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is my Objective-C object being deallocated?
    primarykey
    data
    text
    <p>I have a problem with an Objective-C object (in an iOS game app) that is being mysteriously deallocated.</p> <p>The object is a GameCharacter instance which is instantiated like so:</p> <pre><code>for (int c = 0; c &lt; kNrOfGuards; c++) { GameCharacter* guard = [[GameCharacter alloc] initGuard:self sprite:guardSprite]; [characterArray addObject:guard]; [guard release]; } </code></pre> <p>I also have a convenience method for finding a GameCharacter:</p> <pre><code>- (GameCharacter*)findCharacterWithIndex:(int)index { return [characterArray objectAtIndex:index]; } </code></pre> <p>And the code that generates the error looks like:</p> <pre><code>for (int c = 0; c &lt; [self characterCount]; c++) { GameCharacter* tempCharacter = [self findCharacterWithIndex:c]; if (tempCharacter.playerId == playerIndex]) { ... } } </code></pre> <p>Running this code <em>for some time</em> (never immediately) generates an error in the Console:</p> <p><strong>[GameCharacter playerId]: message sent to deallocated instance 0x4e47560</strong></p> <p>With the <a href="http://www.codza.com/how-to-debug-exc_bad_access-on-iphone" rel="nofollow">NSZombieEnabled trick</a> I've managed to track down the object(s) that is causing the problem, but I still can't understand <em>why</em> this object is being deallocated. Searching my code for "release"/"dealloc" does not produce any clues.</p> <p>I've tried removing the "release" (and even adding a "retain"!) to the alloc/init loop (see top), it seems to extend the time the app can run but not remove the problem entirely.</p> <p>Any hints would be much appreciated!</p> <p><strong>EDIT</strong></p> <p>Thanks to quixoto, Olie, Eiko, tc., I've figured out that it is my GameCharacter object that is being deallocated, but I still don't understand quite why. Here is the trace log in reverse chronological order:</p> <pre><code>#0 -[GameCharacter dealloc] #1 objc_setProperty #2 -[TiledGroundLayer setSelectedCharacter:] #3 -[TiledGroundLayer selectNextCharacterForPlayer:searchStep:] #4 -[GameScene selectNextCharacter:] #5 -[GameScene endTurn] #6 -[HUDLayer onClickDone:] </code></pre> <p>What happens here, is that the user clicks "Done", the selected character on screen is changed and thus the property <code>selectedCharacter</code> on TiledGroundLayer (step #2-4). Since <code>selectedCharacter</code> owns the previous GameCharacter object, it seems it is being deallocated. But why is it not being retained properly by the NSMutableArray (<code>[characterArray addObject:guard];</code>)?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload