gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 20:48:47 26 4
gpt4 key购买 nike

我有一种方法来生成 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com