Yaso
Yaso

Reputation: 643

Objective-C memory management: do you need to release string literals?

do you need to release something very simple this?

NSString *a = @"Hello";

//[a release];  ?

I come from a Java/C# world, and am confused about when things should be released/retained.

Upvotes: 0

Views: 120

Answers (2)

Louis Gerbarg
Louis Gerbarg

Reputation: 43452

No, you do not need to release a constant NSString, though it doesn't cause any problems if you do. Constant strings are special case of the memory management system. Since their content is known at compile time, it is statically defined in the application binary itself, so it never has to be allocated or freed at runtime. For that reason, its retain and release methods are noops.

This is only true for constant NSStrings (strings that start with @), and their toll free bridged cousin, constant CFStrings (defined using the CFSTR() macro).

Upvotes: 1

Jason McCreary
Jason McCreary

Reputation: 73011

No. You only need to release objects you init/alloc yourself or your instance variables in your class dealloc method.

Upvotes: 2

Related Questions