views:

77

answers:

1

Hi everyone,

I've been cracking my head on a prob that doesn't quite make sense. Say I have the following in my project:

AppDelegate MyClassA MyClassB MyViewController

In MyClassA, there is a property of type MyClassB by the name classB. MyClassB has a property of type NSMutableArray by the pointer name myArray.

When in the MyViewController under viewDidLoad I instantiate MyClassA to access the myArray in its(MyClassA's) MyClassB property such as follows:

MyClassA *classA = [[MyClassA alloc] init];
NSLog([NSString stringWithFormat:@"%i", [[[classA classB] myArray] count]);

It will display the count on the log successfully and at the same time, further access to the contents of the array works fine.

However, still in the MyViewController, when the MyClassA is instantiated and accessed from the delegate as follows:

AppDelegate *aDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog([NSString stringWithFormat:@"%i", [[[aDelegate classA] classB] myArray] count]]);

the log will still display the count successfully, but further access to the contents in the array fails showing no error but a "Current language: auto currently objective-c" -- just one line of it.

This is a strange behavior I can't figure. Is there a difference between accessing objects instantiated in the delegate and accessing objects locally in a function(in the viewDidLoad of ViewController as in the example) or something?

Help is very much appreciated =D

+1  A: 

It sounds like your application is crashing due to access to already freed memory. If you wish to examine this further, I recommend you set the NSZombieEnabled environment variable to YES (in XCode's executable settings).

In this specific case, it would be good to know how exactly the classA member variable of the delegate is initialized and how it is accessed. Maybe any variable (be it classA, classB, or myArray) is autoreleased (or created using a convenience method) and not retained. You would then have access to this member variable until the autorelease pool is drained for the next time.

MrMage
Make sure Active Configuration is set to Debug, so you can examine the break point better.
Jordan