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

Re: Memory management question


  • Subject: Re: Memory management question
  • From: Thomas Deniau <email@hidden>
  • Date: Mon, 29 Apr 2002 19:09:18 +0200

Le 29/04/02 18:49, Philippe Martin disait :

> 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 ];

Here you are allocating a NEW array which you will have to release.

> myArray = [ [ anotherArray objectAtIndex:i ]
> componentsSeparatedByString:@"\t" ];

And here you make myArray point to another array (i.e. It no longers points
to the instance you've just allocated) which is autoreleased (since you got
it from a method which isn't alloc, or new, copy).

> // do something here
> [ myArray release ];

And here you are releasing the already autoreleased instance, which produces
a crash.

So you have BOTH a leak (the first array which is never released) and a
crash (release on an autoreleased object).

--
Thomas Deniau
_______________________________________________
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.

References: 
 >Memory management question (From: Philippe Martin <email@hidden>)

  • Prev by Date: Re: Startup items
  • Next by Date: Re: suppressing a browser opening a link
  • Previous by thread: Memory management question
  • Next by thread: Re: Memory management question
  • Index(es):
    • Date
    • Thread