子类化UIView时内存泄漏

时间:2010-02-17 06:44:32

标签: iphone memory uiview subclass memory-leaks

当使用UIView的子类时,我有内存泄漏。它泄漏128个字节并一直向下通过CoreGraphics等。我的子类只是一个生成的骨架,没有任何东西。当我使用UIView而不是ScrollView时,没有报告泄漏。它可能是什么,我错过了什么?

非常感谢, ALEX。

=====================================

//----  main.m

#import <UIKit/UIKit.h>

int main(int argc, char *argv[]) {

  //  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, @"testScrollViewAppDelegate");
  //  [pool release];
    return retVal;
}

//---testScrollViewAppDelegate.h

#import <UIKit/UIKit.h>

@interface testScrollViewAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
}

@property (nonatomic, retain) UIWindow *window;

@end


//--testScrollViewAppDelegate.m

#import "testScrollViewAppDelegate.h"
#import "ScrollView.h"

@implementation testScrollViewAppDelegate

@synthesize window;


- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    window.backgroundColor =  [UIColor whiteColor];

    CGRect frame =  CGRectMake(10, 150, 300, 200);
    ScrollView* scrollView = [[ScrollView alloc] initWithFrame:frame];

    [window addSubview:scrollView];

    [scrollView release],scrollView = nil;

    [window makeKeyAndVisible];
}


- (void)dealloc {
    [window release];
    [super dealloc];
}


@end

//-- ScrollView.h

#import <UIKit/UIKit.h>


@interface ScrollView : UIView {

}

@end


//-- ScrollView.m

#import "ScrollView.h"


@implementation ScrollView


- (id)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {
        // Initialization code
    }
    return self;

  }

- (void)drawRect:(CGRect)rect {
    // Drawing code
}

- (void)dealloc {
    [super dealloc];
}


@end

1 个答案:

答案 0 :(得分:0)

为什么要评论NSAutoreleasePool代码?如果没有自动释放池,很多ObjC和CF对象都会泄露。

(另外,请告诉我们您是如何实施-drawRect:的。)