gpt4 book ai didi

ios - 使用 Pinterest SDK 发送到释放实例的消息

转载 作者:可可西里 更新时间:2023-11-01 05:55:36 25 4
gpt4 key购买 nike

我正在使用 Pinterest iOS SDK 在我的 iPad 应用程序中分享一个项目。以下代码片段将始终崩溃,并在带有注释的行上发送一条消息发送到已释放的实例:

NSString *clientId = [NSMutableString stringWithString:@"1431665"];
NSLog(@"clientId: %@", clientId);
Pinterest *pinterest = [[Pinterest alloc] initWithClientId:clientId];
NSLog(@"gone: %@", clientId); // <- CRASH!

我正在使用 NSMutableString stringWithString 来模拟我的应用程序中的条件。我实际上并没有在我的代码中使用该行。

即使不在最后一行输出 clientId,应用程序也会在离开 block 时崩溃。我假设这是因为 ARC 正在尝试释放已经被释放的引用。

看来 Pinterest SDK 一定是在做一些奇怪的事情,破坏了我传入的字符串。在他们修复代码的同时,我有什么办法可以解决这个问题吗?

编辑 1

简化了测试用例。

编辑 2

看起来 Pinterest SDK 正在使用 clientId 参数。基于clang ARC documentation , 向 clang 表明这一点的方法是用 __attribute((ns_consumed)) 来表明这一点。

新问题:是否可以在不修改添加属性的方法签名的情况下向 ARC 指示这一点?

编辑 3

所以这可行,但它丑陋得像罪恶?还有别的办法吗?

NSString *clientId = [NSMutableString stringWithString:@"1431665"];
[clientId performSelector:NSSelectorFromString(@"retain")]; // <- UGLY!
NSLog(@"clientId: %@", clientId);
Pinterest *pinterest = [[Pinterest alloc] initWithClientId:clientId];
NSLog(@"gone: %@", clientId);

最佳答案

我所做的是制作一个代表 Pinterest 类的静态变量:

//I put this outside my @implementation code at the top of my m file
static Pinterest *_pinterest = nil;

// instantiating
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_pinterest = [[Pinterest alloc] initWithClientId:clientId];
});

我认为 Pinterest 假设每个人都会将他们的类用作静态单例,因为这可能是他们内部所做的。公平地说,在大多数情况下,我预计不会在单个应用程序中使用多个客户端 ID。不过,我同意,这是他们的一个令人震惊的疏忽。他们甚至没有记录这种行为,他们在想什么?!

关于ios - 使用 Pinterest SDK 发送到释放实例的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17139092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com