zoukankan      html  css  js  c++  java
  • 自动释放对象和自动释放池(1)

    Autoreleasing Objects
    • Calling -autorelease flags an object to be sent release at some
    point in the future
    • Let’s you fulfill your retain/release obligations while allowing an
    object some additional time to live
    • Makes it much more convenient to manage memory
    • Very useful in methods which return a newly created object

    Method Names & Autorelease
    • Methods whose names includes alloc, copy, or new
    return a retained object that the caller needs to release

    NSMutableString *string = [[NSMutableString alloc] init];
    // We are responsible for calling -release or -autorelease
    [string autorelease];


    • All other methods return autoreleased objects

    NSMutableString *string = [NSMutableString string];
    // The method name doesn’t


    • This is a convention- follow it in methods you define!

    Hanging Onto an Autoreleased Object
    • Many methods return autoreleased objects
      ■ Remember the naming conventions...
      ■ They’re hanging out in the pool and will get released later
    • If you need to hold onto those objects you need to retain them
      ■ Bumps up the retain count before the release happens
    name = [NSMutableString string];
    // We want to name to remain valid!
    [name retain];
    // ...
    // Eventually, we’ll release it (maybe in our -dealloc?)
    [name release];

    Side Note: Garbage Collection
    • Autorelease is not garbage collection
    • Objective-C on iOS does not have garbage collection

  • 相关阅读:
    moco-globalsettings
    moco-简述
    stub和mock
    软件测试工作经验分享
    类、对象、方法、属性和实例变量
    你真的对 parseInt有足够的了解吗?
    PhoneGap开发环境搭建(记录一下,仅仅针对Android)
    360 前端面试题
    前端WEB开发工程师面试题-基础部分
    有意思的For循环
  • 原文地址:https://www.cnblogs.com/alexfan/p/2112915.html
Copyright © 2011-2022 走看看