Should I release simple NSString in dealloc - nsstring

I have in .h:
#property (nonatomic,retain) NSString *myString;
In .m:
#synthesize myString;
myString =#"Test";
Do I have to release it in dealloc?
Because sometimes my app crashes due to [myString release], not sure why?

As long as the property is defined with the "retain" attribute, it needs to be released in the dealloc.
ARC (Automatic Reference Counting) in modern Objective C has built-in garbage collection, so releasing and retaining are no longer necessary.

Related

Old version of sbjson throw exception in iOS 10

My problem is about coding in old version of sbjson,
there are some code which caused exception in iOS 10.3.3 compiled in XCode 8.3.3.
Update to new version of sbjson might work but since i am maintaining the iOS app, i don't want to change too much on the source code of the application.
In the old version, the class [SBJsonStreamParser.h] and [SBJsonStreamWriter.h] have code which define the #property of [SBJsonStreamParserState *state], [SBJsonStreamWriterState *state] as (nonatomic, assign) follow by a manual retain release setting [__weak],
which cause the error:
Unsafe_unretained property 'status' may not also be declared __weak
May i ask if it'll work fine if i just comment out the 'assign' setting of property, hence:
#property (nonatomic /* , assign*/) __weak SBJsonStreamWriterState
*state
Thanks a lot for the help
I don't think it was possible to use weak as a property modifier back when I wrote that, but the following might work now:
#property (nonatomic, weak) SBJsonStreamWriterState *state

viewDidUnload no longer called in ios6

I just installed the new version of Xcode/ios6. viewDidUnload is now depreciated.
In the apple doc,
viewDidUnload [...] Deprecated in iOS 6.0. Views are no longer purged under low-memory conditions and so this method is never called.
But numbers of apps are using this callback to release their properties, like :
- (void)viewDidUnload {
[super viewDidUnload];
self.recipientButton = nil;
self.connectButton = nil;
self.infoLabel = nil;
}
This was the best practice to release your IBOutlets.
So, first question:
What is going to happen these existing apps in iOS 6? Will they leak ?
and second one:
What is the new recommended way to release an IBOutlet property ? In dealloc method ?
For the first Question:
Your ViewController will receive didReceiveMemoryWarning method callback and you can nil out the view & other components in this method
For Reference Do Check WWDC 2012 video Session on EVOLUTION OF VIEW CONTROLLER, in case you haven't (I Believe they are available only for registered developers, but not sure).
Answer to your second one.
[object release]; in dealloc. No need to assign nil to object before releasing.
I recommend you to use weak property for the IBOutlets like
#property (weak) IBOutlet UILabel * labelText;
That way you don't need to do anything in dealloc. In iOS 6, simply ViewDidUnload won't call, iOS5 or earlier it is just call when memory warning have occur.
and second one: What is the new recommended way to release an IBOutlet
property ? In dealloc method ?
What is the "old" recommended way? You must always release retained instance variables in dealloc; it has always been this way and continues to be this way.
It was just that in viewDidUnload (which is only called for low memory) you could also set your properties to nil.

NSString - stringWithFormat released automatically

#property(nonatomic, retain) NSString *password;
-(id)init {
...
password=[NSString stringWithFormat:#"%#", [[NSProcessInfo processInfo] globallyUniqueString]];
OR
password=[[NSProcessInfo processInfo] globallyUniqueString];
}
My problem is that during course of execution at some random point the password object gets automatically released. Effect is same when i use either of the assignments. As soon as i put in a retain, the problem no longer exists. I'm sure there is no release of password object anywhere in the flow - as i mentioned it is in a singleton class. I also checked that the class instance is same even when the password object is released.
Any hints please!
You assign the iVar, not the property...
So as you don't use the setter method, your object is not retained.
Use the property instead:
self.password = ...
You could use...
password=[[NSString stringWithFormat:#"%#", [[NSProcessInfo processInfo] globallyUniqueString]] retain];
Also checkout Apple's doc on memory management. I would also set breakpoints in your code and see then your object gets released.

Constant NSString release

NSString *string = #"hello";
1) I keep reading that constant NSString does not get released, but this Apple page mentions:
the compiler makes such object
constants unique on a per-module
basis, and they’re never deallocated,
though you can retain and release them
as you do any other object.
http://developer.apple.com/mac/library/documentation/cocoa/conceptual/strings/Articles/CreatingStrings.html
2) If constant NSString does not get released, would it cause memory problems if used extensively? For example, is this a problem if repeated thousands of times:
NSString *string = #"One";
...
string = #"two";
...
string = #"three";
...
what's a good alternative?
Constant strings are part of you app's binary.
So, you do not need to worry about memory management, as they exist through all the execution and can not be released.

Create constant NSString from constant CFStringRef

I am attempting to create a Cocoa Framework that is an object-oriented wrapper around a procedural framework written in the Core Foundation. The procedural framework has a number of constants that are all defined as extern CFStringRef constantName.
How should I create NSString constants that directly map to the procedural constants so that in my framework I can simply cast the NSString constant to the CFStringRef constant within the framework such that the developer using my framework does not need to be aware of the casts himself.
Every thing that I have tried results in the compiler error Initializer element is not constant. This is the pattern I would like to use:
Constants.h:
extern NSString * myConstant
Constants.m:
#import "Constants.h"
NSString *myConstant = ConstantFromCFStringRef;
I am successfully declaring constant values with NSString in Constants.m using NSString *aConstant = #"someStringLiteral" but in this case, I want to have the same value as the CFStringRefs that I cannot ignore.
Have you tried:
NSString * myconstant = (NSString *)constantName;
You can find the same answer here: How to convert CFStringRef to NSString? and there: CocoaDev: CFStringRef.
Indeed Apple has already solved your problem. This is called Toll Free Bridging.

Resources