On 2 Jan 2009, at 01:14, Jacob Rhoden wrote:

I have read the memory management documentation over and over but still cannot work out the problem with this code, can anyone see it? I have spent hours on this!

+(NSMutableArray*)getStrings {
 NSMutableDictionary* dict = [[NSMutableDictionary alloc] init];
 [dict setValue: @"" forKey: [NSString stringWithString:@"one"]];
 [dict setValue: @"" forKey: [NSString stringWithString:@"two"]];

 NSMutableArray *array = [[NSMutableArray array] init];

This line should read: NSMutableArray *array = [[NSMutableArray alloc] init];

This *may* be the cause of the problem.


 NSEnumerator* keyEnum = [dict keyEnumerator];
 NSString* key;
 while ((key = [keyEnum nextObject])) {
     [array addObject: key];
 }
 [dict release];

 return [array autorelease];
}

// Here is where I call the above function
NSMutableArray* a =[StringHelper getStrings];
[a retain];
for(int i=0;i<a.count;i++) {
 NSLog(@"> %@",(NSString*)[a objectAtIndex: i]);

You don't need to cast the object to NSString*. %@ works with any subclass of NSObject.


}
[a release];

AFTER the code is complete I see the following message, the address location is always the pointer to the array created in the getStrings method.

2009-01-02 12:07:29.277 Test[5184:813] *** -[CFArray release]: message sent to deallocated instance 0x132950

Any help muchly appreciated. I have tried with and without the [a retain] and [a release].
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/adc%40jeremyp.net

This email sent to a...@jeremyp.net

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to