使用Pinterest SDK将消息发送到已释放实例 [英] message sent to deallocated instance using Pinterest SDK

查看:141
本文介绍了使用Pinterest SDK将消息发送到已释放实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Pinterest iOS SDK在我的iPad应用程序中共享一个项目.以下代码段将始终在带有注释的行上以message sent to deallocated instance崩溃:

I'm using the Pinterest iOS SDK to share an item in my iPad app. The following snippet of code will always crash with a message sent to deallocated instance on the line with the comment:

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

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

I'm using NSMutableString stringWithString to simulate the conditions in my app. I don't actually use that line in my code.

即使不在最后一行输出clientId,离开块时应用程序也会崩溃.我认为是因为ARC试图释放已经释放的引用.

Even if don't output the clientId on the last line, the app crashes when leaving the block. I assume it's because ARC is trying to release the reference which has already been deallocated.

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

It seems like the Pinterest SDK must be doing something wonky and trashing the string I'm passing in. Is there some way I can get around this while they fix their code?

编辑1

简化了测试案例.

编辑2

似乎Pinterest SDK正在使用clientId参数.根据 clang ARC文档,将其表示为clang的方法是用__attribute((ns_consumed))表示这一点.

It looks like the Pinterest SDK is consuming the clientId argument. Based on the clang ARC documentation, the way to indicate this to clang is to indicate this with __attribute((ns_consumed)).

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

New question: Is it possible to indicate this to ARC without modifying the signature of the method to add the attribute?

编辑3

所以这行得通,但作为罪恶是丑陋的吗?还有另一种方法吗?

So this works, but it's ugly as sin? Is there another way?

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

推荐答案

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

What I did was make a static variable that represented the Pinterest class:

//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.不过,我同意,这对他们来说是一个令人震惊的疏忽.他们甚至没有记录这种行为,他们在想什么?!

I think that Pinterest assumed that everyone would use their class as a static singleton because that's probably what they do internally. To be fair, I don't foresee using multiple client ID's with a single app in most cases. I agree, though, this is a stupefying oversight on their part. They didn't even document this behavior, what were they thinking?!

这篇关于使用Pinterest SDK将消息发送到已释放实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆