简体   繁体   English

ARC中的这个dealloc出了什么问题?

[英]What's wrong with this dealloc in ARC?

I am working on an app that does image processing and displays the resulting image. 我正在开发一个进行图像处理并显示结果图像的应用程序。 Im using UIScrollView to let user scroll all images, because the image is not a standard jpg or png, it takes time to load. 我使用UIScrollView让用户滚动所有图像,因为图像不是标准的jpg或png,加载需要时间。 I use GCD to load asynchronously, when finished dispatch to main queue to display. 我使用GCD异步加载,当完成调度到主队列显示时。 the snippet is as follows: 摘录如下:

- (void)loadImage:(NSString *)name
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        UIImage *image = [Reader loadImage:name];
        dispatch_sync(dispatch_get_main_queue(), ^{
            [self displayImage:image];
        });
    });
}

the loadImage method of Reader is like this: Reader的loadImage方法是这样的:

+ (UIImage *)loadImage:(NSString *)name
{
   UInt8 *data = NULL;
   NSString *mfjPath = [TMP stringByAppendingPathComponent:name];
   NSData *mfjData = [NSData dataWithContentsOfFile:mfjPath];
   if(mfjData){
        data = malloc(sizeof(UInt8)*mfjData.length);
        [mfjData getBytes:data];
   }
   if(data){
        ResultHolder *result = [sDecoder decodeData:data withOffset:0];// static id<IDecoder> sDecoder; in Reader.m before @implementation Reader.
        return [result bitmap];
   }
    retun nil;
}

IDCoder is a protocol which is IDCoder是一个协议

@protocol IDecoder <NSObject>
- (ResultHolder *)decodeData:(UInt8 *) withOffset:(int)offset;
@end

ResultHolder is a class to load simple image and combine complicated image. ResultHolder是一个加载简单图像并组合复杂图像的类。 which is as follows: 如下:

ResultHolder.h ResultHolder.h

typedef struct color24{
    UInt8 R;
    UInt8 G;
    UInt8 B;
} Color24;

@interface ResultHolder : NSObject
{
    unsigned long mWidth;
    unsigned long mHeight;
    UInt8 *mData;
    CGImageRef mBitmap;

    BOOL isMonoColor;
    Color24 mMonoColor;
}

+ (ResultHolder *)resultHolderWithCGImage:(CGImageRef)image;
+ (ResultHolder *)resultHolderWithData:(UInt8 *)data Width:(unsigned long)width andHeight:(unsigned long)height;
+ (ResultHolder *)resultHolderWithMonoColor:(Color24)monoColor withWidth:(unsigned long)width andHeight:(unsigned long)height;

- (ResultHolder *)initWithData:(UInt8 *)data Width:(unsigned long)width andHeight:(unsigned long) height;
- (ResultHolder *)initWithCGImage:(CGImageRef)image;
- (ResultHolder *)initWithMonoColor:(Color24)monoColor withWidth:(unsigned long)width andHeight:(unsigned long)height;

- (BOOL)isSuccess;
- (UIImage *)bitmap;
- (void)combineFixResultHolder:(ResultHolder *)child Rect:(CGRect)bounds Width:(unsigned long)width andHeight:(unsigned long)height;
- (void)combineResultHolder:(ResultHolder *)child Bounds:(CGRect)bounds Width:(unsigned long)width andHeight:(unsigned long)height;
@end

ResultHolder.m ResultHolder.m

@implementation ResultHolder

@synthesize width = mWidth;
@synthesize height = mHeight;
@synthesize isMonoColor;
@synthesize monoColor = mMonoColor;

- (ResultHolder *)initWithData:(UInt8 *)data Width:(unsigned long)width andHeight:(unsigned long)height
{

    if (self = [super init]) {        
        mWidth = width;
        mHeight = height;
        mData = malloc(mWidth*mHeight*sizeof(Color24));
        memcpy(mData, data, mWidth*mHeight*sizeof(Color24));

        mBitmap = NULL;
    }

    return self;
}

- (ResultHolder *)initWithCGImage:(CGImageRef)image
{
    if (self = [super init]) {
        mBitmap = CGImageRetain(image);
        mWidth = CGImageGetWidth(image);
        mHeight = CGImageGetHeight(image);
    }
    return self;
}

- (ResultHolder *)initWithMonoColor:(Color24)monoColor withWidth:(unsigned long)width andHeight:(unsigned long)height
{
    if (self = [super init]) {
        mMonoColor = monoColor;

        isMonoColor = YES;
        mWidth = width;
        mHeight = height;
        mBitmap = NULL;
        mData = NULL;
    }

    return self;
}

+ (ResultHolder *)resultHolderWithCGImage:(CGImageRef)image
{
    ResultHolder *resultHolder = [[ResultHolder alloc] initWithCGImage:image];
    return resultHolder;
}

+ (ResultHolder *)resultHolderWithData:(UInt8 *)data Width:(unsigned long)width andHeight:(unsigned long)height
{
    ResultHolder *resultHolder = [[ResultHolder alloc] initWithData:data Width:width andHeight:height];
    return resultHolder;
}

+ (ResultHolder *)resultHolderWithMonoColor:(Color24)monoColor withWidth:(unsigned long)width andHeight:(unsigned long)height
{
    ResultHolder *resultHolder = [[ResultHolder alloc] initWithMonoColor:monoColor withWidth:width andHeight:height];
    return resultHolder;
}

- (BOOL)isSuccess
{
    if ([ReaderConfigures CodecDebug])
        NSLog(@"ResultHolder isSuccess");
    return (mData != NULL || isMonoColor || mBitmap != nil);
}

- (void)fillMonoColor
{

    if (isMonoColor) {
        if (mData) {
            free(mData);
        }
        mData = (UInt8 *)malloc(mWidth*mHeight*sizeof(Color24));

        for (int i = 0; i < mHeight; i++) {
            for (int j = 0; j < mWidth; j++) {
                memcpy(mData+(i*mWidth+j)*3, &mMonoColor, sizeof(Color24));
            }
        }
        isMonoColor = NO;
    }
}


- (void)extractBitmap  
{
    if (mBitmap) {

        CGDataProviderRef dataProvider = CGImageGetDataProvider(mBitmap);
        CFDataRef bitmapData = CGDataProviderCopyData(dataProvider);
        UInt8 * dataSource = (UInt8 *)CFDataGetBytePtr(bitmapData);

        size_t width = CGImageGetWidth(mBitmap);
        size_t height = CGImageGetHeight(mBitmap);
        if(mData)
            free(mData);
        mData = malloc(width*height*3);

        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                memcpy(mData+(i*width+j)*3, dataSource+(i*width+j)*4, sizeof(Color24));
            }
        }

        CFRelease(bitmapData);
        CGImageRelease(mBitmap);
        mBitmap = NULL;
    }
}


- (UInt8 *)getRawData
{

    if (mBitmap) {
        [self extractBitmap];
    }
    if (isMonoColor) {
        [self fillMonoColor];
    }

    return mData;
}

- (UIImage *)bitmap
{
    if (mBitmap) {
        UIImage *image = [[UIImage alloc] initWithCGImage:mBitmap];
        CGImageRelease(mBitmap);
        mBitmap = NULL;
        return image; 
    }
    if (isMonoColor) {
        [self fillMonoColor];
    }
    if (mData) {
        CGDataProviderRef dataProvider = CGDataProviderCreateWithData(NULL, mData, mWidth*mHeight*3, NULL);
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        CGImageRef bitmap = CGImageCreate(mWidth, mHeight, 8, 24, mWidth*3, colorSpace, kCGBitmapByteOrderDefault, dataProvider, NULL, YES, kCGRenderingIntentDefault);
        CGColorSpaceRelease(colorSpace);
        CGDataProviderRelease(dataProvider);
        UIImage *image = [[UIImage alloc] initWithCGImage:bitmap];
        CGImageRelease(bitmap);

        return image;
    }

    return nil;
}

- (void)combineResultHolder:(ResultHolder *) child Bounds:(CGRect) bounds Width:(unsigned long)width andHeight:(unsigned long)height
{
    CGRect rect = CGRectMake(MAX(0, bounds.origin.x), MAX(0, bounds.origin.y),MIN(width - 1, bounds.origin.x + bounds.size.width), MIN(height - 1, bounds.origin.y + bounds.size.height));

    int w = MIN(rect.size.width + 1, child.width);
    int h = MIN(rect.size.height + 1, child.height);

    int dstPos = (height - 1 - (rect.origin.y + h - 1))*width;

    UInt8 *dataParent = [self getRawData];

    if (child.isMonoColor) {
        Color24 childMonoColor = child.monoColor;
        for (int i = 0; i < h; i++) {
            memcpy(dataParent+(dstPos+(int)rect.origin.x)*3, &childMonoColor, w*3);
            dstPos += width;
        }

    } else {
        UInt8 *dataChild = [child getRawData];
        if (dataChild != nil) {
            int srcPos = 0;
            for (int i = 0; i < h; i++) {
                memcpy(dataParent+dstPos*3+((int)rect.origin.x)*3, dataChild+srcPos*3, w*3);
                srcPos += child.width;
                dstPos += width;
            }
        }

    }

}

- (void)combineFixResultHolder:(ResultHolder *)child Rect:(CGRect)bounds Width:(unsigned long)width andHeight:(unsigned long)height
{
    CGRect rect = CGRectMake(bounds.origin.x, height-1-bounds.origin.y-bounds.size.height, bounds.origin.x+bounds.size.width, height-1-bounds.origin.y);

    [self combineResultHolder:child Bounds:rect Width:width andHeight:height];
}

- (void)dealloc
{
    if (mData) {
        free(mData);
        mData = NULL;
    }
    if (mBitmap) {
        CGImageRelease(mBitmap);
        mBitmap = NULL;
    }
}

@end

for simple image, for example JPEG image only, + (ResultHolder *)resultHolderWithCGImage:(CGImageRef)image; 对于简单图像,例如仅JPEG图像, + (ResultHolder *)resultHolderWithCGImage:(CGImageRef)image; and - (UIImage *)bitmap; - (UIImage *)bitmap; methods are called. 方法被称为。 for some complicated ones, ResultHolder will extract mBitmap to mData , and then combine with sub resultHolder 's mData to get the image. 对于一些复杂的问题, ResultHolder将提取mBitmapmData ,然后用分结合resultHoldermData来获取图像。 these methods work well if I load image in my main thread, but if I use GCD or NSThread to load image in background it is easy to crash, especially when loading complicated ones in background. 如果我在主线程中加载图像,这些方法效果很好,但如果我使用GCD或NSThread在后台加载图像,则很容易崩溃,尤其是在后台加载复杂图像时。 when the app crashes, the main thread state a CGSConvertBGR888toRGBA8888 method error, one of other threads is running the [ResultHolder dealloc] method, actually is free(mData) . 当应用程序崩溃时,主线程状态为CGSConvertBGR888toRGBA8888方法错误,其他一个线程正在运行[ResultHolder dealloc]方法,实际上是free(mData) It seems there is a memory conflict between the loading thread and the main thread. 似乎加载线程和主线程之间存在内存冲突。

when the app crashes, the error is like this: 当应用程序崩溃时,错误是这样的: 在此输入图像描述

I have struggled for this bug for days, but still cannot find how to fix it. 我已经为这个bug挣扎了好几天,但仍然找不到如何修复它。 I do hope someone can help me. 我希望有人可以帮助我。 Any suggestions are appreciated. 任何建议表示赞赏。

UPDATE: I make a demo project ReaderDemo to simulate the situation. 更新:我制作一个演示项目ReaderDemo来模拟情况。 If you are interested, you can download to see the error. 如果您有兴趣,可以下载查看错误。 There are 15 images in this project, the 5,7,14 images will cause the crash when scrolling, they are a little complicated than others. 这个项目中有15个图像,5,7,14个图像在滚动时会导致崩溃,它们比其他图像有点复杂。 but if you scroll through thumbnail scrollview then click, they all can be displayed. 但如果您滚动缩略图滚动视图然后单击,它们都可以显示。

You have a number of problems but lets start off with the first I found: 你有很多问题但我们从第一个发现开始:

  1. Improper test 测试不当

     if (index > [mPageNames count]) { 

    That needs to be >= or you crash. 那需要> =或你崩溃。

  2. you are calling dispatch_sync on the mainQueue, that does not seem to be a good decision (but maybe you have a really good one) - I changed it to async, seems to work OK 你在mainQueue上调用dispatch_sync,这似乎不是一个好的决定(但也许你有一个非常好的) - 我把它改为异步,似乎工作正常

  3. If you enable exceptions in this project it will really help you. 如果您在此项目中启用例外,它将真正帮助您。 Click the Break Points button in the Xcode toolbar. 单击Xco​​de工具栏中的Break Points按钮。 Then select the BreakPoints option left pane, second from the right. 然后选择BreakPoints选项左侧窗格,右侧第二个。 Tap the bottom left '+' icon and add an All Exceptions breakpoint. 点击左下角的“+”图标,然后添加一个All Exceptions断点。 Now when you run the debugger stops where the problem occurrs. 现在,当您运行调试器时会停止发生问题的位置。

  4. I got a final crash that I'll let you fix: 我得到了最后的崩溃,我会让你解决:

     2012-09-26 08:55:12.378 ReaderDemo[787:11303] MFJAtIndex index out of bounds,index:15,bounds:15 2012-09-26 08:55:12.379 ReaderDemo[787:11303] *** Assertion failure in -[ImageScrollView showLoadingForMFJ:], /Volumes/Data/Users/dhoerl/Downloads/ReaderDemo/ReaderDemo/ImageScrollView.m:247 

This should get you on your way. 这应该可以帮到你。


EDIT: Your problem relates to the management of the mData memory. 编辑:您的问题涉及mData内存的管理。 You are trying to manage the lifecycle of it in your code, but this management is not sync'd with the CGImageDataProvider that is trying to use it. 您正尝试在代码中管理它的生命周期,但此管理不会与尝试使用它的CGImageDataProvider The crash is almost for sure (meaning I'm 99.99% convinced) a byproduct of the CGImageProvided created by CGDataProviderCreateWithData trying to access the data after your class has freed that memory in dealloc. 崩溃几乎是肯定的(意思是我相信99.99%)中的副产品CGImageProvided通过创建CGDataProviderCreateWithData类已被释放,在dealloc的内存后试图访问数据。 I have had similar experiences with data providers. 我与数据提供商有过类似的经历。

The proper solution is to remove all free(data) calls, or at least most of them. 正确的解决方案是删除所有free(data)调用,或至少删除大部分调用。 Given the current structure of your code you will need to think about this carefully - you may want to replaced all the tests and malloc/frees with a flag. 鉴于代码的当前结构,您需要仔细考虑这一点 - 您可能希望用标志替换所有测试和malloc / frees。 In the end, what you want to do is once the memory pointer is handed ovdr to CGDataProviderCreateWithData , you need to NULL out mData and let the data provider handle the removal. 最后,你要做的是一旦将内存指针交给ovdr到CGDataProviderCreateWithData ,你需要将mData NULL出来并让数据提供者处理删除。

The way to do this is to provide a function pointer to CGDataProviderCreateWithData in the past parameter: 这样做的方法是在过去的参数中提供一个指向CGDataProviderCreateWithData的函数指针:

CGDataProviderReleaseDataCallback
A callback function that releases data you supply to the function CGDataProviderCreateWithData.

typedef void (*CGDataProviderReleaseDataCallback) (
   void *info,
   const void *data,
   size_t size
);

All that function needs to do is just call free(data); 所有功能需要做的就是free(data);呼叫free(data); . So whenever the data provider is done with the allocated memory, it will free it (and you don't need to worry about it). 因此,每当数据提供程序完成分配的内存时,它就会释放它(并且您不必担心它)。

If you want to free() or release your resources in any class in ARC enabled environment, you have to set proper flags for your class in 'Build Phases'. 如果要在启用ARC的环境中的任何类中释放()或释放资源,则必须在“构建阶段”中为类设置适当的标志。 To do that, select your project file in XCode, select your target, go to 'Build Phases' section, find your class, and put -fno-objc-arc flag for that class. 为此,在XCode中选择项目文件,选择目标,转到“构建阶段”部分,找到您的类,并为该类添加-fno-objc-arc标志。 Or, maybe another reason, you are calling some CoreGraphics function that has to be called from main thread only in another thread. 或者,也许是另一个原因,您正在调用一些CoreGraphics函数,该函数必须仅在另一个线程中从主线程调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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