English 中文(简体)
记忆管理何时使用释放
原标题:Memory management when to use release

我在 iOS 3. 0+ 中还有一个关于记忆管理的问题, 我知道如果我们做到了[变量释放] 将会释放它 保留到一个物体上, 但是这样的代码...

- (void) getPostsFromJson:(NSData *)data
{
    NSError *theError = nil;

    NSDictionary *dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:data error:&theError];

    if (dict == nil) {

        [SystemStatusValues getInstance].isValidJson = NO;
        [SystemStatusValues getInstance].httpStatus = HTTP_STATUS_FAILED;

        NSLog(@"getPostsFromJson - %@ %@",
              [theError localizedDescription],
              [[theError userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
    } else {

        NSArray *keys = [dict allKeys];        
        keys = [keys sortedArrayUsingFunction:keySortByInteger context:nil];

        self.keys = keys;
        self.posts = dict;
        NSLog(@"Posts : getPostsFromJson = %@", self.posts);

        if ([keys count] < TOTAL_SECTIONS) {

            [SystemStatusValues getInstance].isValidJson = NO;

        } else {

            [SystemStatusValues getInstance].isValidJson = YES;
            if (self.nextPosts == nil) {

            }

        }

        // [keys release];
        // [dict release];
        return;
    }
}

您可以看到有两个本地变量- 密钥和 dict, 我是否需要在代码结尾处调用 [ 密钥释放] 或 [ dict 释放]? 实际上, 我做到了, 但有时会粉碎它。 所以对于本地变量, 我们不需要在它使用后释放它?

最佳回答

命令和密钥都是自动释放对象的引用。 您应该释放它们, 这将导致您的程序终止 。 您没有被引用返回的物件, 您决不能放弃对您不拥有的天体的所有权 。

so 对于本地变量来说, 我们不需要在它被使用后释放它? 如果您拥有对象, 当不再需要它们时, 您必须释放它们, 不管它的本地变量还是 ivar 。

读此 - < a href=>"https:// developmenter.apple.com/library/ios/#document/Cocoa/Cocoa/Ceptitual/MemoryMgmt/arties/MemoryMgmt.html#/apple_ref/doc/uid/10000011-SW1, rel=“不跟随”>高级记忆管理规划指南

问题回答

< a href=> "http://whourtouseretaincount.com/" rel="no follow" > 它很简单 :

If you alloc, copy or retain an object you need to release it. Otherwise you don’t.

因此,你不需要释放钥匙或命令(它们都是自发的)。

不要释放 keys 数组。 因为 allKeys 方法可能返回自动释放的 NSAray 对象。 因此它会稍后通过系统发布。

似乎 desrializeAs Dictionary 也返回自动释放的 NSdictionary ,所以你也应该释放它。

仅以 alloc 创建的释放对象。 在这种情况下, 您正在使用外部方法。 您需要遵循该方法, 并查看该对象是否由 alloc 创建 。





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...