iPhone应用内存泄漏问题

发布于 2024-11-03 03:23:57 字数 628 浏览 1 评论 0原文

我发现以下代码出现泄漏。

            cell.lblNoOfReplay.text=[NSString stringWithFormat:@"0 Replies. %@",(NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault, (CFStringRef)[[NSString stringWithFormat:@"Last message on %@",[BabbleVilleAppDelegate dateByAddingHours:Babbleoffset withDate:[[arrayPMMainList objectAtIndex:[indexPath section]] objectForKey:@"datetime"]]] stringByReplacingOccurrencesOfString:@"+" withString:@" "], CFSTR(""), kCFStringEncodingUTF8)];

这里我没有分配任何字符串,但是当我检查内存泄漏时,上面的行中有一些泄漏。 可能是由于 kCFAllocatorDefault 导致的,所以有些人遇到了同样的问题,请帮助我。

问候 姆鲁根

I am getting leaks on the following code.

            cell.lblNoOfReplay.text=[NSString stringWithFormat:@"0 Replies. %@",(NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault, (CFStringRef)[[NSString stringWithFormat:@"Last message on %@",[BabbleVilleAppDelegate dateByAddingHours:Babbleoffset withDate:[[arrayPMMainList objectAtIndex:[indexPath section]] objectForKey:@"datetime"]]] stringByReplacingOccurrencesOfString:@"+" withString:@" "], CFSTR(""), kCFStringEncodingUTF8)];

Here i dont havent allocated any string but when i check for memory leaks there are some leaks in the above line .
Probably it may be due to kCFAllocatorDefault ,so of someome has come across the same issues , help me out .

Regards
Mrugen

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人的旅程 2024-11-10 03:23:57

是的,您已经分配了一个字符串。 Core Foundation 对象遵循创建规则:通过名称包含 Create 或 Copy 的函数获得的任何对象都由调用者拥有,并且必须由调用者在使用完毕后释放。

将代码更改为:

CFStringRef s = CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault, (CFStringRef)[[NSString stringWithFormat:@"Last message on %@",[BabbleVilleAppDelegate dateByAddingHours:Babbleoffset withDate:[[arrayPMMainList objectAtIndex:[indexPath section]] objectForKey:@"datetime"]]] stringByReplacingOccurrencesOfString:@"+" withString:@" "], CFSTR(""), kCFStringEncodingUTF8);
cell.lblNoOfReplay.text=[NSString stringWithFormat:@"0 Replies. %@", (NSString *)s];
CFRelease(s);

另外,请考虑将该行分成多个部分和中间变量。其他看到该代码的人,包括未来的你,都会感谢你。

Yes, you have allocated a string. Core Foundation objects follow the Create rule: any object obtained via a function whose name contains either Create or Copy is owned by the caller and must be released by the caller when it’s finished using it.

Change your code to:

CFStringRef s = CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault, (CFStringRef)[[NSString stringWithFormat:@"Last message on %@",[BabbleVilleAppDelegate dateByAddingHours:Babbleoffset withDate:[[arrayPMMainList objectAtIndex:[indexPath section]] objectForKey:@"datetime"]]] stringByReplacingOccurrencesOfString:@"+" withString:@" "], CFSTR(""), kCFStringEncodingUTF8);
cell.lblNoOfReplay.text=[NSString stringWithFormat:@"0 Replies. %@", (NSString *)s];
CFRelease(s);

Also, consider breaking that line into multiple pieces and intermediate variables. Other people that see that code, including your future self, will thank you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文