4

I have this code of string and I have problems trying to free up memory, I have understood that only those who release it initializes and is not autorelease but I had problems with the string "end", and as nSum release.

NSString *urlBase = [[NSString alloc] initWithFormat:@"http://service.svc/"];
NSString *op = [[NSString alloc] initWithFormat:@"op1"];
NSString * final = [urlBase stringByAppendingFormat:op];
NSString * nSum = sumTextfield.text;
final = [final stringByAppendingFormat:nSum];

//release

[ urlBase release ];
[ op release ];
//[final release]; error
//[final autorelease]; error

thank for you help.

UPDATE:

- (IBAction)mostrarOpciones {
 // code (UP)
}
0x8badf00d
  • 6,391
  • 3
  • 35
  • 68
JohnPortella
  • 1,791
  • 5
  • 21
  • 30

2 Answers2

8

If you create an object using a method that begins with init, new, copy, or mutableCopy, then you own that object and are responsible for releasing it (or autoreleasing it) when you're done with it. If you create an object using any other method, that object is autoreleased, and you don't need to release it. In that case, you actually need to retain the object if you want to keep it around. Apple has a Memory Management Programming Guide that includes all these rules.

The code you've posted is actually correct. You need to release urlBase and op because you created them using a method beginning with init (initWithFormat: in this case). final and nSum are already autoreleased for you. final was created by a method that doesn't begin with init, new, copy or mutableCopy (in this case, the factory method stringByAppendingFormat:). nSum was returned by a method called text, and you can assume that sumTextField "owns" it or has autoreleased it before returning it to you, and so you're not responsible for releasing it.

Andrew Madsen
  • 21,309
  • 5
  • 56
  • 97
  • by applying autorelease Final I get error. I forgot to say that these strings are in an IBAction, I get the error in the third call button. – JohnPortella Jan 02 '12 at 16:20
  • Yes, if you read what I wrote carefully, you'll note that I say that final is already autoreleased when you get it, so you do _not_ need to release or autorelease it yourself. If you do autorelease it, it has been autoreleased twice, meaning it will eventually be released twice, leading to a crash. – Andrew Madsen Jan 02 '12 at 16:21
  • Now I understood that final by having that kind of call comes with autorelease. Thank you for the help. – JohnPortella Jan 02 '12 at 16:24
  • Sure thing. I'd suggest carefully reading through the memory management guide I linked to, to make sure you really understand all this. Just as a tip for Stack Overflow, if a answer fixes your problem, you should "accept" it by clicking the checkmark under the voting arrows. Which is not to say that you must do that for my answer, only if it really solved your problem... – Andrew Madsen Jan 02 '12 at 16:47
  • "You need to release ... because you created them using a method beginning with init (`initWithFormat:` in this case)" Isn't it because you created them with `alloc`? If you received an `NSString` and called `initWithFormat:` on it, then you *shouldn't* release it -- whoever called `alloc` should release it? (Earnest question - I'm new at objc.) The linked programming guide uses a different list of prefixes requiring release: “alloc”, “new”, “copy”, or “mutableCopy”. – idbrii Mar 12 '21 at 01:40
  • 1
    @idbrii: It's very rare (I can't think of a time I've ever done it) that you'd call an init method on an object that has been alloced elsewhere before. In real use, alloc and init are essentially always paired up. To be more clear, I suppose I should have said that an object obtained with alloc/init comes with a +1 reference count and should be released. – Andrew Madsen Mar 16 '21 at 16:42
0

you cannot release NSString which you did not allocate. Since your two variables are not allocated, they need not to be released.

note-knotz
  • 173
  • 2
  • 8