Re: Inconsistent Memory Management Rules
Re: Inconsistent Memory Management Rules
- Subject: Re: Inconsistent Memory Management Rules
- From: Denis Stanton <email@hidden>
- Date: Wed, 16 Apr 2003 14:30:33 +1200
On Wednesday, April 16, 2003, at 12:11 PM, publiclook wrote:
On Tuesday, April 15, 2003, at 07:24 PM, Denis Stanton wrote:
On Wednesday, April 16, 2003, at 08:32 AM, alex wrote:
// take date from words array
NSString *myDateString = [words objectAtIndex: 0];
int i;
for (i = 1; i < [words count]; i++) {
myDateString = [myDateString stringByAppendingString: @" "];
myDateString = [myDateString stringByAppendingString: [words
objectAtIndex: i]];
}
Your example does not leak. You didn't allocate or retain anything in
the example so you don't have to release or autorelease anything.
Cocoa's memory management conventions are that simple.
Really? That simple? No alloc or retain = no leak? I assumed that
since I was using something called myDateString it must exist and
therefore must be implicitly allocated space and therefore was a
potential memory leak. As you saying that in my little example
myDateString would just vanish when the method completed? That would
be a big relief. From using Java I have become used to garbage
collection and I thought that my first attempt at Objective-C was
leaving a trail of uncollected memory usage that would bite me later.
I intend doing a big code audit for this.
How about the one line solution:
NSString *totalDateString = [words componentsJoinedByString:@" "];
I am curious why you didn't use -componentsJoinedByString: since you
mention it in your post and therefore must know of its existence.
I have used .componentsJoinedByString in Java. I could not find such a
method in Objective-C through Cocoa Browser. I found
componentsSeparatedByString, but not ComponentsJoinedByString. I see
now (after comparing your reference to the Cocoa Browser definition of
NSString for ages) that the reason I couldn't find
ComponentsJoinedByString is that it is an instance method of NSArray,
not of NSString.
The other reason is I didn't initially recognize this as a place to use
ComponentsJoinedByString because the code was not quite as simple as my
example. I didn't want every item in the array included in the string.
To use ComponentsJoinedByString I will have to remove a few extraneous
items form the array first.
Thank you so much for taking time to set me straight.
Denis
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.