0

I have an NSString property:

.h file

@property (nonatomic, retain) NSString *str;

.m file

@synthesize str;

What is the retain count of str without alloc/init? Can I [str release] in a method?

jscs
  • 63,694
  • 13
  • 151
  • 195
user2365995
  • 87
  • 1
  • 4

5 Answers5

3

I am assuming your new to the concept of memory management, so I would advise to have a read of the apple docs around memory management before you continue with your development.

Basic Memory Management Rules

  • You own any object you create.
  • You can take ownership of an object using retain.
  • When you no longer need it, you must relinquish ownership of an object you own.
  • You must not relinquish ownership of an object you don't own.

I will refer you to the Memory Management Policy in the apple docs for a good understanding of memory management

So when you have read the apple docs you will fully understand what is going on in your code, but if you don't this is what is wrong. You can not release an object that you don't have ownership of. This violates point 4 of the Basic memory management rules in the apple docs. To take ownership of this object you must do str = [[NSString alloc] init]; (This is not needed with ARC) in your .m file.

My recommendation though would be to read up about ARC it is a lot better then handling the memory management yourself. As you would no longer have to do things such as [str release]; once you wanted to relinquish ownership of the object as it is done automatically.

Community
  • 1
  • 1
Popeye
  • 11,839
  • 9
  • 58
  • 91
  • @AminNegm-Awad Did you want to know what garbage collection is or did what to know why I was talking about it? – Popeye May 12 '13 at 12:28
  • garbage collection is a very different concept. memory management != garbage collection. ARC and MRC are variants of reference counting. – vikingosegundo May 13 '13 at 13:38
  • @ikinciviking True, thanks for reminding me about this I had forgotten to come back and add more to it, I have removed refers to GC. What's with the name change? – Popeye May 13 '13 at 13:53
  • A coworker thought it to be funny to rename my account to turkish. vikingosegundo and ikinci viking means the same. I have to wait 2 more weeks to reverse it. – vikingosegundo May 13 '13 at 14:11
  • btw: this is [Amin Negm-Awad](http://www.amazon.de/Objective-C-Cocoa-Band-1-Grundlagen/dp/3908497825). I am pretty sure he knows what garbage collection is/was. @AminNegm-Awad, is it possible that I get an invite for Objective-Cloud? I am really curious. Danke! – vikingosegundo Aug 30 '13 at 18:00
  • Hi vikingosegundo or ikinciviking. :-) First of all the ink to the actual editon is http://www.amazon.de/Objective-C-Cocoa-Band-1-Grundlagen/dp/3908498082/ref=sr_1_1?s=books&ie=UTF8&qid=1377894583&sr=1-1&keywords=negm-awad :-] I have a metting with Christian at the end of the week. We will do some changes. After that, we will open a bunch of invitations. Please send me a reminder with your email address. – Amin Negm-Awad Aug 30 '13 at 20:29
  • @AminNegm-Awad, I guess addressing me with 2 names was to much for stackoverflow's scripts. luckily I stopped by again. I registered my mail for the invitation list already and will send you a mail through cocoading. – vikingosegundo Sep 24 '13 at 17:49
  • *ggggg* I read it. You will get it, but we are close to a public beta that will change some things. (No XPC any more, super-easy coding and projecting in Xcode and a 5-line client framework.) Are you at the Macoun (www.macoun.de) in 12 days? We can have a beer. – Amin Negm-Awad Sep 24 '13 at 20:05
2

You cannot release an object that has not yet been allocated.

Use ARC where possible, and read about changes to Objective-C in the past 2 years: it is no longer necessary to synthesize variables declared in .h in your .m

cleverbit
  • 5,514
  • 5
  • 28
  • 38
2

You shoudn't release an object that has not yet been allocated. But if you do it it means you are sending a message to a nil object. That is safe because a message to nil does nothing and returns nil, Nil, NULL, 0, or 0.0.

Mr. Frank
  • 288
  • 2
  • 7
  • One thing to note: Messages to NIL work with release because that has no return value, and with a few other methods (like retain) because they return pointers or integers. You can't rely on this working for example for methods that return a struct like CGRect or NSEdgeInsets. – uliwitness May 09 '13 at 22:56
2

Yes you can release this object. Whenever you send alloc, copy, new, retain any of these message to an object. It's retain count increased by 1. And you become the owner of that object. So you must release the object to relinquish the ownership.

And when you use ARC the compiler do it for you. Hope it helps.

Divyu
  • 1,325
  • 9
  • 21
1

As its a retained property you can do self.str=nil; instead of [str release] safely. You can not do [str release] as we can release only what we alloc and init by ourself and its not init yet.

Ishank
  • 2,860
  • 32
  • 43