objective-c - IOS非ARC下 的 下面代码因为什么内存泄露,给个修改方案~plz
大家讲道理
大家讲道理 2017-04-17 17:09:19
0
1
785
    NSObject *obj1 = [[NSObject alloc]init];
    NSObject *obj2 = [[NSObject alloc]init];
   
    obj1 = [obj2 retain];
    [obj1 release];
    [obj2 release];
大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(1)
PHPzhong

This sentence in obj1 = [obj2 retain]; points the pointer of obj1 to obj2, so obj1The space that it originally pointed to was no longer taken care of, so it was leaked. obj1 = [obj2 retain];这一句,把obj1的指针指到obj2那块儿去了,所以obj1原来指的那块空间就没人管了,就泄漏了。

所以单就这个问题而言是obj1 = [obj2 retain];前面就要[obj1 release];

So as far as this issue is concerned, obj1 = [obj2 retain]; needs to be preceded by [obj1 release];. As for what it should be in terms of engineering specifications, I don’t know. It's clear, I haven't written much about non-ARC. If it’s not for an interview, try to use ARC. 🎜
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template