简体   繁体   中英

Bitmap causing memory leak on device

i am using simple bitmap technique to convert text into image , after that i divide this image into raster and later on i am calculating the percentage of black pixels in each raster rectangle. Every thing works fine on simulator but get crashed on Device.here is some related code

-(int)blackValue:(UIImage *)image rect:(CGRect)rect {
    int pixelInRect = (int)rect.size.width * rect.size.height;
    __block int blackCount = 0;
    ImageBitmap * imageBitmap = [[ImageBitmap alloc] initWithImage:image  bitmapInfo:(CGBitmapInfo)kCGImageAlphaNoneSkipLast];
    for (int x = 0; x <(int) rect.size.width; x++) {
        for (int y = 0; y < (int) rect.size.height; y++) {
            Byte * pixel = [imageBitmap pixelAtX:(int)rect.origin.x Y:(int)rect.origin.y];
            Byte red = pixel[0];
            if (red <  0.1)
            {
                blackCount++;
            }
        }
    }
    return blackCount/pixelInRect;
}

- (NSDictionary *)rasterizeBitmap:(UIImage *)image size:(CGFloat)size {
    CGFloat width =(int) (image.size.width/size);
    CGFloat height =(int) (image.size.height/size);
    NSMutableArray *fields = [NSMutableArray array];
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            CGRect rect = CGRectMake(x * size, y * size, size, size);
            CGPoint center = CGPointMake(x * size + size/2.0, image.size.height - (y * size + size/2.0));
            double black = [self blackValue:image rect:rect];
            Field * field = [[Field alloc] init];
            field.center = center;
            field.black = black;
            [fields addObject:field];
        }
    }
    return @{@"width":@(width) , @"fields":fields};
}

when i have try to run it in Profile i got the below result

在此输入图像描述

Can some one suggestion me how can i over come the memory issue?

The problem is that you're manually allocating memory in your ImageBitmap object, but you are never releasing it.

The two suspects are the bitmap context ( context ), and the bitmap data ( contextData ). Both of these are not managed by ARC, so you'll want to be freeing both of these yourself once you are done with them.

In ARC, you can simply implement the dealloc method in your ImageBitmap class and put your cleanup code there.

For example:

-(void) dealloc {
    CGContextRelease(context); // releases the bitmap context, if it was created (CGContextRelease checks for NULL)
    free(contextData); // releases the bitmap data (it was explicitly created, so no need to check)
}

It's also worth noting you should make init unavailable, and mark your designated initialiser.

This is because you cannot use your imageFromContext and pixelAtX:Y: instance methods without having created your instance through your custom initWithSize:bitmapInfo: initialiser, as it creates the bitmap context and allocates the memory for the bitmap data.

Therefore if you were to create your instance by calling init , and call one of your instance methods, you would most likely get a crash.

To remedy this, you can mark the init method as unavailable in your ImageBitmap.h file, and also mark your initWithSize:bitmapInfo: method as the designated initialiser.

-(instancetype) init NS_UNAVAILABLE;

-(id)initWithSize:(CGSize)size bitmapInfo:(CGBitmapInfo)bmInfo NS_DESIGNATED_INITIALIZER;

All the NS_UNAVAILABLE does is prevent you from creating your instance by just calling init , forcing you to use your custom initialisers.

If you try to do [[ImageBitmap alloc] init] , the compiler will show you the following error:

在此输入图像描述

All the NS_DESIGNATED_INITIALIZER does is make sure that any extra initialisers in ImageBitmap must create new instances through your initialiser, and will show you the following warning if they don't:

在此输入图像描述

See here for more info on NS_DESIGNATED_INITIALIZER .

Now, in practise these are really just formalities as you're the only one who's going to be using this, and you know you have to use the custom initialisers. However, it's good to get these formalities right if you ever want to share your code with other people.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM