我的程序有内存泄漏
-(IBAction)play2;
{
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef =CFBundleCopyResourceURL(mainBundle,
(CFStringRef) @"Bear3", CFSTR ("wav"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
}
这给了我一个错误:
potential leak of an object allocated " CFBundleResourceURL
returns a Core Foundation object with a +1 retain count
-(IBAction)play2;
{
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef =CFBundleCopyResourceURL(mainBundle,
(CFStringRef) @"Bear3", CFSTR ("wav"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
}
This is giving me an error:
potential leak of an object allocated " CFBundleResourceURL
returns a Core Foundation object with a +1 retain count
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
AudioServicesCreateSystemSoundID (soundFileURLRef, &soundID); - 此处泄漏,因为创建添加了保留计数
使用 AudioServicesDisposeSystemSoundID 播放声音后
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID); - leak here, because create added to retain count
use AudioServicesDisposeSystemSoundID after playing sound
CFBundleCopyResourceURL
包含副本,因此您对soundFileURLRef
的保留计数实际上是 1。完成后,调用CFRelease(soundFileURLRef)
来减少您的保留数数。除了您收到的错误之外,SAKrisT 关于在使用
AudioServicesCreateSystemSoundID
创建的对象上调用AudioServicesDisposeSystemSoundID
的答案也是需要解决的问题。CFBundleCopyResourceURL
contains copy so your retain count onsoundFileURLRef
is in fact 1. When you are done with it callCFRelease(soundFileURLRef)
to decrement your retain count.In addition to the error you're getting, SAKrisT's answer about calling
AudioServicesDisposeSystemSoundID
on the object you created withAudioServicesCreateSystemSoundID
is also something to address.CFBundleCopyResourceURL
创建一个 您拥有的CFURLRef
对象,因此您需要在某个时刻使用CFRelease
。同样,您需要平衡对AudioServicesCreateSystemSoundID
的调用与对AudioServicesDisposeSystemSoundID
的另一个调用。对于 Core Foundation,名称中包含
Create
或Copy
一词的函数会返回您拥有的对象,因此在使用完该对象后,您必须放弃对该对象的所有权。有关 Core Foundation 内存管理的更多信息,请参阅 核心基础内存管理编程指南。只是为了给你一个提示,我可能会像这样处理内存管理(尽管我有一段时间没有编写 Objective-C 代码了)。这还假设您出于某种原因想要保留 URL 引用:
CFBundleCopyResourceURL
creates aCFURLRef
object that you own, so you need to relinquish ownership of this object at some point withCFRelease
. Similarly you will need to balance your call toAudioServicesCreateSystemSoundID
with another call toAudioServicesDisposeSystemSoundID
.For Core Foundation, functions that have the word
Create
orCopy
in their name return an object that you own, so you must relinquish ownership of it when you are done with it. For more information about Core Foundation memory management, see the Core Foundation Memory Management Programming Guide.Just to give you a hint, I would probably handle the memory management like this (although I haven't coded Objective-C for a while). This also assumes you want to keep the URL reference for whatever reason:
如果您不使用 ARC(在 xcode 4.2 中可用),那么您需要释放您分配的任何内容。在
[alert show]
之后添加[alert release]
。If you're not using ARC (available in xcode 4.2) then you need to release anything you alloc. add
[alert release]
after[alert show]
.每当您使用关键字“alloc”时,就意味着您正在为对象分配一些内存空间。现在,如果您不自己释放它或自动释放它,那么它会显示“内存泄漏”。它不仅涉及 uialertview,还涉及其他所有对象。
你可能想在dealloc()方法中释放alertview对象,但由于内存长时间未使用,仍然会出现内存泄漏。
所以,首先你通过[alert show]显示警报,然后你不再需要该对象,所以通过[alert release]释放它;
享受吧! :)
Whenever you use the keyword 'alloc' , it means you are allocating some memory space for your object. Now if you don't release it yourself or autorelease it, then it shows 'memory leak'. It is not only about uialertview, but for every other objects also.
You may want to release the alertview object in dealloc() method, but still it will show memory leak as the memory is unused for a long time.
So , first you show the alert by [alert show], then you need the object anymore, so release it by [alert release];
Enjoy !! :)