discuss-gnustep
[Top][All Lists]
Advanced

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

Memory leak?


From: Marko Riedel
Subject: Memory leak?
Date: Wed, 5 Jan 2005 12:08:08 +0100

Hi folks,

I discovered a possible memory leak in an application that I am trying
to update and was able to isolate the problem in a test application. I
am sending the source code.

The test application opens a number of windows and fills them with
textviews and scrollviews. It prints the allocation count for the two
classes when the user quits the application.

Now do the following experiment: close some of those windows manually
and watch the allocation count at exit. It should decrease by the
number of windows that you have closed by hand, but it doesn't! (E.g
create ten windows, close four, should leave six textviews.)

What is happening here? Where is my mistake?

Best regards,

Marko Riedel

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

@interface Controller: NSObject

- (void)applicationWillTerminate:(NSNotification *)aNotification;

@end

@implementation Controller

- (void)applicationWillTerminate:(NSNotification *)aNotification;
{
    Class todebug[] = { 
        [NSScrollView class],
        [NSTextView class],
        nil
    }, *current;

    current=todebug;
    while(*current!=nil){
        id cl=*current;
        NSLog(@"%@ count %u total %u peak %u\n",
              NSStringFromClass(cl),
              GSDebugAllocationCount(cl),
              GSDebugAllocationTotal(cl),
              GSDebugAllocationPeak(cl));
        current++;
    }

}

@end


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];
    }

    Controller *con = [Controller new];
    [app setDelegate:con];

    [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]