Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 目标C:发布列表,是否会导致潜在泄漏?_Objective C_Memory Leaks_Release_Nsdictionary_Release Management - Fatal编程技术网

Objective c 目标C:发布列表,是否会导致潜在泄漏?

Objective c 目标C:发布列表,是否会导致潜在泄漏?,objective-c,memory-leaks,release,nsdictionary,release-management,Objective C,Memory Leaks,Release,Nsdictionary,Release Management,试图在网上找到答案,但无法找到。所以我想知道是否还有人知道,为什么 假设我有一个NSDictionary,或NSArray,其中存储了对象。如果我释放了NSDictionary,是否有潜在的泄漏,因为我没有释放NSDictionary列表中的对象 例如: NSDictionary *dict = [NSDictionary alloc] init]; // Create a bunch of objects, NSStrings, etc. // Store it into dict. [dic

试图在网上找到答案,但无法找到。所以我想知道是否还有人知道,为什么

假设我有一个
NSDictionary
,或
NSArray
,其中存储了对象。如果我释放了
NSDictionary
,是否有潜在的泄漏,因为我没有释放
NSDictionary
列表中的对象

例如:

NSDictionary *dict = [NSDictionary alloc] init];
// Create a bunch of objects, NSStrings, etc.
// Store it into dict.
[dict release];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];

MyObject *obj = [[MyObject alloc] init];
[dict setObject:obj forKey:@"foo"];  // the dictionary retains "obj"
[obj release];  // this matches the "alloc/init"
                // but "obj" still is retained by the dictionary

[dict release];  // now "obj" gets released
这是否也会释放dict中的所有内容?(对象、字符串等)。

提前感谢大家

当添加和释放
NSDictionary
NSArray
或销毁列表时,会自动保留
NSDictionary或
NSArray
中的所有项目

例如:

NSDictionary *dict = [NSDictionary alloc] init];
// Create a bunch of objects, NSStrings, etc.
// Store it into dict.
[dict release];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];

MyObject *obj = [[MyObject alloc] init];
[dict setObject:obj forKey:@"foo"];  // the dictionary retains "obj"
[obj release];  // this matches the "alloc/init"
                // but "obj" still is retained by the dictionary

[dict release];  // now "obj" gets released

在NSDictionary或NSArray上执行发布时,只要数组中对象的保留计数为1(这意味着只要在将对象插入数据结构后释放了对象),那么一旦释放字典或数组,这些对象也将被释放