views:

43

answers:

2

Hey,

i have this code in my app where i put the text field text in my NSString property.

iname = textField.text;

after that at some point im doing this to make sure that the string is not empty

[iname isEqualToString:@""]

the problem is that if the textfield contain one word without space it works great if there is space it crashes! and i cant understand why ?

please help! thank you so much

A: 
iname = textField.text;

iname is an instance variable, right? If so, you need to take co-ownership of this string, so that the string doesn't die when the text field stops owning it (which will happen if anything, including the user, replaces the text field's value for text with a new value—a new string). Simply assigning the string's pointer to one of your instance variables does not take ownership of the string. See the Memory Management Programming Guide for Cocoa Touch for more information.

If you don't do that, then your program will crash when you try to send a message to the now-dead object whose pointer you have in iname.

after that at some point im doing this to make sure that the string is not empty

[iname isEqualToString:@""]

…for example.

the problem is that if the textfield contain one word without space it works great if there is space it crashes!

Sometimes there can be an element of randomness to whether the crash manifests.

The way to prove my theory would be to run your app under Instruments's Zombies instrument. That instrument causes objects with no owners to not die (be deallocated), but instead become zombies. When you send a message to a zombie, you'll get a flag in the Instruments timeline, which you can use to examine the object and its history to find out why it died before it should have/why you're still holding onto it after it died.

Peter Hosey
A: 

Using the getter properly solve the problem

self.iname = textField.text;

Orki
Do you understand why the problem took place? when you write `iname = @"something";` then you don't use the property and don't retain the string. I suppose that the property retains the object ("retain" or "copy"). This way, once you have changed to `self.iname = textField.text;` you have retained the string and before that the string was autoreleased. Therefore the object was already released once you tried to compare it...
Michael Kessler