-4

I have a litle problem with ARC which I understand why it does what it does but not how to prevent it. This code is part of a sample "Tick tac toe" game.

The problem is allocating a new UIView inside a loop subclassed with the name Tile. Once it's setup I will add the Tile (the UIView) to the current view and add it to the gamecontroller array of tiles later on used for reference.

Now the problem is that with every iteration of the loop, the tile object(s) get auto released, and I want them to be retained so I can store them in the gamecontroler's tile container. How do I make it remember the tiles?

This is the code on the gameDelegate:

- (void) addTile:(Tile *)tile {
    NSLog(@"Add tile %@", self.tiles);
    [tiles addObject:tile];
}

The output of the last add is here:

Posted on pastebin.com for better formatting

At this point, as expected, the whole local tiles array inside the game controller will be outputted and that's as expected; there is a list with Tile objects.

New download link

This is the code in board.m (subclass of UIView).

-(void) drawBoard
{
    NSLog(@"Drawboard called");

    for (int j =0; j < 3; j++) {
        for (int i = 0; i < 3; i++) {
            CGRect frame = CGRectMake(tilex, tiley, hlineDistance-1, vlineDistance-1);
            Tile* tile = [[Tile alloc] initWithFrame:frame];
            [self addSubview:tile];
            // ...
            [self.gameDelegate addTile:tile];
        }
        // ...
    }
    // ...
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Johnny Mast
  • 703
  • 1
  • 9
  • 17

1 Answers1

1

ARC is not the problem here. For instance, the super view (self) retains the tiles when you add them using:

[self addSubview:tile];

Verify that gameDelegate is not nil at the line:

[self.gameDelegate addTile:tile];

and that it actually adds the tiles to the array.

sch
  • 27,436
  • 3
  • 68
  • 83
  • Well the gamecontroller has this array already just like you said it. The problem is on every alloc of a new Object the prev version was autoreleased. – Johnny Mast Feb 26 '12 at 17:20
  • That is not a problem, they are supposed to be autorelaeased. But they are also retained by the view as the answer states. – Kendall Helmstetter Gelner Feb 26 '12 at 17:29
  • Yes i misread that. For some reason the tiles array is nill all of a sudden source code: https://www.wetransfer.com/dl/8ek6eq1E/03595e523e0e0c8941f9c2c7d20d4ffe249e74b2d8c771e428a8efe596aaeed71b9cf9e42812b60 – Johnny Mast Feb 26 '12 at 17:34
  • Sorry, I can't (and prefer not to) download the code. However I edited your post to keep only the relevant code. Can you post the code for `addTile` in `GameDelegate`? – sch Feb 26 '12 at 17:44
  • Post updated. New download location posted also i would like to add gameDelegate is an IBOutlet. – Johnny Mast Feb 26 '12 at 17:52
  • Do you see the log `NSLog(@"Add tile %@", self.tiles)`? What is its output? If you don't see it then `gameDelegate` is `nil` in the `drawBoard` method. If you see it and it outputs "nil", then `self.tiles` is `nil` and you should initialize in the init method of `GameDelegate`. – sch Feb 26 '12 at 18:05
  • sch did you see the download link ?. You can download the code. – Johnny Mast Feb 26 '12 at 18:12