[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: does NSLog take ownership of the NSString it is given?
From: |
Sebastien Pierre |
Subject: |
Re: does NSLog take ownership of the NSString it is given? |
Date: |
Mon, 16 Jun 2003 09:28:41 +0200 |
Hi Dan,
> I'm still unclear about when temporary strings get free'd (other
> than when the pool they come from gets free'd),
The convention in the Foundation is that every object that gets created by its
class "factory" method (such as stringWithFormat, etc), as opposed to the
default alloc/init construction belong to the autorelease pool -- which mean
that they will be garbage collected once the autorelease pool is released.
You can think of autorealeased pools stacked arrays of object references. When
the pool is released, all references are deallocated. From what I've
experimented, autorelease pools are not processed by an asynchronous garbage
collector, which means that you can safely manipulate the autoreleased object
until the next releasing of the autorelease pool.
Another interesting point is that you can create "nested" autorelease pools
like this:
- foo
{
id pool = [[NSAutoreleasePool] alloc]init];
id string = [NSString stringWithCString:"Hello"];
//Here the string can be safely accessed, although it is is the pool
NSLog("%@", string);
[pool release];
//String was deallocated, so we cannot safely access it.
//There is a chance to get a core dump here.
NSLog("%@, string);
}
This can be used when you want to free some memory earlier than the next loop
in the event loop.
Cheers,
-- Sebastien