• 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
Memory management question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Memory management question


  • Subject: Memory management question
  • From: Philippe Martin <email@hidden>
  • Date: Mon, 29 Apr 2002 18:49:12 +0200

Hi,

I'm quite new at Cocoa, and I guess I have some troubles to understand when a new instance of an object or of a variable is created and when an existing instance is reused. For example, I came up with a method that went something like this:

-( void )doSomeThing {
int i;
NSArray *myArray;
for ( i = 0; i < [ anotherArray count ]; i++ ) {
myArray = [ [ NSArray alloc ] init ];
myArray = [ [ anotherArray objectAtIndex:i ]
componentsSeparatedByString:@"\t" ];
// do something here
[ myArray release ];
myArray = nil;
}
}
}

This crashed my application with an "EXC_BAD_ACCESS" signal. Not in this method, but shortly after having exited from it (the method was doing its job). When I commented out "[ myArray release ]" the crash was gone, but then I assumed the application was leaking.

I fixed it by making myArray "autoreleased", with something like this:

-( void )doSomeThing {
int i;
for ( i = 0; i < [ anotherArray count ]; i++ ) {
NSArray *myArray = [ [ anotherArray objectAtIndex:i ]
componentsSeparatedByString:@"\t" ];
// do something here
}
}
}

But I'd really like to understand why the first way was failing. I originally allocated and released myArray manually because the process may be intensive and I thought it was better to release the objects between each loop rather than only at the end. Obviously I didn't keep the count right, but I can't see where I've been lost. Help, please?

Thanks in advance!

Philippe
--
______________________________________________________________________
Philippe MARTIN (a.k.a. Flip) mailto:email@hidden
http://www.MacrobyteResources.com http://www.Free-Conversant.com
_______________________________________________
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.

  • Follow-Ups:
    • Re: Memory management question
      • From: email@hidden
    • Re: Memory management question
      • From: Andy Lee <email@hidden>
    • Re: Memory management question
      • From: Thomas Deniau <email@hidden>
  • Prev by Date: Re: XML-RPC
  • Next by Date: Re: C'mon Apple! DECIDE! (QuickTime Obj-C API)
  • Previous by thread: Re: XML-RPC
  • Next by thread: Re: Memory management question
  • Index(es):
    • Date
    • Thread