1

I am new to IPhone programming and am I having trouble solving the following memory leak.

while(numDeckCounter < numDecks){
    int cardCounter=1;
    for (int i =1; i<=52; i++) {
        tempCard = [Card new]; //leaks tool says that this is leaking object
        if(i>=1 && i<=13)
        {
            tempCard.suit = CLUBS;
            tempCard.faceValue = cardCounter;
            [deckArr addObject:tempCard]; //reference count 2
            cardCounter++;
        }
        else if(i>=14 && i<=26)
        {
            tempCard.suit = DIAMONDS;
            tempCard.faceValue = cardCounter;
            [deckArr addObject:tempCard];
            cardCounter++;
        }
        else if(i>=27 && i<=39)
        {
            tempCard.suit = HEARTS;
            tempCard.faceValue = cardCounter;
            [deckArr addObject:tempCard];
            cardCounter++;
        }
        else
        {
            tempCard.suit = SPADES;
            tempCard.faceValue = cardCounter;
            [deckArr addObject:tempCard];
            cardCounter++;
        }
        if(cardCounter ==14){
            cardCounter=1;
        }
        [tempCard release];  //this causes an EXC_BAD_ACCESS -reference count should be 1
    }
    numDeckCounter++;
}

I was under the impression that adding an object to the array would increase its reference count by one, then it would be safe to release the object you just added because it would not be deallocated until the array was released bumping which would then release each object in the array. This is when the object should finally be deallocated.

When I add the [tempCard release]; it crashes my app because it can't access the memory location because it has already been deallocated.

From everything I have read, I think what I said above is true. Someone please correct me if I am wrong. Thanks.

  • 3
    How and where is temp card declared. Also why not declare it on the same line? `Card *tempCard = [[Card alloc] init];`. – Joe Apr 11 '12 at 20:56
  • 1
    If he was using ARC, it should be complaining about the use of `-release` (and not complaining about a leak). – Wevah Apr 11 '12 at 21:11
  • It's not a complete impossibility that he switched some flag none of us usually touch. – larsacus Apr 11 '12 at 21:16
  • Looking at your code, you shouldn't be having this error. Can you post the `init` method of your `Card` class. That is the only point of failure I can see. – Jimmy Luong Apr 11 '12 at 22:54

2 Answers2

1

I don't see any leaks in the code you presented. (There are some opportunities to slim it down, though, say by moving the identical operations out of the conditionals).

The Leaks tool output is pretty tricky to read. Is it possible that the Card object is leaking one of it's ivars?

Instead of leaks, run static analysis on your product (Product->Analyze). I think it will flag a different part of your code.

danh
  • 62,181
  • 10
  • 95
  • 136
  • You were right, I misread the output of leaks. My card objects were leaking a NSMutableString because I forgot to release it in the [Card dealloc] method. Thanks for the help everyone. –  Apr 12 '12 at 01:55
0

Perhaps try [[Card alloc] init] instead of [Card new]. This is just a guess. However trying the IMO more common method of object creation could be helpful.

Check this out: Use of alloc init instead of new

You could also try removing all the code for adding the Cards to the array. So you'd essentially have:

card = [Card new];
[card release];

This could help you find memory issues associated w/ the array retaining the object perhaps?

Community
  • 1
  • 1
SundayMonday
  • 19,147
  • 29
  • 100
  • 154
  • 1
    `new` is `alloc/init`, but `alloc/init` is the preferred way to do it. – Joe Apr 11 '12 at 20:53
  • I was under the impression that performed the same tasks, I did make the changes though and they had the same results. –  Apr 11 '12 at 20:56
  • @michaelw2608 try removing code until the problem goes away. Then slowly re-add the code and check for the issue each time you add a small piece of code. This approach usually solves a wide variety programming problems. – SundayMonday Apr 11 '12 at 20:57
  • @michaelw2608 your impression was correct, http://stackoverflow.com/questions/719877/use-of-alloc-init-instead-of-new-objective-c – Joe Apr 11 '12 at 20:58
  • 1
    There is nothing wrong with +new. +alloc/-init are used mostly for historical reasons that haven't been relevant in many, many years. But they're traditional and so they're how we tend to do it. Since moving to ARC, I've found myself using +new much more often for some reason (I haven't figured out why; it just seemed natural as part of moving to ARC). For a little more background: http://stackoverflow.com/questions/948070/objective-c-two-phase-construction-of-objects/948140#948140 – Rob Napier Apr 12 '12 at 01:37