Re: Memory Managment (copys??)
Re: Memory Managment (copys??)
- Subject: Re: Memory Managment (copys??)
- From: Will Mason <email@hidden>
- Date: Sun, 6 Feb 2005 11:43:17 -0800 (PST)
- Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys
--- Thomas Davie <tom.dadaviemgmailom> wrote:
> >>> NSNSMutableArrayarray = [[NSNSMutableArraylallocininit
> >>> SOSOMEObjectobject;
> >>> int i;
> >>>
> >>> for (i = 0; i < 10; i++)
> >>> {
> >>> object = [[SOSOMEObjectlallocininit
> >>> [object sesetSomePropertyi];
> >>> [array adaddObjectobject];
> >>> [object release];
> >>> }
> >>> /* Do something with the array */
> >>> [array release];
> >>
> >> This code will leak. You should replace the first line in the for
> >> loop
> >> with:
> >>
> >> object = [[[SOSOMEObjectlallocininitauautorelease
> >>
> >> You need to make sure that any local objects you use are (retain,
> >> alalloccopy) / (auautoreleaserelease) balanced.
> >
> > Sorry, but it looks like you overlooked something. There are
> exactly 10
> > alalloc'sor the variable "object" and exactly 10 releases for it.
> There
> > is exactly one alallocor the variable "array" and 1 release for it.
> If
> > I replace the line as you suggest there will be 20 releases (10
> > releases and 10 auautoreleasesor the variable "object"), which will
> > cause a sesegault, as there are only 10 alalloc's
> >
> > There are no leaks in that code.
> I agree that there are no leaks, but there are 21 retains and 21
> releases in the code.
> The array is retained when you alalloct.
> Each object is retained when you alalloct.
> Each object is retained when you add it to the array
> Each object is released when you release it
> Each object, and the array is released when you release the array.
I agree, but I'm only concerned about retain/releases for which I am
personally reresponsibleFor all I know, the array might retain/release
the object 1000 times. I neither know nor care; it's not my job to
balance retain/release in code that I don't own. However, obviously you
are correct that there are *at least* 21 retains and 21 releases in the
code, and it's good to point that out for the sake of clarity.
Okay, I'll stop posting to this thread now, as I'm sure everyone is
quite bored...
Will
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden