-2

This code is working on the iPad 4S, however, it results in a crash when ran on an iPad 1.

The code is not using ARC, ( I do not wish to use ARC either as I am trying to grasp the concept of memory management).

The header file on pastebin

The implementation file on pastebin

The code is a Coin Flip App, the user can select the amount of coins from a different class, and this value is accessed by the implementation file.

I initially thought the Arrays were giving me the trouble. From my understanding of memory management, I have declared them as @property(nonatomic, retain), meaning that I have ownership and thus must release them in the dealloc method. The dealloc method is called once the retainCount of an object reaches 0. I have tried my best to understand the problem.

But I am unsure if there is one, as the code works on the 4s.

John Conde
  • 217,595
  • 99
  • 455
  • 496
Ríomhaire
  • 3,084
  • 4
  • 25
  • 40
  • 12
    there is no thing such an iPad 4S :) – janusfidel Aug 15 '12 at 11:29
  • Please post your crash logs - if it works on one device but not another it is probably not a memory issue, and more likely something else. If you post your crash logs the root cause of your problem will be much easier to diagnose. It's unlikely people will want to read through all your code without them. – lxt Aug 15 '12 at 11:32
  • Loading all those images you could defently run into memory warnings on an iPad 1, since it has less memory then the iPhone 4S. – rckoenes Aug 15 '12 at 11:38
  • @lxt My apologies, I did not want to seem like I am trying to bum a free answer. Normally I look at the Logs in XCode, but when it crashes on the iPad it just goes to a black screen. No log, it is for this reason I posted up the code. – Ríomhaire Aug 15 '12 at 11:43
  • @Ríomhaire - as someone else has pointed out, it's quite possible you're getting a memory warning instead (when I say 'not a memory issue' I don't really mean that - I more mean, not a bad access issue). This would be why on older devices (which have less memory available) you run into problems. – lxt Aug 15 '12 at 11:46
  • for understanding the Memory Management in `Objective C`, you should read some relevant documentations before you start to confuse yourself with such a code you've presented. – holex Aug 15 '12 at 11:49
  • @lxt ok Thank you this is what my research had me believe. What would be the best way for me to present the problem to the community? Sync the iPad, and get the crash logs, and then post that? / What is the best information to post considering XCode doesn't output anything? Thanks – Ríomhaire Aug 15 '12 at 11:49
  • Why dont you add an exception breakpoint? It might help track the error. http://developer.apple.com/library/mac/#recipes/xcode_help-breakpoint_navigator/articles/adding_an_exception_breakpoint.html – geminiCoder Aug 15 '12 at 11:51
  • I have. I have gathered that when I alloc / copy etc. I increase the retainCount by 1. When i retain I also increment this count by 1. When I release an object, i decrement this count by one. The dealloc method is called when the retainCount for an Object = 0. I have also read that when I @property (retain) an object I must release it in the dealloc. In addition to this I watched the Stanford lecture on memory management but can not understand why the crash is happening at all. I was not looking for a quick fix solution, posting here is after many hours at the problem. – Ríomhaire Aug 15 '12 at 11:53

3 Answers3

3

See my comment above about providing the crash logs - however:

I see from your code you're using the retainCount method all over the place. Don't do it!

retainCount should never be used like this - it's a very confusingly named method that in 99% of cases just serves to cause further trouble. Apple's documentation is pretty clear on this point: don't use retainCount for things like this. See this question for more information (the top rated answer is actually by someone who now works as an Apple Frameworks engineer) :

When to use -retainCount?

Community
  • 1
  • 1
lxt
  • 31,146
  • 5
  • 78
  • 83
1

There are a lot of issues with that code.

• uses NSMutableArray when an NSArray will do

• calls fillArray way too often

• loads a ton of images all at once (likely source of memory issue, regardless)

• has a bunch of global variables that are probably supposed to be instance variables

• Does this: [NSString stringWithFormat:@" "] (just use @" " directly)

• uses retainCount at all

• leaks some objects

Post the crash log for more information on the actual crash. Use the static analyzer (build and analyze) and fix all indicated issues first.

bbum
  • 162,346
  • 23
  • 271
  • 359
0

I apologize in advance if I've missed something, but it looks like you're running out of memory because you're using:

    @property(nonatomic, retain)  NSMutableArray *myArray;

a "retained" synthesizer, and you're also allocating more memory for the array when you do this:

    [[NSMutableArray alloc] init]

So you're effectively doing this:

    [self setMyArray:[[NSMutableArray alloc] init]];

which is causing the array to be retained both by the allocation and again when assigned to your array variable. Retain count is 2. Instead, I'd use:

    [self setMyArray:[NSMutableArray array]];

In this case, the [NSMutableArray array] method autoreleases the array's memory before it is returned. The setter will apply a retain to the array as it is assigning it to your variable. Retain count is 1. Thus, when you release the variable in the dealloc, the array memory is freed.

Note that the retainCount method may not accurately reflect the true retain count since it doesn't consider autoreleases on the object.

Mike M
  • 4,358
  • 1
  • 28
  • 48
  • Thank you @Mike M,. I had tried to implement the code with `headsTails =[NSArray arrayWithObjects:`instead of with `alloc` and `init` , for this reason. However, when I do the App crashes at the [headsTails release] in dealloc, with the error message `*** error for object 0x581210: pointer being freed was not allocated`. So are you saying that I should omit the [headsTails release]. Also where should I include `[self setHeadsTails: headsTails]`. I tried various locations but it resulted in a crash. Thank you for explaining this to me. – Ríomhaire Aug 15 '12 at 13:41
  • self.headsTails = [[NSArray arrayWithObjects is equivalent I think. (Sorry, I thought that that's what you were doing before) – Mike M Aug 15 '12 at 14:23