Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: memory problem, advice needed



Yes! This works of course. If I forego the convenience constructors and use
[[... alloc] initWith...] followed by a [... release]; at the end of myFun()
all is good in the world. Thanks all for reminding me of this.


-Paul


On 4/1/06 1:40 AM, "Sean Murphy" <email@hidden> wrote:

> On Apr 1, 2006, at 12:40 AM, Paul Gribble wrote:
> 
>> > Here¹s a skeleton of my situation:
>> >
>> > for (i=1; i<1000; i++) {
>> >     int myResult;
>> >     myResult = myFun(someInput, someMoreInput);
>> > }
>> >
>> > then within myFun() the following sorts of things happen:
>> >
>> >  NSArray *myBigArray = [NSArray arrayWithObjects: mySmallArray1,
>> > mySmallArray2 ,nil];
>> >
>> > How do I ensure that when myFun() ends, myBigArray is released?
> 
> Hi Paul,
> 
> You can create an additional NSAutoreleasePool as Kevin mentioned
> previously, which means you would have to release the pool itself at
> the end of myFun().  This might degrade performance, however, since
> myFun() is called 1000 times, and extra work would be done each time
> creating and releasing the pool.  Not creating your own autorelease
> pool will not cause a memory leak, but it will wait until the event
> loop is over and free all 1000+ objects then.
> 
> With much less code, you can just handle the memory allocation and
> release of myBigArray yourself (without using autorelease pools) by
> simply doing this:
>         NSArray *myBigArray = [[NSArray alloc] initWithObjects:
> mySmallArray1, mySmallArray2, nil];
>         // rest of myFun() code here...
>         [myBigArray release];
>         return someInt;
> 
> Important to note is the fact that releasing the myBigArray within
> myFun() assumes it will not be returned, and not used by the for loop
> calling the myFun() method.
> 
> The [anArray array...] methods are called factory methods are
> shortcuts which create an already autoreleased object for you,
> meaning less code.
> 
> Good luck!
> 
> -Sean
> 

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.