PDA

View Full Version : Losing object reference




MontyClift
Sep 2, 2008, 05:47 AM
Hi guys,
Sorry in advance if this is an obvious/simple one.
I'm new to objective c but for the most part I'm picking it up ok and enjoying learning the new language.
However I'm having a problem with my test app at the moment that I can't figure out…
// pseudocode
In the ViewController of my view,
In the .h I declare a private var and a property, something like
{
MyObject *_obj;
}
@property (nonatomic, retain) *obj;

in the viewDidLoad I have something like
_obj = [[MyObject alloc] initWithName:(NSString *)name];
label.text = _obj.name;

At this point when I debug the object is getting instantiated correctly from what I can see, all the properties get set and then the view label text is set correctly.

OK so a little further on I am handling an event where text is entered into a IUTextField
Within the function I need to do some manipulation on my MyObject _obj, but the object seems to be dead/null/nil
When I debug the application and halt it just inside the event handler, investigating the obj tells me that all the properties are set to “invalid” and of course trying to access the obj then causes the app to blow up.

Has anyone an idea what I'm doing wrong?
Thanks in advance for any help.



robbieduncan
Sep 2, 2008, 05:51 AM
Why is your object called _obj when you declare it as obj in the property and then use obj in your init?

MontyClift
Sep 2, 2008, 05:58 AM
Sorry that was actually a typo, everywhere in the Controller.m file I call it as _obj

robbieduncan
Sep 2, 2008, 06:50 AM
Well, with the tiny amount of code you've posted I can't see much more to help with. If you post all the code (in code tags please) we might have something to go on.

tacoman667
Sep 2, 2008, 07:01 AM
Hi guys,
Sorry in advance if this is an obvious/simple one.
I'm new to objective c but for the most part I'm picking it up ok and enjoying learning the new language.
However I'm having a problem with my test app at the moment that I can't figure out…
// pseudocode
In the ViewController of my view,
In the .h I declare a private var and a property, something like
{
MyObject *_obj;
}
@property (nonatomic, retain) *obj;

in the viewDidLoad I have something like
_obj = [[MyObject alloc] initWithName:(NSString *)name];
label.text = _obj.name;

At this point when I debug the object is getting instantiated correctly from what I can see, all the properties get set and then the view label text is set correctly.

OK so a little further on I am handling an event where text is entered into a IUTextField
Within the function I need to do some manipulation on my MyObject _obj, but the object seems to be dead/null/nil
When I debug the application and halt it just inside the event handler, investigating the obj tells me that all the properties are set to “invalid” and of course trying to access the obj then causes the app to blow up.

Has anyone an idea what I'm doing wrong?
Thanks in advance for any help.

Check to make sure you are not releasing it somewhere. NEVER release properties until the dealloc method. I did this once, and ONLY once, and realized the errors of my ways.

MontyClift
Sep 2, 2008, 07:15 AM
Well, with the tiny amount of code you've posted I can't see much more to help with. If you post all the code (in code tags please) we might have something to go on.
OK I'll post more comprehensive code when I'm at my mac later, at the moment I'm working on a windows machine. I was hoping it might just be something silly/obvious.
Apologies about the code tags, haven't posted here before.

I will post the actual code later, but one thing I should have mentioned was the initWithName function is actually taking a database reference
So it is more like

AppDelegate appDelegate etc... // get reference to the AppDelegate which contains a database property
_obj = [[MyObject alloc] initWithName(NSString *)name andDatabase:(sqlite *)appDelegate.database];

// in the init function on MyObject then I am querying the data base and populating the object from there
// this step does work as debugging here I can see all the properties correctly
label.text = _obj.name;

robbieduncan
Sep 2, 2008, 07:20 AM
I can't see the database reference making any difference.

As you have declared it as a property I assume you synthesized the accessors? If so it's good practice to use them :p


AppDelegate appDelegate etc... // get reference to the AppDelegate which contains a database property
MyObject *temp = [[MyObject alloc] initWithName(NSString *)name andDatabase:(sqlite *)appDelegate.database];
self._obj = temp;
[temp release];


I agree with tacoman667: you must be releasing _obj or setting it to nil somewhere.

Luke Redpath
Sep 2, 2008, 10:12 AM
As robbieduncan says, if you've set up properties and synthesized accessors, at the very least use the accessors for setting the variable as it will ensure it will get swapped with the old value with the proper retain/release calls (assuming you've configured your @property to use retain). By assigning to the variable directly you bypass this and need to retain/release yourself which defeats the whole point of @synthesize.

MontyClift
Sep 2, 2008, 12:20 PM
I can't see the database reference making any difference.

As you have declared it as a property I assume you synthesized the accessors? If so it's good practice to use them :p


AppDelegate appDelegate etc... // get reference to the AppDelegate which contains a database property
MyObject *temp = [[MyObject alloc] initWithName(NSString *)name andDatabase:(sqlite *)appDelegate.database];
self._obj = temp;
[temp release];


I agree with tacoman667: you must be releasing _obj or setting it to nil somewhere.

As robbieduncan says, if you've set up properties and synthesized accessors, at the very least use the accessors for setting the variable as it will ensure it will get swapped with the old value with the proper retain/release calls (assuming you've configured your @property to use retain). By assigning to the variable directly you bypass this and need to retain/release yourself which defeats the whole point of @synthesize.

Thanks a lot for your help guys,
As a vb.net programmer by trade I was unaware of how to really use properties and @synthesize properly. I changed the MyObject class, where it is reading from the database, aswell as the code in the viewcontroller to set the properties using the dot notation as opposed to directly hitting the private vars and it seems to be working fine now.