ios - XCode 内存泄漏问题
<p><p>我的项目接近尾声,但在 XCode 中分析我的项目后,它向我表明这一行存在内存泄漏:</p>
<p> <img src="/image/8Y0p2.png" alt="http://i.imgur.com/uTkbA.png"/> </p>
<p>以下是相关代码的文字版:</p>
<pre><code>- (void)displayPerson:(ABRecordRef)person
{
NSString* firstName = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *lastName = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);
NSMutableString *fullName = ;
//NSLog(@"%@", fullName);
NSString* phoneNum = nil;
ABMultiValueRef phoneNumbers;
phoneNumbers = ABRecordCopyValue(person,
kABPersonPhoneProperty);
if (ABMultiValueGetCount(phoneNumbers) > 0) {
phoneNum = (__bridge_transfer NSString*) ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
} else {
phoneNum = @"Unknown";
}
NSLog(@"First name is %@ and last name is %@", firstName, lastName);
NSLog(@"Phone is %@", phoneNum);
phoneNum = ;
phoneNum = ;
</code></pre>
<p>谁能帮我解决这个问题?我不相信这会造成严重后果,但我不想给苹果一个理由拒绝我的应用程序从商店。谢谢。</p>
<p>最佳...SL</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>除了 <code>ABRecordCopyValue</code> 的 <code>phoneNumbers</code> 返回值之外,您在任何地方都在使用 <code>__bridge_transfer</code>。</p>
<p>您需要将<code>phoneNumbers</code>的所有权转让给ARC或手动释放内存。</p>
<p><strong>更新:</strong>仔细研究了这个问题,我不确定您是否可以将所有权转让给 ARC,请参阅 <a href="https://stackoverflow.com/questions/9217548/bridge-transfer-and-abrecordcopyvalue-and-arc" rel="noreferrer noopener nofollow">__bridge_transfer and ABRecordCopyValue: and ARC</a>了解更多详情。</p>
<p>添加<code>CFRelease(phoneNumbers)</code>会手动释放内存。</p>
<p>例如:</p>
<pre><code>NSString* phoneNum = nil;
ABMultiValueRef phoneNumbers;
phoneNumbers = ABRecordCopyValue(person,
kABPersonPhoneProperty);
if (ABMultiValueGetCount(phoneNumbers) > 0) {
phoneNum = (__bridge_transfer NSString*) ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
} else {
phoneNum = @"Unknown";
}
CFRelease(phoneNumbers);
</code></pre></p>
<p style="font-size: 20px;">关于ios - XCode 内存泄漏问题,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/14330860/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/14330860/
</a>
</p>
页:
[1]