仪器说我有内存泄漏,但是我看不到 [英] Instruments says I have a memory leak, but I don't see it

查看:61
本文介绍了仪器说我有内存泄漏,但是我看不到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a method to generate a Deck object (NSObject subclass with an NSMutableArray property) and it populates the array with Card objects (UIView subclass with some integer and one NSString property). When I ask for a Deck I check to see if one already exists (I think) and if so, release it before getting a new one.

我的视图控制器中的代码...

The code from my viewcontroller...

#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类如下...


The Deck class is as follows...

#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方法请求一张新卡时,我做同样的事情,并且它可以工作.有什么建议吗?


I do the same thing when I ask for a new card from the newCardFromDeck method and it works. Any suggestions?

谢谢!

推荐答案

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

Add this code to your Deck.m file:

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

这篇关于仪器说我有内存泄漏,但是我看不到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆