【发布时间】:2013-05-27 11:22:58
【问题描述】:
我正在尝试访问地址簿中的所有名字值字段。我正在为此使用此代码
CFStringRef firstName = ABRecordCopyValue(aSource, kABPersonFirstNameProperty);
first_name=[NSString stringWithFormat:@"%@",firstName];
CFRealease(firstname)
我没有使用 ARC。所以我最后需要 CFRealease(firstname) 。但是在我的代码中,当我添加 CFRealease(firstname) 时,我的应用程序此时崩溃,没有这个应用程序可以正常工作。
但是当我尝试使用分析器分析我的应用程序时,它说 object Leaked: object assigned and stored into 'firstname' is not refrenced later in the execution path and has a retain count of +1.
中间名和姓氏的大小写相同,其代码如下。
CFStringRef midname = ABRecordCopyValue(aSource, kABPersonMiddleNameProperty);
mid_name=[NSString stringWithFormat:@"%@",midname];
CFRelease(midname);
CFStringRef lastName = ABRecordCopyValue(aSource, kABPersonLastNameProperty);
last_name=[NSString stringWithFormat:@"%@",lastName];
CFRelease(lastName);
请告诉我哪里做错了。提前致谢。
【问题讨论】:
-
应用程序崩溃,因为您的 first_name 字符串正在存储一些字符串,并且在存储后您立即意识到它。而且您还必须在应用程序中使用 first_name,因此一旦您确定了 first_name,它就不会在内存池中获得任何分配。所以,试着在你不再需要它们的地方实现你的字符串。
-
你应该检查你的变量的写法(大写和小写),例如您正在使用 firstName 和 firstname。
-
@DeepakKhiwani 感谢您的回复,但是在我将 firstName 值发送到另一个 NSString 变量 first_name 之后我将释放 firstName,然后我释放了 firstName,因为现在我不再需要它了,不知何故我也有甚至在我不再需要它的 for 循环结束时尝试释放它,但这也不起作用。
-
@ReinhardMänner 这是一个拼写错误。对不起..
标签: iphone memory-management cfstring