0

I am trying to repair all the memory leaks in my app. I am having some issues with this line of code

[appDelegate setPendingConnectionsArray:[[NSArray alloc]initWithArray:[response JSONValue]]];

Here PendingConnectionsArray is an NSArray defined in appDelegate. When i analyze my app using analyzer tool of XCode. then it says potential leak of an object but no further information is there. How can i solve this leak issue.

Situation is same for this line too

phone_book_data.contact_image = [UIImage imageWithData:(NSData *)ABPersonCopyImageData(aSource)];

Thanks in advance.

4

3 回答 3

0

在您之前的示例中,我必须假设您没有使用 ARC。在这种情况下,您可以通过以下方式解决:

NSArray *array = [[NSArray alloc] initWithArray:[response JSONValue]];
[appDelegate setPendingConnectionsArray:array];
[array release];

或与:

[appDelegate setPendingConnectionsArray:[[[NSArray alloc]initWithArray:[response JSONValue]] autorelease]];

或者(如果JSONValue数组是不可变的):

[appDelegate setPendingConnectionsArray:[response JSONValue]];

在你的后一个例子中,如果它是 ARC,你只需转移所有权,然后让它清理:

phone_book_data.contact_image = [UIImage imageWithData:(NSData *)CFBridgingRelease(ABPersonCopyImageData(aSource))];

但在 MRC 中,您可能会这样做:

CFDataRef dataRef = ABPersonCopyImageData(aSource);
phone_book_data.contact_image = [UIImage imageWithData:(NSData *)(dataRef)];
CFRelease(dataRef);

或者

phone_book_data.contact_image = [UIImage imageWithData:[(NSData *)ABPersonCopyImageData(aSource) autorelease]];
于 2013-05-28T09:05:44.257 回答
0

你必须释放数组,因为你已经分配了它。

[appDelegate setPendingConnectionsArray:[[NSArray alloc]initWithArray:[response JSONValue]]autorelease];
于 2013-05-28T08:34:16.053 回答
0

您需要在 appDelegate的 dealloc 中释放PendingConnectionsArray

于 2013-05-28T08:32:10.527 回答