Drawrect memory leak

806 Views Asked by At

I am subclassing an UIView in order to draw custom figures on a view, the view itself will be quite large, could go up to 10000 x 10000, we are using this large view so we can accommodate high level of zooms. I have tried various things to draw on a view, either by overwriting drawRect: or by adding a sublayer. However with sublayers on zoom or scroll motion becomes jerky and has a visible lag. This is due to sublayer being redrawn a lot.

With drawRect: however things are quite smooth except when I overwrite drawRect: there are a lot of memory usage, for 2000 x 2000 sized view it takes 70.5 mb (I am calling [super drawRect:rect] with no benefit). If I do not overwrite drawrect I can set my custom view to any size I tried even 30000 x 30000, there was no problem in regards to memory. However as soon as drawRect: gets overwrite memory leaks occur. My project is ARC based, I am quite satisfied with performance when using drawRect: to draw custom shapes, so I was wondering if there is a way to plug the memory leak? or optimize it a bit.

Any help is much appreciated, please let me know if I have not clarified something

Regards

The main/default viewcontroller.h

#import <UIKit/UIKit.h>
#import "P10Maps.h"

@interface P10ViewController : UIViewController {
    IBOutlet UIScrollView *scrollView;

}
@end

The main/default viewcontroller.m

#import "P10ViewController.h"
@interface P10ViewController ()

@end

@implementation P10ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    //[self initializeViews];
    //[self coordinateGen];
    mapView = [[P10Maps alloc] initWithFrame:CGRectMake(0, 0, 2000, 2000)];

    scrollView.contentSize = CGSizeMake(2000,2000);
    scrollView.minimumZoomScale = -10;
    scrollView.maximumZoomScale = 10;
    [scrollView setZoomScale:0.2 animated:YES];
    scrollView.delegate = (id)self;

    }
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return mapView;
}
@end

The subclass for UIview .h and .m respectively

#import <UIKit/UIKit.h>
#import "P10ViewController.h"
@interface P10Maps : UIView

@end

#import "P10Maps.h"

@implementation P10Maps
int countryID;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    NSLog(@"frame %@", NSStringFromCGRect(frame));
    [self drawPaths];
    if (self) {
        //self.opaque = YES;
        self.backgroundColor = [UIColor whiteColor];

    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
}

@end
0

There are 0 best solutions below