0

My NSMutableArray object disappears in the middle of a function. Game.h:

@interface Game : NSObject
{
    NSMutableArray* boardPositions;
}

Game.m:

boardPositions=[[NSMutableArray alloc]initWithObjects:[[NSMutableString alloc]initWithString:@" "], nil];

[Players playTurnWithPlayerTurn:playerTurn andBoardPositions:boardPositions];

Players.m:

+(void) playTurnWithPlayerTurn:(BOOL)playerTurn andBoardPositions:(NSMutableArray*)boardPositions
{
    //Printing "X turn" or "O turn"
    if (playerTurn) {
        NSLog(@"O turn");
    }
    else
        NSLog(@"X turn");

    NSLog(@"Where do you want to insert the %c?",playerTurn?'O':'X');//boardPositions disappears
    char input[3];
    gets(input);
    NSString *inputString=[NSString stringWithUTF8String:input];

    //Checking user's input and implementing his choice to the board
    if ([inputString isEqualToString:@"0,0"]) {
        [boardPositions replaceObjectAtIndex:0 withObject:playerTurn?@"O":@"X"];
    }

Does somebody know how to solve this?

It disappears from here and I can't access it:
Before:
https://i.stack.imgur.com/s6BEa.jpg
After:
https://i.stack.imgur.com/LqPu2.jpg

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Yahllilevy
  • 39
  • 1
  • 5
  • 1
    What do you mean it "disappears in the middle of a function"? Provide specific details about the problem. – rmaddy Aug 18 '15 at 17:01
  • Please look at the links I've added – Yahllilevy Aug 18 '15 at 17:08
  • 1
    It would have helped if your question clearly mentioned that you are talking about it disappearing in the debugger while stepping through the code. Change "auto" to "local" at the bottom of the debugger pane. – rmaddy Aug 18 '15 at 17:13
  • Now I see it, but it's empty, in the 'Before' picture there are 9 objects inside it, after it is "0x100112800" and I can't access it. – Yahllilevy Aug 18 '15 at 17:19

3 Answers3

0

On the Debugger Bar, change the settings from Auto to Local or All Variables depending on your case. (Local should keep your variable on when debugging the method)

See the image below:

enter image description here

E-Riddie
  • 14,660
  • 7
  • 52
  • 74
  • Now I see it, but it's empty, in the 'Before' picture there are 9 objects inside it, after it is "0x100112800" and I can't access it. – Yahllilevy Aug 18 '15 at 17:19
  • For that question refer to these questions: http://stackoverflow.com/questions/15102082/view-object-property-state-in-xcode-while-debugging and http://stackoverflow.com/questions/4735156/xcode-debugger-view-value-of-variable – E-Riddie Aug 18 '15 at 17:22
  • It's not just tell me it's empty, so overriding the description won't help, I can't access it and if I try it's crash with this error: "Thread 1: EXC_BAD_ACCESS (code=1, address=)" – Yahllilevy Aug 18 '15 at 17:28
  • I can't reproduce your error, in my code it is showing ok, I can see the objects. (NSString Objects) If you are having specific problems with that, I suggest you to ask another question specific to your issue. – E-Riddie Aug 18 '15 at 17:40
  • I've seen this same problem with Xcode 6.x, at least twice (6.3 I think). The debugger seems to lose it's mind, and doesn't have access to any of your programs any more (both globals and variables in the current stack frame.) When that happened I had to quit and restart Xcode, and then all was back to normal. – Duncan C Aug 18 '15 at 17:42
  • This is not a compiler error, this is something in the code, I've restarted my computer a few times, it's doesn't work – Yahllilevy Aug 18 '15 at 17:52
0

This may have something to do with the fact that you're using a class method instead of an instance method. Make a mock version using an instance method, see if that retains the array. If it does, then you're on a first step to a solution. Also I assume you've got no other operations operating on your array, and that it's not being deallocated from another function going out of scope.

0

Are you using ARC? Replace:

@interface Game : NSObject
{
    NSMutableArray* boardPositions;
}

with:

@interface Game : NSObject
{
   @property(nonatomic, strong) NSMutableArray* boardPositions;
}

and change your other references to boardPositions to self.boardPositions. The compiler may be optimizing your array out of existence if you no longer use it.

Owen Hartnett
  • 5,925
  • 2
  • 19
  • 35
  • WOW! _boardPositions, not self.boardPositions, that's works!! Thanks for giving me the direction man! Tried for hours! Thank you so much! – Yahllilevy Aug 18 '15 at 18:59
  • It will appear as _boardPositions, but you access it in the console with self.boardPositions. If it stopped working, you might want to back up your changes to the previously working state. You can step through your code and type 'po self.boardPositions' (without the quotes) after every step to see what's happening to it. Put a breakpoint after your 'initWithObjects' call and step through after that. – Owen Hartnett Aug 20 '15 at 17:42