/*
 *  This Application subclass provides support for NSAutoreleasePools.
 *  If you have an application that uses Foundation but not EOF you
 *  need to make this class your Application class, otherwise your
 *  application will have a lot of serious memory leaks.
 *
 *  No guarantee is made for the fitness of this code for any particular
 *  use.  No warranty expressed or implied.  Use at your own risk!
 *
 *  Randy Tidd
 *  NeXT Premium Developer Support
 */
#import "PoolApplication.h"

@implementation PoolApplication : Application

- (void)_setupAutoreleasePool
{
    autoreleasePool = [[NSAutoreleasePool alloc] init];    
    disableCount = 0;
}

+ new
{
    id poolApplication;

    poolApplication = [super new];

    [poolApplication _setupAutoreleasePool];

    return poolApplication;
}

/*
 *  Release the current pool, and create a new one.
 */
- (void)_releasePool
{
	if(disableCount<= 0) {
		[autoreleasePool release];
		autoreleasePool = [[NSAutoreleasePool alloc] init];
	}
}

/*
 *  Release the current pool before and after every event.
 */
- sendEvent:(NXEvent *)event
{
	[self _releasePool];

	[super sendEvent:event];

	[self _releasePool];

	return self;
}

/*
 *  Need to disable the autorelease pool during modal loops otherwise
 *  objects that are involved in the modal loop will be freed prematurely.
 *  We need to have a "disable count" rather than a boolean flag because
 *  we can have nested modal sessions.
 */
- (int)runModalSession:(NXModalSession *)session
{
	int retVal;

	disableCount++;
	retVal = [super runModalSession:session];
	disableCount--;

	return retVal;
}

- (int)runModalFor:theWindow
{
	int retVal;

	disableCount++;
	retVal = [super runModalFor:theWindow];
	disableCount--;

	return retVal;
}


@end
