开发者

Memory leak when subclassing UIView

开发者 https://www.devze.com 2022-12-20 04:48 出处:网络
I have a memory leak when just using subclass of UIView. It leaks 128 bytes and goes all the way to down thru CoreGraphics etc. My subclass is just a generated skeleton without anything in it. When I

I have a memory leak when just using subclass of UIView. It leaks 128 bytes and goes all the way to down thru CoreGraphics etc. My subclass is just a generated skeleton without anything in it. When I use just UIView instead of ScrollView no leak are reported. What could it be and what I am missing?

Thanks a lot, ALex.

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

//----  main.m

#import <UIKit/UIKit.h>

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

  //  NSAutoreleasePool * pool = [[NSAutore开发者_开发技巧leasePool 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


Why did you comment out the NSAutoreleasePool code? If there's no autorelease pool, a lot of ObjC and CF objects will be leaked.

(Also, please show us how did you implement -drawRect:.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号