• 请不要在回答技术问题时复制粘贴 AI 生成的内容
wohenyingyu01
V2EX  ›  程序员

关于 objective c 的问题求教

  •  
  •   wohenyingyu01 · Apr 12, 2016 · 4269 views
    This topic created in 3703 days ago, the information mentioned may be changed or developed.
    在没有开 arc 的类里面写代码,新对象到底什么情况下才需要释放?比如

    NSString *something=@"something";

    NSDate *now=[NSDate date];

    NSString *string=[NSString stringWithFormat:@"xxxx"];

    NSData *data=[string dataUsingEncoding:NSUTF8StringEncoding];

    这些对象需要我用完手动 release 吗?貌似是编译时对象不需要手动 release ?
    22 replies    2016-04-13 14:50:16 +08:00
    zhangchioulin
        1
    zhangchioulin  
       Apr 12, 2016
    看到 alloc new 再释放
    wohenyingyu01
        2
    wohenyingyu01  
    OP
       Apr 12, 2016
    @zhangchioulin 我调用了一个带 arc 的类,类的创建方法名字里面不带 init ,但是我点进去看它就是 alloc init 了一个对象返回给我,我在不带 arc 的类里面获取到了这个对象,此时我需要 release 吗?
    noif
        3
    noif  
       Apr 12, 2016
    你主动 alloc new copy 的 release ,其它不用管
    noif
        4
    noif  
       Apr 12, 2016
    rerurn 一次 release 一次
    noif
        5
    noif  
       Apr 12, 2016
    建议去看下 引用计数机制 你就什么都懂了
    wohenyingyu01
        6
    wohenyingyu01  
    OP
       Apr 12, 2016
    @noif 好的吧,主要现在是 arc 和非 arc 代码混用,比较乱,在 arc 里面 alloc 传到非 arc 代码里的对象,应该也会自动 release 吧,因为没有在非 arc 代码里 retain ?
    lee0100317
        7
    lee0100317  
       Apr 12, 2016   ❤️ 1
    @wohenyingyu01 arc 里面的函数返回的类会自动添加 autorelease 。 mrc 代码里面只需要关注调用的函数是否包含 init copy 等关键字是否存在来判断计数值。
    weithl
        8
    weithl  
       Apr 12, 2016
    cmd+shift+i 跑一下,看到蓝色的就有问题了
    wohenyingyu01
        9
    wohenyingyu01  
    OP
       Apr 12, 2016
    @lee0100317 好的谢啦
    zymmm3
        10
    zymmm3  
       Apr 12, 2016
    如果有源代码,就把它自动转化为 arc
    LINAICAI
        11
    LINAICAI  
       Apr 12, 2016
    有 alloc 的才需要 release
    cheng4741
        12
    cheng4741  
       Apr 12, 2016
    你不用点进去看它是否调用 alloc init ,谁 alloc 了谁去释放。大家都遵守这个原则就行。
    wohenyingyu01
        13
    wohenyingyu01  
    OP
       Apr 12, 2016
    @zymmm3 有代码,量大,几十万行全是不带 arc 的,有风险不……
    zymmm3
        14
    zymmm3  
       Apr 12, 2016
    @wohenyingyu01 比用人脑想风险低。
    wohenyingyu01
        15
    wohenyingyu01  
    OP
       Apr 12, 2016
    @zymmm3 万一转失败我岂不是背锅了
    hyzjshwo
        16
    hyzjshwo  
       Apr 12, 2016
    arc 和 mrc 上面的代码不都不需要 release ,如果是 nsthread 子线程调用,需要加上 autoreleasepool 。上面的代码会在 funcation 结束的时候回收 autorelease 对象。
    frederick036
        17
    frederick036  
       Apr 12, 2016
    这些例子全都不需要手动释放。
    zymmm3
        18
    zymmm3  
       Apr 12, 2016
    ylovesy
        19
    ylovesy  
       Apr 12, 2016
    这些都有 autorelease 管理,不需要手动释放
    noif
        20
    noif  
       Apr 13, 2016
    @wohenyingyu01 建议有时间还是全部替换成 arc 吧 一劳永逸
    wohenyingyu01
        21
    wohenyingyu01  
    OP
       Apr 13, 2016
    @hyzjshwo 子线程调用会自动 retain 吗?应该要加上__block 声明的变量才会吧?没有__block 应该不需要 autoreleasepool
    hyzjshwo
        22
    hyzjshwo  
       Apr 13, 2016
    @wohenyingyu01 不会调用 retain ,老的 mrc 中 nsthread 会有 leaks ,需要套上 autoreleasepool 。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4893 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 93c8b4a0 · 49ms · UTC 01:07 · PVG 09:07 · LAX 18:07 · JFK 21:07
    ♥ Do have faith in what you're doing.