tags:

views:

25

answers:

5

Here is some code I have in my app that I DIDNT release. When I try to release I get a Program received signal: “EXC_BAD_ACCESS”. exception and the app crashes. If I dont try to release it the app runs fine. Do I need to call a release message on these objects? Any idea what could be going on here?

        NSString *sA = legA.text;
        NSArray *firstLeg = [sA componentsSeparatedByString:@","]; 


[sA release]; //works ok
[firstLeg release]; //sends the bad access exception and crashes the app
A: 

Both of them don't need to be -released.

I suggest you read the Memory Management Rules. You need to -release an object if and only if the current scope is an owner of that object. You become an owner only when the object is received as

  • [[Foo alloc...] init...]
  • [foo new...]
  • [foo copy...] or [foo mutableCopy...]
  • [foo retain]

in all other cases, you should never -release it. As .text and -componentsSeparatedByString: are not one of these methods, there's no need to -release sA and firstLeg.

KennyTM
YANARCR: Yet Another N.A.R.C Response! (New, Alloc, Retail and Copy)
ohhorob
A: 

I'm not great at objective c, but shouldn't there be a [[NSArray alloc] init] thing somewhere? Or does the componentsSeperatedBySTring replace it?

meman32
componentsSeparatedByString: creates its own array to return (and autoreleases it).
JeremyP
A: 

No, you may only send release to objects you have ownership off: those you get from alloc/init, retain or copy.

Claus Broch
A: 

Run, don't walk, to the "Object Ownership" section of the Memory Management Programming Guide for Cocoa and read up on the rules for when you "own" an object. You only release objects you own, so in this case you don't want to release them.

David Gelhar
A: 

Several correct answers so far, but nobody has yet posted the Cocoa Memory Management Rules which should be compulsory when answering a "do I own this" question..

Edit: must type faster...

JeremyP