- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Three20/TTThumbsviewcontroller 加载照片。很长一段时间以来,我一直在努力解决设置照片源时的内存泄漏问题。我是 Object C 和 iOS 内存管理的初学者。请查看以下代码,并提出任何明显的错误或声明和释放变量的任何错误。
-- PhotoViewController.h
@interface PhotoViewController : TTThumbsViewController <UIPopoverControllerDelegate,CategoryPickerDelegate,FilterPickerDelegate,UISearchBarDelegate>{
......
NSMutableArray *_photoList;
......
@property(nonatomic,retain) NSMutableArray *photoList;
-- PhotoViewController.m
@implementation PhotoViewController
....
@synthesize photoList;
.....
- (void)LoadPhotoSource:(NSString *)query:(NSString *)title:(NSString* )stoneName{
NSLog(@"log- in loadPhotosource method");
if (photoList == nil)
photoList = [[NSMutableArray alloc] init ];
[photoList removeAllObjects];
@try {
sqlite3 *db;
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *dbPath = [documentsPath stringByAppendingPathComponent: @"DB.s3db"];
BOOL success = [fileMgr fileExistsAtPath:dbPath];
if(!success)
{
NSLog(@"Cannot locate database file '%@'.", dbPath);
}
if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
{
NSLog(@"An error has occured.");
}
NSString *_sql = query;
const char *sql = [_sql UTF8String];
sqlite3_stmt *sqlStatement;
if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
{
NSLog(@"Problem with prepare statement");
}
if ([stoneName length] != 0)
{
NSString *wildcardSearch = [NSString stringWithFormat:@"%@%%",[stoneName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
sqlite3_bind_text(sqlStatement, 1, [wildcardSearch UTF8String], -1, SQLITE_STATIC);
}
while (sqlite3_step(sqlStatement)==SQLITE_ROW) {
NSString* urlSmallImage = @"Mahallati_NoImage.png";
NSString* urlThumbImage = @"Mahallati_NoImage.png";
NSString *designNo = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,2)];
designNo = [designNo stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *desc = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,7)];
desc = [desc stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *caption = designNo;//[designNo stringByAppendingString:desc];
caption = [caption stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *smallFilePath = [documentsPath stringByAppendingPathComponent: [NSString stringWithFormat:@"Small%@.JPG",designNo] ];
smallFilePath = [smallFilePath stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([fileMgr fileExistsAtPath:smallFilePath]){
urlSmallImage = [NSString stringWithFormat:@"Small%@.JPG",designNo];
}
NSString *thumbFilePath = [documentsPath stringByAppendingPathComponent: [NSString stringWithFormat:@"Thumb%@.JPG",designNo] ];
thumbFilePath = [thumbFilePath stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([fileMgr fileExistsAtPath:thumbFilePath]){
urlThumbImage = [NSString stringWithFormat:@"Thumb%@.JPG",designNo];
}
NSNumber *photoProductId = [NSNumber numberWithInt:(int)sqlite3_column_int(sqlStatement, 0)];
NSNumber *photoPrice = [NSNumber numberWithInt:(int)sqlite3_column_int(sqlStatement, 6)];
char *productNo1 = sqlite3_column_text(sqlStatement, 3);
NSString* productNo;
if (productNo1 == NULL)
productNo = nil;
else
productNo = [NSString stringWithUTF8String:productNo1];
Photo *jphoto = [[[Photo alloc] initWithCaption:caption
urlLarge:[NSString stringWithFormat:@"documents://%@",urlSmallImage]
urlSmall:[NSString stringWithFormat:@"documents://%@",urlSmallImage]
urlThumb:[NSString stringWithFormat:@"documents://%@",urlThumbImage]
size:CGSizeMake(123, 123)
productId:photoProductId
price:photoPrice
description:desc
designNo:designNo
productNo:productNo
] autorelease];
[photoList addObject:jphoto];
[jphoto release];
}
}
@catch (NSException *exception) {
NSLog(@"An exception occured: %@", [exception reason]);
}
self.photoSource = [[[MockPhotoSource alloc]
initWithType:MockPhotoSourceNormal
title:[NSString stringWithFormat: @"%@",title]
photos: photoList
photos2:nil] autorelease];
}
使用不同的查询再次调用上述 LoadPhotosource 方法时会发生内存泄漏...我觉得声明 NSMutableArray (photoList) 有问题,但无法弄清楚如何修复内存泄漏。非常感谢任何建议。
最佳答案
我没有在您的代码中看到任何泄漏的对象,但实际上是双重释放的对象,最终会使您的应用程序崩溃:
在你的最后几行中,你有这个:
Photo *jphoto = [[[Photo alloc] initWithCaption:caption
urlLarge:[NSString stringWithFormat:@"documents://%@",urlSmallImage]
urlSmall:[NSString stringWithFormat:@"documents://%@",urlSmallImage]
urlThumb:[NSString stringWithFormat:@"documents://%@",urlThumbImage]
size:CGSizeMake(123, 123)
productId:photoProductId
price:photoPrice
description:desc
designNo:designNo
productNo:productNo
] autorelease];
[photoList addObject:jphoto];
[jphoto release];
如果你仔细观察,你有一个双重释放(alloc 中的自动释放和 addObject 之后的释放)。您应该删除其中一个。
除此之外,我还向您推荐一些东西:
删除 NSMutableArray *_photoList;
,您没有使用它,因为您将属性合成器声明为 @synthesize photoList;
。此外,正如罗伯特在下面评论的那样,您应该在使用该属性时使用这种形式来明确区分:@synthesize photoList = photoList_;
下划线放在最后更好,因为 Apple 在开头使用它,您不想不小心使用相同的变量名。
使用属性访问器来管理 ivar。而不是这样做:
如果(照片列表 == 无)
photoList = [[NSMutableArray alloc] init ];
试试这个:
if (photoList == nil)
self.photoList = [[[NSMutableArray alloc] init] autorelease];
在您的示例中,两行具有相同的行为,但第二行更可取,因为它将依赖于您的可变内存策略。如果你有一天通过副本或分配更改你的保留,你的代码仍然可以毫无问题地工作。以同样的方式,通过为你的 ivar 分配一个 nil 来释放你的内存 self.photoList = nil
为什么你认为你有泄漏的对象?
关于ios - 重新加载对象时 NSMutableArray 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11072382/
在 viewDidLoad 上,我的应用程序填充 NSMutableArray arrayOfHaiku,它是 NSDictionary 项的数组,如下所示: NSError *error; NS
我在 NSMutableArray 上是这样的: NSMutableArray *array = [NSMutableArray arrayWithObjects:@"0",@"0",@"0",@"0
我正在尝试设置我的 allArticlesArray 的内容,然后将附加对象附加到数组中。这是我的代码片段: [self.allArticlesArray setArray:newsArray]; [
我用 NSMutableArray 中的数据填充了 TableView ,一切正常。当我选择一个单元格(didSelectRowAtIndex)时,该项目将从数组中删除,正如它应该做的那样。现在我希望
我有一个 NSMutableArray 是我的代表,我也在我的一个 View Controller 中使用它。 所以在 viewDidLoad 我像这样制作我的 NSMutableArray 的可变副
我有 100 个元素的 NSMutableArray。 我想将“20 - 50”、“10 - 20”、“60 - 100”和 50 - 60. 元素加载到单独的 NSMutableArray 中,并将
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 9 年前。 Improve t
我在尝试将字符串的 NSMutableArray 共享给另一个类时遇到了一些问题。我有一个 tableView,其中填充了 Strings,我想将其添加到 NSMutableArray。然后在另一个
正如我们所知,数组是一个连续的内存分配。那么 NSMutableArray 大小将如何增加。 最佳答案 C 数组确实使用连续内存,C++ std::vector 也是如此,但是 NSMutableAr
我需要对一个数组进行排序,里面有一个数组,像这样: NSMutableArray * array_example = [[NSMutableArray alloc] init]; [array_e
这看起来很简单,但我只是得到一个空数组。 我想获取一个按下的按钮,找到它旁边的所有按钮(参见代码中的注释),并将所有找到的按钮添加到另一个 NSMutableArray。 然后我想遍历该数组并对每个按
我正在尝试在另一个 NSMutableArray 中添加 NSMutableArray。但我想做的是嵌套数组。 我当前的代码是: NSMutableArray *array1 = [NSMutab
我有三个类,即DepartmentViewController、DepartmentRequest、Department。这就像我从 DepartmentViewcontroller 发出部门请求,并
我正在尝试将 NSMutableArray 存储在 NSMutableArray 中。所以 var all:NSMutableArray = NSMutableArray() let localArr
这一定是那些错误之一,您一直盯着代码看很长时间,以至于找不到错误。 我有这个代码块,我在其中循环遍历一个包含多个 NSMutableArray 的 NSMutableArray: // FoodVi
我有一个包含 50 个条目的 NSMutableArray - 有什么简单的方法可以将其分成 5 个 NSMutableArray,每个 10 个条目。 最佳答案 是的,要划分 NSMutableAr
我想将 NSMutableArray 的 addObject 放入 NSMutableArray 中,那么我如何快速实现这一点。并检索它,所以请帮助我,因为现在我正在快速学习。 间接说我要二维数组是指
好吧: 我有 NSMutableArray 1 我还有 NSMutableArray 2 我想从数组 1 中删除与数组 2 中的对象匹配的所有对象。 有什么想法吗? 最佳答案 我刚刚打开文档,打印 N
我正在尝试制作 NSMutableArray 的深拷贝,其对象是与此类似的自定义类的实例: @interface CustomParent : NSObject @property NSInteger
我知道你们可能会觉得这是一个重复的问题。但我对我的项目的这个问题搞砸了。 现在转到这个问题,我已经采取了NSMutableArray命名为 arr1包含字符串值,例如 @"34" , @"40"等等。
我是一名优秀的程序员,十分优秀!