Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is assigning self.string = @"" on an @property that's (retain)'d proper?

A philosophical question, of sorts. Is it proper to assign a constant string to an @property that's (retained)? Or, should I do self.string = [NSString stringWithString:@""];

Is there a memory leak? What if it's overreleased?

It's a constant string, so does it behave the same way as an NSString object?

If the property is (assign) does that mean it will not be valid after the run loop?

like image 436
AWF4vk Avatar asked Dec 02 '25 16:12

AWF4vk


1 Answers

Yes, it's fine. No matter which way you do it, the constant string will still be compiled into your program (because you have to use it in [NSString stringWithString:@""] anyway). Constant strings don't actually get retained/released, but the what matters is the semantics of it: you're assigning a string (which has a net +0 retainCount as far as you're concerned — you haven't alloced it, so you don't own it) to a property which will take ownership of it. If the property is (assign), then it may still work with a constant string, but it will be semantically invalid after the autorelease pool drains.

like image 133
jtbandes Avatar answered Dec 06 '25 01:12

jtbandes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!