• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Stumped on memory problem :(
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Stumped on memory problem :(


  • Subject: Re: Stumped on memory problem :(
  • From: Graham Cox <email@hidden>
  • Date: Tue, 6 Jan 2009 20:45:52 +1100


On 2 Jan 2009, at 12:14 pm, 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"]];

OK so far.

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

This line makes no sense.

It could be considered equivalent to:

array = [[[[NSMutableArray alloc] init] autorelease] init];

initing twice?

Replace with:

array = [[NSMutableArray alloc] init];

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

return [array autorelease];

You are autoreleasing the object, but it has already been autoreleased, so it's being done twice. Thus it is being over-released hence the error you are seeing. The double -init is wrong, so it's hard to say exactly what effect that is having on the outcome.


Note that the all of above code that copies the keys into an array can be replaced by this, assuming that you really do intend to return a mutable array:

return [[[dict allKeys] mutableCopy] autorelease];

But since you aren't using the dictionary at all, the entire method can be replaced by:

return [NSMutableArray arrayWithObjects:@"one", @"two", nil];

NSMutableArray* a =[StringHelper getStrings];
[a retain];
for(int i=0;i<a.count;i++) {
   NSLog(@"> %@",(NSString*)[a objectAtIndex: i]);
}
[a release];


There is no reason for retaining and releasing it here, as there is nothing that will release <a> out from under you. Autoreleased objects get released at well-defined times, namely, when the current event loop completes.

However, this shouldn't cause any harm.

Looks like you need to read the memory management docs again. ;-)

hth,

Graham


_______________________________________________

Cocoa-dev mailing list (email@hidden)

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:
This email sent to email@hidden


References: 
 >Stumped on memory problem :( (From: Jacob Rhoden <email@hidden>)

  • Prev by Date: Re: Stumped on memory problem :(
  • Next by Date: Re: Enum advice please
  • Previous by thread: Re: Stumped on memory problem :(
  • Next by thread: Re: Stumped on memory problem :(
  • Index(es):
    • Date
    • Thread