search

Home  >  Q&A  >  body text

objective-c - IOS非ARC下 的 下面代码因为什么内存泄露,给个修改方案~plz

    NSObject *obj1 = [[NSObject alloc]init];
    NSObject *obj2 = [[NSObject alloc]init];
   
    obj1 = [obj2 retain];
    [obj1 release];
    [obj2 release];
大家讲道理大家讲道理2885 days ago797

reply all(1)I'll reply

  • PHPz

    PHPz2017-04-17 17:11:19

    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. 🎜

    reply
    0
  • Cancelreply