ios - Objective-C 内存管理问题

标签 ios objective-c memory-management

我是 iPhone 编程和 Objective-C 的新手。
我正在构建一个基于 View 的应用程序。

问题是 UIViewController 的 dealloc 函数都没有被调用过。
我决定在呈现下一个 UIViewController 类之前以编程方式卸载所有保留的对象。
我已经解决了 Xcode 检测到的所有泄漏问题,使用 Leaks and Allocations Tools 测试了应用程序,一切正常,但是使用的内存达到了 180 MB 左右,应用程序崩溃了。
self.retainCount在呈现下一个 UIViewController 为 1 之前,所拥有的对象被释放并且指针为 nil,但内存仍在增加。

你能给我一个线索吗?如果需要,我会发布一些代码示例。或者你能给我发一些关于Objective-C memory management 101之类的引用资料吗?

这是我的界面:

@interface GameScreen : UIViewController
{
    IBOutlet UILabel     *timeLabel;
    IBOutlet UIView      *gameView;
    IBOutlet UIImageView *gameViewFrame;
    IBOutlet UIButton    *showOutlineButton;    
    UIImage *puzzleImage;
    UIView *optionsView;
    UIView *blockView;  
    NSTimer *timer;
    UIImageView *viewOriginalPicture;
    SHKActivityIndicator *activityIndicator;
    BOOL originalPictureShown;
    BOOL outLineShown;
}

@property (nonatomic, retain) IBOutlet UILabel     *timeLabel;
@property (nonatomic, retain) IBOutlet UIView      *gameView;
@property (nonatomic, retain) IBOutlet UIImageView *gameViewFrame;
@property (nonatomic, retain) IBOutlet UIButton    *showOutlineButton;
@property (nonatomic, retain) UIImage *puzzleImage;

这是实现:
- (id) initWithPuzzleImage: (UIImage *) img
{
if((self = [super init]))
{
    puzzleImage = [[UIImage alloc] init];
    puzzleImage = img;
}
return self;
}

这是用户点击退出按钮时调用的函数:
- (void) onExit
{   
[timer invalidate];
CurrentMinuts = 0;
CurrentSeconds = 0;

    //remove piece configurations
    [pieceConfigMatrix removeAllObjects];
    [pieceFramesMatrix removeAllObjects];

PuzzleViewController *modalView = [[PuzzleViewController alloc] init];
    [self unloadObjects];
[self presentModalViewController:modalView animated:YES];
[modalView release];

}

unloadObjects功能:
- (void) unloadObjects
{
    [self resignFirstResponder];         
    [viewOriginalPicture release];
    viewOriginalPicture = nil;  
    [timeLabel release];
    timeLabel = nil;
    [gameView release];
    gameView = nil;
    [originalImage release];
    originalImage = nil;
    [gameViewFrame release];
    gameViewFrame = nil;
    [timer release];
    [showOutlineButton release];
    showOutlineButton = nil;    
}

我知道我做错了什么,但我不确定。让我解释。我正在将拼图添加到“gameView”属性中。为此,我有一个“SplitImage”对象。在 - (void) viewDidLoad 中调用以下函数:
- (void) generatePuzzleWithImage:(UIImage *) image
{   
    SplitImage *splitSystem = [[SplitImage alloc] initWithImage:image andPuzzleSize:gPuzzleSize];
    [splitSystem splitImageAndAddToView:self.gameView];
    [splitSystem release];
}

接下来,SplitImage 的初始化函数类和 splitImageAndAddToView功能:
- (id) initWithImage: (UIImage *) image andPuzzleSize: (int) pSize
{
    if((self = [super init]))
    {
        UIImage *aux = [[[UIImage alloc] init] autorelease];
        pieceCenterSize = [SplitImage puzzlePieceSizeForNumberOfPieces:pSize];

        UIImage *outSideBallSample = [UIImage imageNamed:@"sampleHorizontal.jpg"]; //convexity size for puzzle size
        outSideBallSample = [outSideBallSample resizedImageWithContentMode:UIViewContentModeScaleAspectFit bounds:CGSizeMake(pieceCenterSize, pieceCenterSize) interpolationQuality:kCGInterpolationHigh];      
    outSideBallSize = roundf(outSideBallSample.size.height);

        puzzleSize = pieceCenterSize * pSize;

        pieceNumber = pSize;

        if(image.size.height < puzzleSize || image.size.height > puzzleSize || image.size.width < puzzleSize || image.size.width > puzzleSize)
    {
        aux = [SplitImage resizeImageForPuzzle:image withSize:puzzleSize];
        aux = [SplitImage cropImageForPuzzle:aux withSize:puzzleSize];
    }

    aux = [aux imageWithAlpha];
    originalImage = [[UIImage imageWithCGImage:aux.CGImage] retain];

    mainImage = aux.CGImage;
    imageSize = CGSizeMake(aux.size.width, aux.size.height);
    NSLog(@"%@", NSStringFromCGSize(imageSize));

    splitImageSize = CGSizeMake(pieceCenterSize + 2*outSideBallSize, pieceCenterSize+2*outSideBallSize);
    }   
return self;
}



- (void) splitImageAndAddToView: (UIView *) view
{   
    for (int i = 0; i < pieceNumber; i++)
    for(int j = 0; j < pieceNumber; j++)

    //some code

    UIImage *mask;
    mask = [self randomlyRetriveMaskWithPrefix:1 forPieceAtI:i andJ:j];
    CGImageRef split = CGImageCreateWithImageInRect(mainImage, cuttingRect);

    PuzzlePiece *splitView = [[PuzzlePiece alloc] initWithImage:[UIImage imageWithCGImage:split] andMask:mask centerSize:pieceCenterSize objectMatrixPosition:i*pieceNumber+j outSideBallSize:outSideBallSize pieceType:pieceType pieceFrame:cuttingRect];

    [pieceFramesMatrix addObject:[NSValue valueWithCGRect:cuttingRect]];
    [splitView setTag:(i+1)*100+j];
    [view addSubview:splitView];
    CGImageRelease(split);
    [splitView release];
    }

谢谢,
安德烈

最佳答案

Objective-C 内存管理 101 在这里:

https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html

忽略垃圾收集的东西,它不适用于 iOS。

关于ios - Objective-C 内存管理问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5324616/

相关文章:

java - 就地复制位图会泄漏内存吗?

ios - NSMutableArray 中的 NSDictionary 在我的整个 for 循环中发生变化

iphone - 用于创建动态运动的算法资源

ios - 如何让 Facebook 广告上线?

c++ - 异步网络接口(interface)状态检查

c - 使用malloc有什么问题?

ios - 了解 iOS 上的 Instruments 内存分配日志

ios - 是否可以向没有来源的现有枚举添加案例?

ios - 证书中的 Bundle ID 与您输入的 Bundle ID 不匹配

c++ - 一些内存似乎在 malloc() 和 free() 之后分配