Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MontyClift

macrumors newbie
Original poster
Sep 2, 2008
18
0
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.
 

MontyClift

macrumors newbie
Original poster
Sep 2, 2008
18
0
Sorry that was actually a typo, everywhere in the Controller.m file I call it as _obj
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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

macrumors regular
Mar 27, 2008
143
0
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

macrumors newbie
Original poster
Sep 2, 2008
18
0
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
Code:
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

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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

Code:
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

macrumors 6502a
Nov 9, 2007
733
6
Colchester, UK
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

macrumors newbie
Original poster
Sep 2, 2008
18
0
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

Code:
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.