Iphone 从未调用nsobject dealloc
我在Iphone 从未调用nsobject dealloc,iphone,objective-c,xcode,automatic-ref-counting,dealloc,Iphone,Objective C,Xcode,Automatic Ref Counting,Dealloc,我在for循环的每次迭代中创建一个对象。但是,从未调用dealloc函数。它不应该在每次迭代时发布吗?我正在使用ARC,我已经停用了NSZombies。我也没有看到任何循环引用。从xcode运行内存泄漏工具不会显示任何泄漏,但是类的指针内存永远不会被释放,dealloc调用永远不会完成。你知道为什么会这样吗 谢谢大家! for(int i=0; i<10; i++) { //calculate the hog features of the image HogFeature
for
循环的每次迭代中创建一个对象。但是,从未调用dealloc函数。它不应该在每次迭代时发布吗?我正在使用ARC,我已经停用了NSZombies。我也没有看到任何循环引用。从xcode运行内存泄漏工具不会显示任何泄漏,但是类的指针内存永远不会被释放,dealloc
调用永远不会完成。你知道为什么会这样吗
谢谢大家!
for(int i=0; i<10; i++)
{
//calculate the hog features of the image
HogFeature *hogFeature = [self.image obtainHogFeatures];
if(i==0) self.imageFeatures = (double *) malloc(hogFeature.totalNumberOfFeatures*sizeof(double));
//copy the features
for(int j=0; j<hogFeature.totalNumberOfFeatures; j++)
self.imageFeatures[i*hogFeature.totalNumberOfFeatures + j] = hogFeature.features[j];
}
以及实施:
@implementation HogFeature
@synthesize totalNumberOfFeatures = _totalNumberOfFeatures;
@synthesize features = _features;
@synthesize dimensionOfHogFeatures = _dimensionOfHogFeatures;
- (void) dealloc
{
free(self.features);
free(self.dimensionOfHogFeatures);
NSLog(@"HOG Deallocation!");
}
@end
最后,在UIImage
类别中调用ActainHogFeatures
,如下所示:
- (HogFeature *) obtainHogFeatures
{
HogFeature *hog = [[HogFeature alloc] init];
[...]
return hog;
}
您可能希望用一个
@autoreleasepool{…}
来封闭内部循环,它告诉编译器何时进行处理,否则只有当控件返回到主循环时,池才会清空
for (i = 0; i < 10; i++) {
@autoreleasepool {
...
}
}
这些物体粘在一起的原因是它们没有被释放。我没有看到self.imageFeatures的声明-这是一个数组吗?如果将这些特性放入阵列中,则只要这些特性保留在阵列中或阵列本身未被释放,它们就不会被释放 我对使用C malloc和(尝试的)免费调用有点困惑。这里可能有一个我不知道的动机,但是,考虑到你提供的,我将这样写,如果交易没有按预期触发,我会感到惊讶:
NSMutableArray *features = [[NSMutableArray alloc] init];
for (int i = 0; i < 10; i++)
{
NSArray *hogFeatureArray = [[self image] obtainHogFeatures];
for (HogFeature *feature in hogFeatureArray)
{
[features addObject:hogFeature];
}
}
[self setImageFeatures:features];
假设您已将所有hog功能实例放入此imageFeatures数组中,它们将由该imageFeatures数组保留。为了观察dealloc的运行情况,需要做两件事之一:要么从数组中删除hog特性,要么释放数组本身(这可以通过将指针设置为nil来实现):
您的自我形象是否已发布/设置为零?你在哪里做的?你启用了ARC吗?问题中清楚地说明了ARC正在使用。如果你将函数名从
ActainHogFeatures
更改为createHogFeatures
,会发生什么情况?@iain:我同意,名称选择得不好。实际上,@autoreleasepool
s跳出循环,以便收集循环中分配的对象。这将产生10个不同的autoreleasepool调用。这似乎是他想要的…在循环的每个迭代结束时释放HogFeature。但是,是的,如果他不介意10个对象在for循环结束前一直挂着,你可以把@autoreleasepool
放在循环之外,这就是他想要的。我们无法使用该编译器指令控制自动释放池。如果他想要解除分配,他应该使用明确的释放。这将向autoreleasepool添加10个对象,但不能保证,比如说,任何时候都不会有多个对象处于活动状态。但我将编辑我的答案以突出您的观点,因为使用@autoreleasepool销毁每个循环迭代中的临时对象是有效的,这实际上是它存在的原因。把它放在循环之外是很奇怪的。self.imageFeatures是一个双数组(类似于C),它为不同的图像存储串联的hogFeature.features。它在第一段代码中分配。为了提高性能,我使用了类似double*C的数组,而不是NSMutableArray。我使用free来释放HogFeature对象的双数组。如果我在没有使用nsarray的情况下找不到答案,我将尝试您的方法。非常感谢。虽然C将为您提供更好的性能,但您需要负责管理内存,因为ARC只负责Objective-C对象。就我个人而言,我的建议是在Objective-C中实现这一点,并且只有在性能足够差的情况下才回到C(同样,我不知道您已经尝试了什么……但我们都知道过早优化的格言!)。祝你好运
@autoreleasepool {
for (i = 0; i < 10; i++) {
...
}
}
NSMutableArray *features = [[NSMutableArray alloc] init];
for (int i = 0; i < 10; i++)
{
NSArray *hogFeatureArray = [[self image] obtainHogFeatures];
for (HogFeature *feature in hogFeatureArray)
{
[features addObject:hogFeature];
}
}
[self setImageFeatures:features];
@property (nonatomic, retain) NSMutableArray *imageFeatures;
[self setImageFeatures:nil] // Previously assigned array now released
[[self imageFeatures] removeAllObjects]; // Works alternatively