使用ARC的iPhone应用程序遇到严重问题。

我有一个viewcontroller(称之为A)。该视图控制器打开一个导航控制器,作为一个模态,该模式遍历3个不同的视图控制器(分别称为1、2和3)。查看数字3后,导航控制器关闭,我们再次返回A。

因此流程为:A打开导航控制器,并经过1-> 2-> 3,然后再次关闭。

每当我经历这个流程时,我都会失去记忆。为了解决这个问题,我搜索了所有文件,以寻找任何保留强大的属性,无效的计时器或类似的内容。

我有一个主意,可能是问题所在。在viewcontroller 1上,我展示了使用coreanimation和sprite制作的动画。我正在使用其他人的实现。似乎如果我禁用动画,则所使用的内存似乎非常恒定(因此没有内存丢失)。我对实现进行了一些修改以使用ARC。这是我用于精灵动画的实现:

MCSpriteLayer.h

//
//  MCSpriteLayer.h
//
//  Created by Miguel Angel Friginal on 8/20/10.
//  Copyright 2010 Mystery Coconut Games. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>

@interface MCSpriteLayer : CALayer {
    unsigned int sampleIndex;
}

// SampleIndex needs to be > 0
@property (nonatomic) unsigned int sampleIndex;

// For use with sample rects set by the delegate
+ (id)layerWithImage:(CGImageRef)img;
- (id)initWithImage:(CGImageRef)img;

// If all samples are the same size
+ (id)layerWithImage:(CGImageRef)img sampleSize:(CGSize)size :(int)useRetina;
- (id)initWithImage:(CGImageRef)img sampleSize:(CGSize)size;

// Use this method instead of sprite.sampleIndex to obtain the index currently displayed on screen
- (unsigned int)currentSampleIndex;


@end


MCSpriteLayer.m

//
//  MCSpriteLayer.m
//
//  Created by Miguel Angel Friginal on 8/20/10.
//  Copyright 2010 Mystery Coconut Games. All rights reserved.
//

#import "MCSpriteLayer.h"


@implementation MCSpriteLayer

@synthesize sampleIndex;

#pragma mark -
#pragma mark Initialization, variable sample size


- (id)initWithImage:(CGImageRef)img;
{
    self = [super init];
    if (self != nil)
    {
        self.contents = (__bridge id)img;
        sampleIndex = 1;
    }

    return self;
}


+ (id)layerWithImage:(CGImageRef)img;
{
    MCSpriteLayer *layer = [(MCSpriteLayer*)[self alloc] initWithImage:img];
    return layer;
}


#pragma mark -
#pragma mark Initialization, fixed sample size


- (id)initWithImage:(CGImageRef)img sampleSize:(CGSize)size;
{
    self = [self initWithImage:img];
    if (self != nil)
    {
        CGSize sampleSizeNormalized = CGSizeMake(size.width/CGImageGetWidth(img), size.height/CGImageGetHeight(img));
        self.bounds = CGRectMake( 0, 0, size.width, size.height );
        self.contentsRect = CGRectMake( 0, 0, sampleSizeNormalized.width, sampleSizeNormalized.height );
    }

    return self;
}


+ (id)layerWithImage:(CGImageRef)img sampleSize:(CGSize)size :(int)useRetina;
{

    CGSize newSampleSize;
    if(useRetina == 1) {
        // Supporting retina displays
        if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] &&
            ([UIScreen mainScreen].scale == 2.0)) {
            newSampleSize = CGSizeMake(size.width*2, size.height*2);
        } else {
            newSampleSize = size;

        }
    } else
        newSampleSize = size;

    MCSpriteLayer *layer = [[self alloc] initWithImage:img sampleSize:newSampleSize];
    return layer;
}


#pragma mark -
#pragma mark Frame by frame animation


+ (BOOL)needsDisplayForKey:(NSString *)key;
{
    return [key isEqualToString:@"sampleIndex"];
}


// contentsRect or bounds changes are not animated
+ (id < CAAction >)defaultActionForKey:(NSString *)aKey;
{
    if ([aKey isEqualToString:@"contentsRect"] || [aKey isEqualToString:@"bounds"])
        return (id < CAAction >)[NSNull null];

    return [super defaultActionForKey:aKey];
}


- (unsigned int)currentSampleIndex;
{
    return ((MCSpriteLayer*)[self presentationLayer]).sampleIndex;
}


// Implement displayLayer: on the delegate to override how sample rectangles are calculated; remember to use currentSampleIndex, ignore sampleIndex == 0, and set the layer's bounds
- (void)display;
{
    if ([self.delegate respondsToSelector:@selector(displayLayer:)])
    {
        [self.delegate displayLayer:self];
        return;
    }

    unsigned int currentSampleIndex = [self currentSampleIndex];
    if (!currentSampleIndex)
        return;

    CGSize sampleSize = self.contentsRect.size;
    self.contentsRect = CGRectMake(
        ((currentSampleIndex - 1) % (int)(1/sampleSize.width)) * sampleSize.width,
        ((currentSampleIndex - 1) / (int)(1/sampleSize.width)) * sampleSize.height,
        sampleSize.width, sampleSize.height
    );
}


@end


此实现是否某种程度上无法正确释放或保留任何内容?提前致谢。

更新资料
-我正在使用仪器测量内存。我正在使用内存监视器,在那里我关注物理内存的释放
-图像创建如下:

NSString *path = [[NSBundle mainBundle] pathForResource:@"round_start.png" ofType:nil];
CGSize fixedSize = CGSizeMake(320, 480);
mascot = [MCSpriteLayer layerWithImage:[UIImage imageWithContentsOfFile:path].CGImage sampleSize:fixedSize :0];
mascot.frame = CGRectMake(ANIMATION_X, ANIMATION_Y, ANIMATION_WIDTH, ANIMATION_HEIGHT);
[self.view.layer addSublayer:mascot2];

CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"sampleIndex"];
anim.delegate = self;
anim.fromValue = [NSNumber numberWithInt:1];
anim.toValue = [NSNumber numberWithInt:52];
anim.duration = ANIMATION_DURATION;
anim.repeatCount = 1;

[mascot addAnimation:anim forKey:nil];


-我一直在与关闭模态

[self dismissModalViewControllerAnimated:YES];




[self.navigationController dismissModalViewControllerAnimated:YES];

最佳答案

假设您对它有很强的引用,则解除导航控制器不会释放它(这将需要零)。在它使用的视图控制器中,在dealloc方法中添加一条日志消息,这样就可以知道它们正在被解除分配(您可以对任何子类项目执行此操作)。如果需要,您可以创建一个简单的UINavigation子类,其唯一目的是在dealloc中添加一条消息。您会发现这些项中的一项或多项没有被取消分配,然后您需要确定它们是由属性/ ivar保留还是由于它们仍具有超级视图。

10-08 03:23