iphone - Instruments 说我有内存泄漏,但我没有看到它

标签 iphone objective-c xcode memory-management memory-leaks

我有一种方法来生成 Deck 对象(具有 NSMutableArray 属性的 NSObject 子类),并使用 Card 对象(具有一些整数和一个 NSString 属性的 UIView 子类)填充数组。当我请求一副牌时,我会检查是否已经存在一副牌(我认为),如果是,则在获取新牌之前释放它。

我的 View Controller 中的代码...

#import "FlashTestViewController.h"

@implementation FlashTestViewController

- (IBAction)generateDeck {

    if (aDeck != nil) {
        [aDeck release];
    }

    aDeck = [[Deck alloc] initDeckWithOperator:@"+"];
}


- (IBAction)generateCard {

    if (aCard != nil) {
        [aCard fadeAway];
    }

    aCard = [aDeck newCardFromDeck];
    [self.view addSubview:aCard];
}

- (void)fadeAway {
    [aCard removeFromSuperview];
    [aCard release];
    }

    @end

Deck类如下...

#import <Foundation/Foundation.h>
#import "Card.h"

@class Deck;

@interface Deck : NSObject {

    NSMutableArray* cards;
}

@property(nonatomic, retain) NSMutableArray* cards;

- (id)initDeckWithOperator: (NSString*)mathOper;
- (id)newCardFromDeck;

@end

- (id)initDeckWithOperator: (NSString*)mathOper {

    if (cards != nil) {
        [cards release];
    }
    cards = [[NSMutableArray alloc] init];
    for (int i=0; i<11; i++) {
        for (int j=0; j<11; j++) {
            int xPos = (random() % 220) + 10;
            int yPos = (random() % 360) + 10;
            Card* aCard = [[Card alloc] initWithFrame:CGRectMake(xPos, yPos, 60, 80)];
            aCard.upperOperand = i;
            aCard.lowerOperand = j;
            aCard.theOperator = mathOper;
            aCard.theResult = i + j;

            UITextView* upperTextView = [[UITextView alloc] initWithFrame:CGRectMake(5, 5, 50, 20)];
        NSString* upperOper = [[NSString alloc] initWithFormat:@"     %d", i];
        upperTextView.text = upperOper;
        [aCard addSubview:upperTextView];
        [upperTextView release];
        [upperOper release];

        UITextView* middleTextView = [[UITextView alloc] initWithFrame:CGRectMake(5, 30, 50, 20)];
        NSString* middleOper = [[NSString alloc] initWithFormat:@"%@  %d", mathOper, j];
        middleTextView.text = middleOper;
        [aCard addSubview:middleTextView];
        [middleTextView release];
        [middleOper release];

        UITextView* lowerTextView = [[UITextView alloc] initWithFrame:CGRectMake(5, 55, 50, 20)];
        NSString* lowerOper = [[NSString alloc] initWithFormat:@"     %d", j+i];
            lowerTextView.text = lowerOper;
            [aCard addSubview:lowerTextView];
            [lowerTextView release];
            [lowerOper release];

            [cards addObject: aCard];
            [aCard release];
        }
    }
    return self;
}

- (id)newCardFromDeck {
    int index = random() % [cards count];
    Card* selectedCard = [[cards objectAtIndex:index] retain];
    [cards removeObjectAtIndex:index];
    return selectedCard;
}

@end

当我从 newCardFromDeck 方法请求一张新卡时,我做了同样的事情并且它有效。有什么建议吗?

谢谢!

最佳答案

将此代码添加到您的 Deck.m 文件中:

- (void)dealloc
{
    [cards release];
    [super dealloc];
}

关于iphone - Instruments 说我有内存泄漏,但我没有看到它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3214327/

相关文章:

iphone - Objective-C:何时调用 self.myObject 与仅调用 myObject

iphone - UICollectionViewCell 选择行为

ios - UIImagePicker 中的图像方向不正确

xcode - 如何在 swift 中使用performSelector 的默认协议(protocol)实现?

iphone - 为什么我的对象没有被添加到数组中?

iphone - 从我的 iPhone 应用程序调用电话

c# - 如何为 iPhone 制作聊天客户端

ios - 注释是否会从可执行的 Objective-C 中删除

ios - AFNetworking:我的类 NSURLResponse 中的 setRedirectResponseBlock 总是返回空值

ios - swift 如何让动画在两个 UIView 之间翻转