shannoga
shannoga

Reputation: 19869

allocating and releasing VS. autoRelease. why and when?

I see that many people are allocating and releasing NSStrings.

I understand that the benefit is that the string is being released immediately and not by autoRelease.

my questions :

  1. does it effective and should i always prefer allocating and releasing on autoRelease?
  2. what is more expansive - allocating and releasing immediately and then allocating again OR allocating, using and releasing in dealloc.

will appreciate any explanation.

Thanks

shani

Upvotes: 0

Views: 382

Answers (2)

CodeStage
CodeStage

Reputation: 540

I don't see how you could re-use a NSString. Reusing a NSMutableString instance might be slightly faster the recreating it but you won't see the difference. Focus on simplicity and maintainability of your code.

I think your question is wether to use [[NSString alloc] init] or [NSString string]. As long as performance is not an issue, always go with the simplest one. That would be the autoreleased version, because you don't need to release it yourself.

Upvotes: 1

Kobski
Kobski

Reputation: 1636

  1. In most cases it does not matter. I think you should use the autorelease since it makes the code more simple and the @"string" shortcut is very elegant.
  2. The basic difference is the point in time when the release happens. Like I said, in most cases it does not make any difference. If you want to control the release time more closely, then you can also do that for autorelease by rolling your own NSAutoreleasePool pool.

Upvotes: 1

Related Questions