English 中文(简体)
如何在没有记忆泄露的情况下归还CFDataRef?
原标题:How to return CFDataRef without memory leak?[ios]

当我返回CFDataRef时。 页: 1

(CFDataRef)MyFunction{
    .....
    CFDataRef data = CFDataCreate(NULL, buf, bufLen);
    free(buf);
    return data;
}

There is a memory leak, how to make CFDataRef autorelease? the method [data autorelease] doesn t exit.

最佳回答

你们可以自行释放核心基金会的目标。 (然而,你可以自动释放支持免费桥梁的核心基金会物体,如CFDataRef;见下文@newacct的答复)。)

目标C公约将指明您的方法,从<代码>新开始,以表明打电话者负责发放其返回价值。 例如:

+ (CFDataRef)newDataRef {
    return CFDataCreate(...);
}

CFDataRef myDataRef = [self newDataRef];
...
CFRelease(myDataRef);

如果你遵守这一命名公约,Xcode的静态分析器将正确地标明核心基金会的记忆问题。

问题回答

how to make CFDataRef autorelease? the method [data autorelease] doesn t exit.

简单地把它放在一个目标点上,以便打电话autorelease:

-(CFDataRef)MyFunction{
    .....
    CFDataRef data = CFDataCreate(NULL, buf, bufLen);
    free(buf);
    return (CFDataRef)[(id)data autorelease];
}




相关问题
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, ...

热门标签