- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我的 ABAddressBookGetPersonWithRecordID 和 ABPersonSetImageData 都出现了一些内存泄漏。在发帖之前我一直在寻找解决方案,但我仍然不明白。如果我用 iPhone 3GS 玩了很长时间,或者只用 iPhone 3G 玩了几次,实际上应用程序会崩溃。这是我在 didSelectRowAtIndexPath 方法中的代码。我已经看过这些方法的示例代码,但我没有看到我遗漏了什么。先感谢您。 (抱歉有错误...)
Contact *myContact = [fetchedResultsController objectAtIndexPath:indexPath];
cancelCreateContact = NO;
ABAddressBookRef ab = ABAddressBookCreate();
int len = ABAddressBookGetPersonCount(ab);
ABRecordID contactID;
ABRecordRef person;
BOOL alreadyExists = NO;
CFStringRef first, last;
for(int i = 1; i < (len + 1); i++)
{
person = ABAddressBookGetPersonWithRecordID(ab, (ABRecordID)i);
if(!person){
len++;
continue;
}
first = ABRecordCopyValue(person, kABPersonFirstNameProperty);
last = ABRecordCopyValue(person, kABPersonLastNameProperty);
if ([[(NSString*)first lowercaseString] isEqualToString:[myContact.firstname lowercaseString]] && [[(NSString*)last lowercaseString] isEqualToString:[myContact.lastname lowercaseString]]) {
alreadyExists = YES;
contactID = ABRecordGetRecordID(person);
break;
}
}
if (alreadyExists) {
//NSLog(@"already exists");
ABRecordRef aContactFound = ABAddressBookGetPersonWithRecordID(ab, contactID);
ABRecordRef aRecord = ABPersonCreate();
CFErrorRef anError = NULL;
CFStringRef firstname = ABRecordCopyValue(aContactFound, kABPersonFirstNameProperty);
ABRecordSetValue(aRecord, kABPersonFirstNameProperty, firstname, &anError);
CFRelease(firstname);
CFStringRef lastname = ABRecordCopyValue(aContactFound, kABPersonLastNameProperty);
ABRecordSetValue(aRecord, kABPersonLastNameProperty, lastname, &anError);
CFRelease(lastname);
CFStringRef job = ABRecordCopyValue(aContactFound, kABPersonJobTitleProperty);
ABRecordSetValue(aRecord, kABPersonJobTitleProperty, job, &anError);
CFRelease(job);
ABMultiValueRef instantMessage = ABRecordCopyValue(aContactFound, kABPersonInstantMessageProperty);
ABRecordSetValue(aRecord, kABPersonInstantMessageProperty, instantMessage, &anError);
CFRelease(instantMessage);
ABMultiValueRef phone = ABRecordCopyValue(aContactFound, kABPersonPhoneProperty);
ABRecordSetValue(aRecord, kABPersonPhoneProperty, phone, &anError);
CFRelease(phone);
ABMultiValueRef email = ABRecordCopyValue(aContactFound, kABPersonEmailProperty);
ABRecordSetValue(aRecord, kABPersonEmailProperty, email, &anError);
CFRelease(email);
CFDataRef imageData = ABPersonCopyImageData(aContactFound);
ABPersonSetImageData(aRecord, imageData, &anError);
ABAddressBookSave(ab, &anError);
CFRelease(imageData);
ABUnknownPersonViewController *ABView = [[ABUnknownPersonViewController alloc] init];
ABView.unknownPersonViewDelegate = self;
ABView.displayedPerson = aRecord;
ABView.allowsAddingToAddressBook = NO;
ABView.allowsActions = YES;
ABView.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:ABView animated:YES];
[ABView release];
CFRelease(aRecord);
}else{
//NSLog(@"doesn't exist");
//sinon ouvre une fiche pré-remplie
ABRecordRef aRecord = ABPersonCreate();
CFErrorRef anError = nil;
if(![myContact.firstname isEqualToString:@""]) ABRecordSetValue(aRecord, kABPersonFirstNameProperty, myContact.firstname, &anError);
if(![myContact.lastname isEqualToString:@""]) ABRecordSetValue(aRecord, kABPersonLastNameProperty, myContact.lastname, &anError);
if(![myContact.email isEqualToString:@""]) {
ABMultiValueRef ABemail = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(ABemail, myContact.email, kABWorkLabel, NULL);
ABRecordSetValue(aRecord, kABPersonEmailProperty, ABemail, &anError);
CFRelease(ABemail);
}
if(![myContact.phone_business isEqualToString:@""] || ![myContact.phone_mobile isEqualToString:@""]){
ABMultiValueRef ABphones = ABMultiValueCreateMutable(kABMultiStringPropertyType);
if(![myContact.phone_business isEqualToString:@""]) ([myContact.phone_business stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]].length == 4 || [myContact.phone_business stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]].length == 5) ? ABMultiValueAddValueAndLabel(ABphones, [NSString stringWithFormat:@"014443%@", myContact.phone_business], kABPersonPhoneMainLabel, NULL) : ABMultiValueAddValueAndLabel(ABphones, myContact.phone_business, kABPersonPhoneMainLabel, NULL);
if(![myContact.phone_mobile isEqualToString:@""] && ([myContact.phone_mobile stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]].length == 10)) ABMultiValueAddValueAndLabel(ABphones, myContact.phone_mobile, kABPersonPhoneMobileLabel, NULL);
ABRecordSetValue(aRecord, kABPersonPhoneProperty, ABphones, &anError);
CFRelease(ABphones);
}
if(![myContact.job isEqualToString:@""]) ABRecordSetValue(aRecord, kABPersonJobTitleProperty, myContact.job, &anError);
if(![myContact.msn isEqualToString:@""] || ![myContact.twitter isEqualToString:@""] || ![myContact.facebook isEqualToString:@""]){
ABMultiValueRef ABmessaging = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
NSMutableDictionary *dMessaging;
if(![myContact.msn isEqualToString:@""]){
dMessaging = [[NSMutableDictionary alloc] init];
[dMessaging setObject:myContact.msn forKey:(NSString *) kABPersonInstantMessageUsernameKey];
[dMessaging setObject:@"MSN" forKey:(NSString *)kABPersonInstantMessageServiceKey];
ABMultiValueAddValueAndLabel(ABmessaging, dMessaging, kABPersonInstantMessageServiceMSN, NULL);
[dMessaging release];
}
if(![myContact.twitter isEqualToString:@""]){
dMessaging = [[NSMutableDictionary alloc] init];
[dMessaging setObject:myContact.twitter forKey:(NSString *) kABPersonInstantMessageUsernameKey];
[dMessaging setObject:@"Twitter" forKey:(NSString *)kABPersonInstantMessageServiceKey];
ABMultiValueAddValueAndLabel(ABmessaging, dMessaging, kABOtherLabel, NULL);
[dMessaging release];
}
if(![myContact.facebook isEqualToString:@""]){
dMessaging = [[NSMutableDictionary alloc] init];
[dMessaging setObject:myContact.facebook forKey:(NSString *) kABPersonInstantMessageUsernameKey];
[dMessaging setObject:@"Facebook" forKey:(NSString *)kABPersonInstantMessageServiceKey];
ABMultiValueAddValueAndLabel(ABmessaging, dMessaging, kABOtherLabel, NULL);
[dMessaging release];
}
ABRecordSetValue(aRecord, kABPersonInstantMessageProperty, ABmessaging, &anError);
CFRelease(ABmessaging);
}
//pas dans l'XMLToObjectParser parce que ça prenait une plombe...
NSURL *url = [NSURL URLWithString:myContact.picture_path];
NSData *data = [NSData dataWithContentsOfURL:url];
if(!data){
NSString *picture_path = (![myContact.gender isEqualToString:@""]) ? [NSString stringWithFormat:@"default_%@_head.png", [myContact.gender lowercaseString]] : @"default_m_head.png";
[myContact setPicture_path:picture_path];
NSError *error = nil;
if(![self.managedObjectContext save:&error]){
NSLog(@"pb lors de l'enregistrement de picture path");
}
//NSData *localData = [NSData dataWithContentsOfFile:myContact.picture_path];
UIImage *image = [UIImage imageNamed:picture_path];
NSData *localData = UIImagePNGRepresentation(image);
CFDataRef cfLocalData = CFDataCreate(NULL, [localData bytes], [localData length]);
ABPersonSetImageData(aRecord, cfLocalData, &anError);
ABAddressBookSave(ab, &anError);
CFRelease(cfLocalData);
}else {
UIImage *image = [UIImage imageWithData:data];
NSString *extension = [(NSArray*)[myContact.picture_path componentsSeparatedByString:@"."] objectAtIndex:1];
NSData *localData = ([extension isEqualToString:@"png"]) ? UIImagePNGRepresentation(image) : UIImageJPEGRepresentation(image, 1.0f);
CFDataRef cfLocalData = CFDataCreate(NULL, [localData bytes], [localData length]);
ABPersonSetImageData(aRecord, cfLocalData, &anError);
ABAddressBookSave(ab, &anError);
CFRelease(cfLocalData);
}
if (anError != nil) { NSLog(@"error :: %@", anError); }
ABUnknownPersonViewController *ABView = [[ABUnknownPersonViewController alloc] init];
ABView.unknownPersonViewDelegate = self;
ABView.displayedPerson = aRecord;
ABView.allowsAddingToAddressBook = YES;
ABView.allowsActions = YES;
ABView.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:ABView animated:YES];
[ABView release];
CFRelease(aRecord);
}
CFRelease(ab);
最佳答案
首先:阅读您的 Core Foundation 内存管理。你还没有把规则牢记在心。
第二个:当 CF 友好函数的名称中包含“Copy”时,您必须检查其结果是否为 NULL
,如果不是 NULL
,则在完成后释放该结果.所以这:
first = ABRecordCopyValue(person, kABPersonFirstNameProperty);
CFRelease(first);
将成为内存泄漏.
NULL
,将其传递给
CFRelease
会崩溃:
CFStringRef firstname = ABRecordCopyValue(aContactFound, kABPersonFirstNameProperty);
ABRecordSetValue(aRecord, kABPersonFirstNameProperty, firstname, &anError);
CFRelease(firstname);
firstname
是
NULL
(这可能是 - 想象一个简单地命名为“史密斯”的联系人)然后会发生崩溃。
关于iphone - AddressBook 框架的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4510421/
我需要开发一个简单的网站,我通常使用 bootstrap CSS 框架,但是我想使用 Gumbyn,它允许我使用 16 列而不是 12 列。 我想知道是否: 我可以轻松地改变绿色吗? 如何使用固定布局
这个问题在这里已经有了答案: 关闭 13 年前。 与直接编写 PHP 代码相比,使用 PHP 框架有哪些优点/缺点?
我开发了一个 Spring/JPA 应用程序:服务、存储库和域层即将完成。 唯一缺少的层是网络层。我正在考虑将 Playframework 2.0 用于 Web 层,但我不确定是否可以在我的 Play
我现有的 struts Web 应用程序具有单点登录功能。然后我将使用 spring 框架创建一个不同的 Web 应用程序。然后想要使用从 struts 应用程序登录的用户来链接新的 spring 应
我首先使用Spark框架和ORMLite处理网页上表单提交的数据,在提交中文字符时看到了unicode问题。我首先想到问题可能是由于ORMLite,因为我的MySQL数据库的字符集已设置为使用utf8
我有一个使用 .Net 4.5 功能的模块,我们的应用程序也适用于 XP 用户。所以我正在考虑将这个 .net 4.5 依赖模块移动到单独的项目中。我怎样才能有一个解决方案,其中有两个项目针对不同的版
我知道这是一个非常笼统的问题,但我想我并不是真的在寻找明确的答案。作为 PHP 框架的新手,我很难理解它。 Javascript 框架,尤其是带有 UI 扩展的框架,似乎通过将 JS 代码与设计分开来
我需要收集一些关于现有 ORM 解决方案的信息。 请随意编写任何编程语言。 你能谈谈你用过的最好的 ORM 框架吗?为什么它比其他的更好? 最佳答案 我使用了 NHibernate 和 Entity
除了 Apple 的 SDK 之外,还有什么强大的 iPhone 框架可供开始开发?有没有可以加快开发时间的方法? 最佳答案 此类框架最大的是Three20 。 Facebook 和许多其他公司都使用
有人可以启发我使用 NodeJS 的 Web 框架吗?我最近开始从免费代码营学习express js,虽然一切进展顺利,但我对express到底是什么感到困惑。是全栈框架吗?纯粹是为了后端吗?我发现您
您可以推荐哪种 Ajax 框架/工具包来构建使用 struts 的 Web 应用程序的 GUI? 最佳答案 我会说你的 AJAX/javascript 库选择应该较少取决于你的后端是如何实现的,而更多
我有生成以下错误的 python 代码: objc[36554]: Class TKApplication is implemented in both /Library/Frameworks/Tk.
首先,很抱歉,如果我问的问题很明显,因为我没有编程背景,那我去吧: 我想运行一系列测试场景并在背景部分声明了几个变量(我打印它们以仔细检查它们是否已正确声明),第一个是整数,另外两个字符串为你可以看到
在我们承担的一个项目中,我们正在寻找一个视频捕获和录制库。我们的基础工作(基于 google 搜索)表明 vlc (libvlc)、ffmpeg (libavcodec) 和 gstreamer 是三
我试过没有运气的情况下寻找某种功能来杀死/中断Play中的正常工作!框架。 我想念什么吗?还是玩了!实际没有添加此功能? 最佳答案 Java stop类中没有像Thread方法那样的东西,由于种种原因
我们希望在我们的系统中保留所有重大事件的记录。例如,在数据库可能存储当前用户状态的地方,事件日志应记录对该状态的所有更改以及更改发生的时间。 事件记录工具应该尽可能接近于事件引发器的零开销,应该容纳结
那里有 ActionScript 2.0/3.0 的测试框架列表吗? 最佳答案 2010-05-18 更新 由于这篇文章有点旧,而且我刚刚收到了赞成票,因此可能值得提供一些更新的信息,这样人们就不会追
我有一个巨大的 numpy 数组列表(一维),它们是不同事件的时间序列。每个点都有一个标签,我想根据其标签对 numpy 数组进行窗口化。我的标签是 0、1 和 2。每个窗口都有一个固定的大小 M。
我是 Play 的新手!并编写了我的第一个应用程序。这个应用程序有一组它依赖的 URL,从 XML 响应中提取数据并返回有效的 URL。 此应用程序需要在不同的环境(Dev、Staging 和 Pro
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 4年前关闭。 Improve thi
我是一名优秀的程序员,十分优秀!