C 배열을 사용할 때 코드가 누출되는 것으로 보이고 이유가 확실하지 않습니다.ARC를 사용하여 누설이 활성화되었습니다.
/* LeakyClass.m */
@property (nonatomic, assign) char **array1;
@property (nonatomic, assign) id __strong *array2;
@property (nonatomic, assign) unsigned short currentDictCount;
//...
- (id)initWithCapacity:(unsigned short)capacity
{
if ((self = [super init])) {
_array1 = (char **)calloc(sizeof(char*), capacity); //Leak
_array2 = (id __strong *)calloc(sizeof(id), capacity); //Leak
}
return self;
}
- (void)dealloc {
free(_array1);
free(_array2);
_array1 = NULL;
_array2 = NULL;
}
- (void)addObjectToArray2:(id)object andCharToArray1:(char *)str
{
[self array2][[self currentDictCount]] = object;
[self array1][[self currentDictCount]] = str;
[self setCurrentDictCount:[self currentDictCount] + 1];
}
@end
는 I이 함께 LeakyClass
전화 :기구는, 본 실시 예에서는 어레이 1. object
, @"example"
및 [NSURL URLWithString:exampleString]
을 추가 LeakyClass
에 전달 된 값을 각각 가리키는
/* OtherClass.m */
LeakyClass *leaky = [[LeakyClass alloc] initWithCapacity:20];
[leaky addObjectToArray2:object andCharToArray1:"1"]; // Leak
[leaky addObjectToArray2:@"example" andCharToArray1:"2"]; /Leak
[leaky addObjectToArray2:[NSURL URLWithString:exampleString] andCharToArray1:"3"]; /Leak
. 인스트루먼트는 _array1
및 _array2
에 대해서도 calloc
을 가리키고 있지만 둘 다 dealloc
에 둘 다 무료로 지정합니다.
내가 누락 된 항목이 있습니까? id
포인터로 __strong
를 사용하여 모든의
왜 'NSMutableArrays'를 사용하지 않습니까? – Ismael
뿐만 아니라 이것은 NSMutableDictionary의 기본 구현처럼 보입니다. 너 왜 이러는거야? – WDUK
사실 그는 객관적인 수업을 사용할 수는 있지만 초심자가 이러한 것들을 시도해 보는 것이 좋습니다. –