English 中文(简体)
挫败潜在记忆泄漏
原标题:ios thwarting a potential memory leak
- (NSString*)encodeURL:(NSString *)string
{
    NSString *newString = (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef)string, NULL, CFSTR(":/?#[]@!$ & ()*+,;="<>%{}|\^~`"), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));

    if (newString) 
    {
        return newString; // <-- potential leak here
    }

    return @"";
}

我不熟悉CFTypes(不了解他们的情况)。 这是我从互联网上传来的法典,它不得不冒着在阿农研中心工作的余地。 I m 得到潜在的泄漏警报,I m 无法确定如何予以确定。 建议

最佳回答

是的,这是一种记忆泄露。 您打算使用<代码>CFBridgingRelease(,而不是_bridge

由<代码>CFURLCreateStringByAddingPercentEfalls创建的物体因包括<代码>Create而被额外保留。 你们需要把该物体转让给非洲排雷中心,让它知道增加额外释放,) 就是这样做的。

NSString *newString = 
    CFBridgingRelease(
        CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, 
                                               (__bridge CFStringRef)string, 
                                               NULL,
                                               CFSTR(":/?#[]@!$ & ()*+,;="<>%{}|\^~`"),           
                                               kCFStringEncodingUTF8));

您确实使用<条码>>桥,用于在座标,因为您没有将其转至核心基金会。 你只是要求核心基金会在非洲资源中心继续拥有它的时候使用它。 当你“转让”所有权时,你通常指的是“该物体被用作核心基金会,现在则指ARC”(或反之亦然)。 这符合<编码>新标准

我放弃了长期的国家合作框架编码功能,结果只是缩短了。

问题回答

return a autoreleased version of the string, CFStringRef can be converted to NSString and vice versa, which also means that you can treat it like a normal NSString and thus autorelease it (if you are interested in this you should read about the toll free bridging between parts of Foundation and Core Foundation)

另一种方式是使用__bridge_ transfer,而不是像......bridge:

NSString *newString = (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef)string, NULL, CFSTR(":/?#[]@!$ & ()*+,;="<>%{}|\^~`"), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));

See more information in this question: ARC:





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

热门标签