discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

autorelease pool


From: Marko Riedel
Subject: autorelease pool
Date: Wed, 5 Jan 2005 14:30:52 +0100

Hi folks,

I modified my test program to check whether the scrollview and the
textview are indeed being put into the main autorelease pool. I also
created a string and a number for comparison purposes. The result is
that the views have an autorelease count of zero, and the string and
the number have an autorelease count of one. Is this correct behavior?
What is the reasoning here?

Best regards,

Marko Riedel

#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>


int main(int argc, char** argv, char **env)
{
    NSAutoreleasePool *pool = [NSAutoreleasePool new];
    NSApplication *app;
    
    GSDebugAllocationActive(YES);

    app = [NSApplication sharedApplication];

    NSMenu *menu = [NSMenu new];
    [menu addItemWithTitle: @"Quit"
          action:@selector(terminate:)
          keyEquivalent:@"q"];
    [NSApp setMainMenu:menu];

    int w;
    for(w=0; w<10; w++){
        unsigned int style = NSTitledWindowMask | NSClosableWindowMask | 
            NSMiniaturizableWindowMask | NSResizableWindowMask;
        NSRect winRect = { 100+w*40, 100+w*40, 100, 100 };
        NSWindow *window = 
            [[NSWindow alloc] initWithContentRect: winRect
                              styleMask: style
                              backing: NSBackingStoreRetained
                              defer: NO];

   
        NSScrollView *scrollView = 
            [[NSScrollView alloc] initWithFrame: winRect];
        [scrollView setHasHorizontalScroller: NO];
        [scrollView setHasVerticalScroller: YES]; 
        [scrollView setAutoresizingMask: 
                        NSViewHeightSizable | NSViewWidthSizable];
        [[scrollView contentView]
            setAutoresizingMask: NSViewHeightSizable | NSViewWidthSizable];
        [[scrollView contentView] setAutoresizesSubviews:YES];

        NSRect textRect = [[scrollView contentView] frame];
        NSTextView *textView = [[NSTextView alloc] initWithFrame: textRect];

        [scrollView setDocumentView: textView];
        RELEASE(textView);
        [window setContentView: scrollView];
        RELEASE(scrollView);    

        [window setReleasedWhenClosed:YES];

        [window orderFront:nil];

        NSString *str = [NSString stringWithCString:"test"];
        NSString *nmb = [NSNumber numberWithInt:42];
        NSLog(@"%u %u %u %u",
              (unsigned)[pool autoreleaseCountForObject:nmb],
              (unsigned)[pool autoreleaseCountForObject:str],
              (unsigned)[pool autoreleaseCountForObject:window],
              (unsigned)[pool autoreleaseCountForObject:scrollView],
              (unsigned)[pool autoreleaseCountForObject:textView]);
    }


    [app run];

    [pool release];
    exit(0);
}



-- 
+------------------------------------------------------------+
| Marko Riedel, EDV Neue Arbeit gGmbH, mriedel@neuearbeit.de |
| http://www.geocities.com/markoriedelde/index.html          |
+------------------------------------------------------------+



reply via email to

[Prev in Thread] Current Thread [Next in Thread]