Reputation: 4706
I have read about the concept of ARC and how we can use the @property
to define strong
or weak
references. My question is how do a define or free pointers when they are inside methods and I would want the memory to be reclaimed as soon as the scope ends such as this
- (void) SomeMethod {
NSString* databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"contacts.db"]];
....
....
}
My question is how do I free the string databasePath
?
Upvotes: 1
Views: 45
Reputation: 726639
Unless some of the ....
code stores databasePath
in an instance variable, the memory for the string will be reclaimed as soon as you assign nil
to databasePath
, or let the variable go out of scope. You do not need to do anything special for it, because you used alloc / init
.
The story is slightly different with autoreleased objects, i.e.
NSString* databasePath = [NSString stringWithString: [docsDir stringByAppendingPathComponent: @"contacts.db"]];
This string would stay around until the run loop gets to decrementing its reference count, unless you create it inside a separate autorelease pool. Therefore, simply setting databasePath
to nil
is no longer sufficient to release the memory right away: you need to put an autorelease pool around your variable in order to achieve immediate freeing of memory.
Upvotes: 1
Reputation: 259
When you declared a local variable, it marks as strong reference by default. And the memory will be freed, when all the strong references to var have become lost. So outside scope, your variables memory automatically is freed, because strong reference is inside scope.
Upvotes: 2