gpt4 book ai didi

iphone - 为 Objective-C 数据对象分配内存的问题

转载 作者:行者123 更新时间:2023-12-03 17:06:48 25 4
gpt4 key购买 nike

我已经用 Objective-C 编程几个月了,到目前为止,我已经做得很好了,无需提出任何问题。这将是我的第一次。问题是我在其方法之一中收到来自数据对象的内存泄漏警告。我可以看到问题是我向它发送了一个分配而不释放它,但我不知道如何让它将对象保留在内存中。如果我取出分配,程序就会崩溃。如果我把它留在里面,它就会泄漏内存。这是有问题的方法:

+ (id) featureWithID:(int)fID name:(NSString*)fName secure:(int)fSecure {
Feature *newFeature = [[self alloc] init];
newFeature.featureID = fID;
newFeature.featureName = fName;
newFeature.featureSecure = fSecure;

return [newFeature autorelease];

}

此方法由我的 View Controller 中的另一个方法调用。该方法如下:

+ (NSMutableArray*) createFeatureArray {

NSString *sqlString = @"select id, name, secure from features";
NSString *file = [[NSBundle mainBundle] pathForResource:@"productname" ofType:@"db"];
sqlite3 *database = NULL;
NSMutableArray *returnArray = [NSMutableArray array];

if(sqlite3_open([file UTF8String], &database) == SQLITE_OK) {

const char *sqlStatement = [sqlString UTF8String];
sqlite3_stmt *compiledStatement;

if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {

while(sqlite3_step(compiledStatement) == SQLITE_ROW) {

Feature *myFeature = [Feature featureWithID:sqlite3_column_int(compiledStatement,0)
name:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]
secure:sqlite3_column_int(compiledStatement,2)];

[returnArray addObject:myFeature];

}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
return returnArray;

}

我尝试了几种方法,例如创建一个 featureWithFeature 类方法,这将允许我在调用方法中分配 init 功能,但这也会导致程序崩溃。

如果您需要任何说明或代码的任何其他部分,请告诉我。预先感谢您的帮助。

更新:2011 年 4 月 14 日

阅读前两个回复后,我实现了该建议,发现程序现在崩溃了。我完全不知道如何追查罪魁祸首。希望这有帮助,我也发布了 View Controller 的调用方法:

- (void)setUpNavigationButtons {
// get array of features from feature data controller object
NSArray *featureArray = [FeatureController createFeatureArray];
int i = 0;


for (i = 0; i < [featureArray count]; i++) {
Feature *myFeature = [featureArray objectAtIndex:i];
CGRect buttonRect = [self makeFeatureButtonFrame:[featureArray count] withMember:i];

UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton setFrame:buttonRect];

[aButton addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
[aButton setTitle:[NSString stringWithFormat:@"%@",myFeature.featureName] forState:UIControlStateNormal];
aButton.tag = myFeature.featureID;

[self.view addSubview:aButton];

}

}

注意:这些方法的发布顺序与其调用顺序相反。最后一个方法调用第二个方法,第二个方法又调用第一个方法。

更新:我已经更新了这些函数以显示其中的内容:下面,我将发布该对象的头文件 - 也许这会有所帮助

@interface Feature : NSObject {
int featureID;
int featureSecure;
NSString *featureName;
}

@property (nonatomic, assign) int featureID;
@property (nonatomic, assign) int featureSecure;
@property (nonatomic, retain) NSString *featureName;

- (id) init;

- (void) dealloc;

+ (id) featureWithID:(int)fID name:(NSString*)fName secure:(int)fSecure;



@end

@interface FeatureController : NSObject {

}


- (id) init;

- (void) dealloc;

+ (NSMutableArray*) createFeatureArray;

+ (Feature*) getFeatureWithID:(int)fetchID;

@end

最佳答案

便捷方法应遵循返回自动释放对象的约定。更改此:

+ (id) featureWithID:(int)fID name:(NSString*)fName secure:(int)fSecure {
Feature *newFeature = [[self alloc] init];
...
return newFeature;
}

至:

+ (id) featureWithID:(int)fID name:(NSString*)fName secure:(int)fSecure {
Feature *newFeature = [[self alloc] init];
...
return [newFeature autorelease];
}

关于iphone - 为 Objective-C 数据对象分配内存的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5671229/

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