Welcome to MacForumz.com!
FAQFAQ      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Autoreleased objects, no event loop

 
   Macintosh computer (Home) -> General Discussion RSS
Next:  Strange link error with Project Builder  
Author Message
Tom Harrington

External


Since: Aug 19, 2003
Posts: 939



(Msg. 1) Posted: Wed Aug 27, 2003 7:49 pm
Post subject: Autoreleased objects, no event loop
Archived from groups: comp>sys>mac>programmer>misc (more info?)

What's a good way of dealing with Cocoa objects that are implcitly
autoreleased, when you know there's no event loop coming to clean them
up? Like in a Foundation tool.

The specific situation is more or less as follows. In this code, an
autorelease pool has been created in main(), and at some time later code
like this is called in a function:

// NSArray *myArray defined elsewhere

while ( /* some condition is true */ ) {
id currentObject;
NSEnumerator *myEnum = [myArray objectEnumerator];
while (currentObject = [myEnum nextObject]) {
/* Do stuff with currentObject */
}
}

At every pass through the outer loop a new enumerator is created,
autoreleased. But there's no AppKit around, and therefore no event
loop, so those enumerators are going to exist for a potentially very
long time. I suppose I could iterate through the array using
-objectAtIndex:, but that just dodges a problem that may show up other
situations.

--
Tom "Tom" Harrington
Macaroni, Automated System Maintenance for Mac OS X.
Version 1.4: Best cleanup yet, gets files other tools miss.
See http://www.atomicbird.com/

 >> Stay informed about: Autoreleased objects, no event loop 
Back to top
Login to vote
Michael Ash

External


Since: Jun 23, 2003
Posts: 144



(Msg. 2) Posted: Thu Aug 28, 2003 12:36 am
Post subject: Re: Autoreleased objects, no event loop [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article <tph-C2F374.16490727082003@localhost>,
Tom Harrington <tph RemoveThis @pcisys.no.spam.dammit.net> wrote:

 > What's a good way of dealing with Cocoa objects that are implcitly
 > autoreleased, when you know there's no event loop coming to clean them
 > up? Like in a Foundation tool.
 >
 > The specific situation is more or less as follows. In this code, an
 > autorelease pool has been created in main(), and at some time later code
 > like this is called in a function:
 >
 > // NSArray *myArray defined elsewhere
 >
 > while ( /* some condition is true */ ) {

id pool = [NSAutoreleasePool new]; // create an autorelease pool
// for temp objects

 > id currentObject;
 > NSEnumerator *myEnum = [myArray objectEnumerator];
 > while (currentObject = [myEnum nextObject]) {
 > /* Do stuff with currentObject */
 > }

[pool release]; // destroy the pool and release the temp objects

 > }

It's quite easy to manage your own autorelease pools, as you can see. So
if you have a tight loop, you can manage your own. Depending on how you
want to trade off speed versus maximum memory consumption, you may want
to put the pool farther inside the loop or farther outside the loop.
This is also a useful technique even in AppKit applications if you're
going to be generating a lot of temporary objects before you go back to
the event loop, or if you're using multiple threads.<!-- ~MESSAGE_AFTER~ -->

 >> Stay informed about: Autoreleased objects, no event loop 
Back to top
Login to vote
Timothy1

External


Since: Aug 16, 2003
Posts: 2



(Msg. 3) Posted: Thu Aug 28, 2003 4:00 am
Post subject: Re: Autoreleased objects, no event loop [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

While I can not answer your question, I can ask one: why create a new
NSEnumerator each pass?

On Wed, 27 Aug 2003 16:49:07 -0600
Tom Harrington <tph RemoveThis @pcisys.no.spam.dammit.net> wrote:

--snip--
 >
 > // NSArray *myArray defined elsewhere
 >
 > while ( /* some condition is true */ ) {
 > id currentObject;
 > NSEnumerator *myEnum = [myArray objectEnumerator];
 > while (currentObject = [myEnum nextObject]) {
 > /* Do stuff with currentObject */
 > }
 > }
 >
 > At every pass through the outer loop a new enumerator is created,
 > autoreleased. But there's no AppKit around, and therefore no event
 > loop, so those enumerators are going to exist for a potentially very
 > long time. I suppose I could iterate through the array using
 > -objectAtIndex:, but that just dodges a problem that may show up other
 > situations.
 ><!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: Autoreleased objects, no event loop 
Back to top
Login to vote
Joe Shimkus

External


Since: Aug 31, 2003
Posts: 19



(Msg. 4) Posted: Thu Aug 28, 2003 4:03 am
Post subject: Re: Autoreleased objects, no event loop [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article <20030827205208.47e7a606.treaves.TakeThisOut@notme.silverfields.com>,
Timothy <treaves.TakeThisOut@notme.silverfields.com> wrote:

 > On Wed, 27 Aug 2003 16:49:07 -0600
 > Tom Harrington <tph.TakeThisOut@pcisys.no.spam.dammit.net> wrote:
 >
 > --snip--
  > >
  > > // NSArray *myArray defined elsewhere
  > >
  > > while ( /* some condition is true */ ) {
  > > id currentObject;
  > > NSEnumerator *myEnum = [myArray objectEnumerator];
  > > while (currentObject = [myEnum nextObject]) {
  > > /* Do stuff with currentObject */
  > > }
  > > }
  > >
  > > At every pass through the outer loop a new enumerator is created,
  > > autoreleased. But there's no AppKit around, and therefore no event
  > > loop, so those enumerators are going to exist for a potentially very
  > > long time. I suppose I could iterate through the array using
  > > -objectAtIndex:, but that just dodges a problem that may show up other
  > > situations.
  > >
 > While I can not answer your question, I can ask one: why create a new
 > NSEnumerator each pass?
 >

There's no provision for resetting an NSEnumerator; once you exhaust it,
you need to get a new one.


--
PGP Key (DH/DSS): <a style='text-decoration: underline;' href="http://www.shimkus.com/public_key.asc" target="_blank">http://www.shimkus.com/public_key.asc</a>
PGP Fingerprint: 89B4 52DA CF10 EE03 02AD 9134 21C6 2A68 CE52 EE1A<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: Autoreleased objects, no event loop 
Back to top
Login to vote
Matthew Russotto

External


Since: Aug 04, 2003
Posts: 292



(Msg. 5) Posted: Thu Aug 28, 2003 11:42 am
Post subject: Re: Autoreleased objects, no event loop [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article <tph-C2F374.16490727082003@localhost>,
Tom Harrington <tph.RemoveThis@pcisys.no.spam.dammit.net> wrote:
 >What's a good way of dealing with Cocoa objects that are implcitly
 >autoreleased, when you know there's no event loop coming to clean them
 >up? Like in a Foundation tool.
 >
 >The specific situation is more or less as follows. In this code, an
 >autorelease pool has been created in main(), and at some time later code
 >like this is called in a function:
 >
 >// NSArray *myArray defined elsewhere
 >
 >while ( /* some condition is true */ ) {
 > id currentObject;
 > NSEnumerator *myEnum = [myArray objectEnumerator];
 > while (currentObject = [myEnum nextObject]) {
 > /* Do stuff with currentObject */
 > }
 >}
 >
 >At every pass through the outer loop a new enumerator is created,
 >autoreleased. But there's no AppKit around, and therefore no event
 >loop, so those enumerators are going to exist for a potentially very
 >long time. I suppose I could iterate through the array using
 >-objectAtIndex:, but that just dodges a problem that may show up other
 >situations.
Bracket your loop with:

NSAutoreleasePool mypool = [[NSAutoreleasePool alloc] init];
[mypool release];
--
Matthew T. Russotto mrussotto.RemoveThis@speakeasy.net
"Extremism in defense of liberty is no vice, and moderation in pursuit
of justice is no virtue." But extreme restriction of liberty in pursuit of
a modicum of security is a very expensive vice.<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: Autoreleased objects, no event loop 
Back to top
Login to vote
Tom Harrington

External


Since: Aug 19, 2003
Posts: 939



(Msg. 6) Posted: Thu Aug 28, 2003 2:01 pm
Post subject: Re: Autoreleased objects, no event loop [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article <mail-17FAC0.21365027082003@localhost>,
Michael Ash <mail DeleteThis @mikeash.com> wrote:

 > In article <tph-C2F374.16490727082003@localhost>,
 > Tom Harrington <tph DeleteThis @pcisys.no.spam.dammit.net> wrote:
 >
  > > What's a good way of dealing with Cocoa objects that are implcitly
  > > autoreleased, when you know there's no event loop coming to clean them
  > > up? Like in a Foundation tool.
  > >
  > > The specific situation is more or less as follows. In this code, an
  > > autorelease pool has been created in main(), and at some time later code
  > > like this is called in a function:
  > >
  > > // NSArray *myArray defined elsewhere
  > >
  > > while ( /* some condition is true */ ) {
 >
 > id pool = [NSAutoreleasePool new]; // create an autorelease pool
 > // for temp objects
 >
  > > id currentObject;
  > > NSEnumerator *myEnum = [myArray objectEnumerator];
  > > while (currentObject = [myEnum nextObject]) {
  > > /* Do stuff with currentObject */
  > > }
 >
 > [pool release]; // destroy the pool and release the temp objects
 >
  > > }
 >
 > It's quite easy to manage your own autorelease pools, as you can see. So
 > if you have a tight loop, you can manage your own. Depending on how you
 > want to trade off speed versus maximum memory consumption, you may want
 > to put the pool farther inside the loop or farther outside the loop.
 > This is also a useful technique even in AppKit applications if you're
 > going to be generating a lot of temporary objects before you go back to
 > the event loop, or if you're using multiple threads.

Thanks, I didn't realize I could nest autorelease pools like that-- I
thought I'd have to release the existing one before creating a new one.
I've since located a discussion of this in Apple's documentation.

--
Tom "Tom" Harrington
Macaroni, Automated System Maintenance for Mac OS X.
Version 1.4: Best cleanup yet, gets files other tools miss.
See <a style='text-decoration: underline;' href="http://www.atomicbird.com/" target="_blank">http://www.atomicbird.com/</a><!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: Autoreleased objects, no event loop 
Back to top
Login to vote
Tom Harrington

External


Since: Aug 19, 2003
Posts: 939



(Msg. 7) Posted: Thu Aug 28, 2003 2:02 pm
Post subject: Re: Autoreleased objects, no event loop [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article <20030827205208.47e7a606.treaves.DeleteThis@notme.silverfields.com>,
Timothy <treaves.DeleteThis@notme.silverfields.com> wrote:

  > While I can not answer your question, I can ask one: why create a new
 > NSEnumerator each pass?

Because once you've iterated through the enumerator once, it's done.
You can't make it go back to the beginning. So in a loop like the one
below you need to either create a new one at every pass or find some way
to avoid creating them at all.


 > On Wed, 27 Aug 2003 16:49:07 -0600
 > Tom Harrington <tph.DeleteThis@pcisys.no.spam.dammit.net> wrote:
 >
 > --snip--
  > >
  > > // NSArray *myArray defined elsewhere
  > >
  > > while ( /* some condition is true */ ) {
  > > id currentObject;
  > > NSEnumerator *myEnum = [myArray objectEnumerator];
  > > while (currentObject = [myEnum nextObject]) {
  > > /* Do stuff with currentObject */
  > > }
  > > }
  > >
  > > At every pass through the outer loop a new enumerator is created,
  > > autoreleased. But there's no AppKit around, and therefore no event
  > > loop, so those enumerators are going to exist for a potentially very
  > > long time. I suppose I could iterate through the array using
  > > -objectAtIndex:, but that just dodges a problem that may show up other
  > > situations.
  > >

--
Tom "Tom" Harrington
Macaroni, Automated System Maintenance for Mac OS X.
Version 1.4: Best cleanup yet, gets files other tools miss.
See <a style='text-decoration: underline;' href="http://www.atomicbird.com/" target="_blank">http://www.atomicbird.com/</a><!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: Autoreleased objects, no event loop 
Back to top
Login to vote
Andrea Gozzi

External


Since: Aug 29, 2003
Posts: 5



(Msg. 8) Posted: Fri Aug 29, 2003 3:17 pm
Post subject: Re: Autoreleased objects, no event loop [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

 > Bracket your loop with:
 >
 > NSAutoreleasePool mypool = [[NSAutoreleasePool alloc] init];
 > [mypool release];

You can also strategically release the local pool every "n" loops, in this
way

if ((++cyclecounter)%somevalue)
{
[mypool release];
mypool=[[NSAutoreleasePool alloc]init];
}<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: Autoreleased objects, no event loop 
Back to top
Login to vote
Andrea Gozzi

External


Since: Aug 29, 2003
Posts: 5



(Msg. 9) Posted: Fri Aug 29, 2003 3:20 pm
Post subject: Re: Autoreleased objects, no event loop [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

 > Bracket your loop with:
 >
 > NSAutoreleasePool mypool = [[NSAutoreleasePool alloc] init];
 > [mypool release];

If you have long loops of impredictable lenght, you can also periodically
release the local pool by adding inside the loop section:

int poolcycles;

if (poolcycles++==somevalue)
{
poolcycles=0;
[mypool release];
mypool=[[NSAutoreleasePool alloc]init];
}<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: Autoreleased objects, no event loop 
Back to top
Login to vote
Joe Shimkus

External


Since: Aug 31, 2003
Posts: 19



(Msg. 10) Posted: Sat Aug 30, 2003 8:05 am
Post subject: Re: Autoreleased objects, no event loop [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article <bin96j$auevj$1@ID-113187.news.uni-berlin.de>,
"Andrea Gozzi" <agoz DeleteThis @inwind.it> wrote:

  > > Bracket your loop with:
  > >
  > > NSAutoreleasePool mypool = [[NSAutoreleasePool alloc] init];
  > > [mypool release];
 >
 > You can also strategically release the local pool every "n" loops, in this
 > way
 >
 > if ((++cyclecounter)%somevalue)
 > {
 > [mypool release];
 > mypool=[[NSAutoreleasePool alloc]init];
 > }
 >
 >

That would release the pool *except* every nth time. You'd want to
change your example to:

if ((++cyclecounter) % somevalue == 0)


--
PGP Key (DH/DSS): <a style='text-decoration: underline;' href="http://www.shimkus.com/public_key.asc" target="_blank">http://www.shimkus.com/public_key.asc</a>
PGP Fingerprint: 89B4 52DA CF10 EE03 02AD 9134 21C6 2A68 CE52 EE1A<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: Autoreleased objects, no event loop 
Back to top
Login to vote
Andrea Gozzi

External


Since: Aug 29, 2003
Posts: 5



(Msg. 11) Posted: Sat Aug 30, 2003 2:38 pm
Post subject: Re: Autoreleased objects, no event loop [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"Joe Shimkus" <joe.TakeThisOut@shimkus.com>
 > That would release the pool *except* every nth time. You'd want to
 > change your example to:

Yes, of course. I was still finishing editing when i hit the wrong key :)<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: Autoreleased objects, no event loop 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Quick feedback-loop despite preemptive multitasking - Hello, What are some strategies for getting a feedback-loop to run quickly in OS X? I am receiving data from an ADC (analog-digital converter) and need to output a function of each sample as it comes in without letting more than one sample at a time bac...

simple hack to reduce CPU utilization of WaitNextEvent loop - When I first carbonized my old-style MacOS application to run on OSX, I got a lot of complaints about high CPU utilization. It was using WaitNextEvent with 0 wait time, to do some time-sensitive idle processing. Bumping that up to 1 tick wait time on..

NIB objects don't receive dealloc message - After monitoring a NIB loaded view class that I created, I noticed that its retain count never drops below 2, even when the program shuts down. This prevents the object from ever receiving a dealloc message. Does this mean that this NIB based view neve...

Sending mouseclicks to WebView-Objects - I'd like to use clicks with the third/middle mouse button in a WebView to open the clicked links as new Tab. But how can this be done? I've already tried the following: 1) subclassing WebView and overwriting "otherMouseDown:" and in this met...

Getting mouseDown event for background window - It seems that if my (Carbon) application is in the background, and the user clicks on a window that contains controls, then I don't get a mouseDown event. I just get osEvt, activateEvt, mouseUp. When the window does not contain any (Control Manager)..
   Macintosh computer (Home) -> General Discussion All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]