4

I am using [[UIApplication sharedApplication] delegate] to share a variable across several classes. I set the value in the AppDelegate. I am able to NSLog it from the myAppDelegate.m and see the value. Then I try to NSLog the value when one of my Tabs loads, and it crashes:

myAppDelegate *app = (myAppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"Value:%@ ", app.delegateVar); // <--- Causes Crash

Basically seems like it is creating a new instance of app.delegateVar ?

delegateVar is defined in myAppDelegate.h and then it myAppDelegate.m I do this:

  - (void)applicationDidFinishLaunching:(UIApplication *)application {

        ...

        [delegateVar release];
        delegateVar = [NSString stringWithFormat:@"Test Value"];
        NSLog(@"%@",delegateVar);

    ...
    }
Chris
  • 5,485
  • 15
  • 68
  • 130
  • What the `delegateVar` property and how is it defined? – Shaggy Frog Nov 12 '10 at 03:17
  • What is the name of your app delegate class? Normal naming conventions would be to have `AppDelegate.m` contain a class named `AppDelegate`, not `myAppDelegate`. (and class names are capitalized by convention, so "`myAppDelegate *app = ...`" looks suspicious. – David Gelhar Nov 12 '10 at 03:21
  • The naming inconsistency was just an error in my editing when I posted here. – Chris Nov 12 '10 at 03:29
  • delegateVar is an NSMutableArray. I define it in myAppDelegate.m... see my edits above. – Chris Nov 12 '10 at 03:30
  • "Causes Crash" how? What error exactly are you getting? – David Gelhar Nov 12 '10 at 03:33
  • Are you sure delegateVar is a `NSMutableArray`? It's being set to a string in your `applicationDidFinishLaunching`. – Sam Dolan Nov 12 '10 at 03:36
  • It just dies, like when you NSLog a bad variable. If I comment out the NSLog, it works - but of course there is no access to the delegateVar. – Chris Nov 12 '10 at 03:37
  • Sorry. NSMutableString. I'm messing up info, overtired. – Chris Nov 12 '10 at 03:38

2 Answers2

6

One possibility is that delegateVar is being released prematurely.

For example, maybe the delegateVar property is not set up with the retain option, you are explicitly calling [delegateVar release], or you are bypassing the setter (and its retain semantics) by assigning directly to it (delegateVar = instead of self.delegateVar =).

In any case, look at the code that creates, assigns, and releases delegateVar.


Update:

Bingo. This is your problem right here:

    [delegateVar release];
    delegateVar = [NSString stringWithFormat:@"Test Value"];
    NSLog(@"%@",delegateVar);

You are assigning an autoreleased value (from +NSString stringWithFormat:) to delegateVar, and are not doing anything to retain it. That means that as soon as applicationDidFinishLaunching: returns, delegateVar is automatically released (and becomes invalid).

If delegateVar is a property with the "retain" option defined, you should be doing this as:

self.delegateVar = [NSString stringWithFormat:@"Test Value"];

You don't need to release delegateVar before assigning to it (using self.delegateVar =), because the setter will release the old value as needed. But you do need to release it in your dealloc method.

David Gelhar
  • 27,873
  • 3
  • 67
  • 84
  • 2
    Ahhh yes, the ol' "premature release" problem. – barfoon Nov 12 '10 at 04:31
  • Premature releasing is a serious problem. I have friends that suffer from premature releasing and simply raising awareness of the condition helps other premature releasers to confront their problem and find ways to deal with it. – dreamlax Nov 12 '10 at 06:39
  • Yes looks like it was a premature release problem. And to think that my girlfriend never complained... – Chris Nov 12 '10 at 15:24
2

David Gelhar has likely found the cause of the issue, however when you have memory management issues (EXC_BAD_ACCESS is a sign of memory management problems), there are a number of things you can do:

  1. Re-read the Cocoa memory management rules and make sure that you're following them.
  2. Run the static analyser. This will often pick up places where you have neglected the memory management rules.
  3. Try using NSZombieEnabled to find out whether you are sending messages to unallocated instances.
dreamlax
  • 93,976
  • 29
  • 161
  • 209