views:

42

answers:

3

Hi Forum

I'm a little confused about objc and allocating/releasing objects.

If I do this:

NSString *myString;

if([someString isEqualToString: @"test1"]){
    myString = @"got 1";
}else{
    myString = @"got 2";
}

Do I have to release myString after that?

And the same with self-defined objects:

myOwnObject *someObject = [someArray objectAtIndex: 1];

myButton.label1.text = someObject.name;

Do I have to release someObject?

The reason why I'm asking is that I get memory-leaks in a method and I can't find where it is. So I'm trying to figure out whether I do the alloc/release stuff correctly. The leak occurs on a NSPlaceholderString (I guess that's somewhere hidden in my NIB-File).

Also - if I have an object, allocate it, but only use some of the properties, but DO a release of every property on dealloc - will this cause memory leaks?

Sorry - hope my questions do make at least some sense :)

Thanks for any help!

A: 

No, you don't have to release either of those. I usually release only objects that I alloc, such as this snippet:

NSString *string = [[NSString alloc] initWithFormat:@"%@", something];
// yadda yadda yadda some code...
[string release];
esqew
thanks for the clarification - greatly appreciated ;)
Urs
A: 

To answer your first question, you don't need to release strings created with the @"" syntax.

On your second example, you should not have to release someObject. However, a problem could arise if your dealloc method in your myOwnObject class does not correctly release all of its instance variables.

Jergason
+1  A: 
jtbandes