Skip navigation.
 
mlMemory Management and objects creation via static methods...
FROM : Samvel
DATE : Tue Mar 25 05:19:39 2008

Hi,

Lots of classes define static methods that create objects for instance:

Class NSString:
   +(id) stringWithString: (NSString *) string;

or Class NSArray:
   +(id) arrayWithObjects: (id) firstObj, ...;

I am really confused about next situation and memory management of 
objects created via those methods. Please, explain.
Imagine class with

   NSString *string;

instance variable. I create it and initiate with method above in:
   -(id) init;

of my class. Now, want to use this string in another method sometime 
later, say in:
   -(void) plug;

Should I retain string in init?
e.g.:

   -(id) init
   {
     // ...
     string = [NSString stringWithString: @"hello"];
     [string retain];
     // ...
   }

   -(void) dealloc
   {
     // ...
     [string dealloc];
     // ...
   }

   -(void) plug
   {
     // ...
     NSLog( "%@", string);
     // ...
   }

Thanks, Samvel.

Related mailsAuthorDate
mlMemory Management and objects creation via static methods... Samvel Mar 25, 05:19
mlRe: Memory Management and objects creation via static methods... Ken Thomases Mar 25, 05:45
mlRe: Memory Management and objects creation via static methods... Jens Alfke Mar 25, 05:54
mlRe: Memory Management and objects creation via static methods... Sherm Pendley Mar 25, 05:54
mlRe: Memory Management and objects creation via static methods... Samvel Mar 25, 05:56