我在X-Code 8.2,OSX而不是iOS,Objective-C
在自定义的NSBlockOperation中,我包含了一个Applescript(由XCode编译),如下所示:
SHImportLayoutOperation.m文件
#import "SHImportLayoutOperation.h"
#import "Helpers.h"
@class SHIndesignImport; // Applescript
// ################################################
@interface SHImportLayoutOperation()
@property SHIndesignImport *indesignImport;
@end
// ################################################
@interface SHIndesignImport:NSObject
// Methods
- (NSMutableDictionary*)scanIndesign:(NSMutableDictionary*)parameters errors:(NSMutableArray*)errors;
@property NSBlockOperation* operation;
@end
// ################################################
@implementation SHImportLayoutOperation
- (id)init
{
self = [super init];
if (self)
{
_helpers = [[Helpers alloc] init];
init];
_indesignImport = [[NSClassFromString(@"SHIndesignImport") alloc] init];
}
return self;
}
稍后在操作中我需要一个脚本方法。一切都很好。但是:我想将当前操作(self)的引用传递给脚本以检查isCancelled状态。这也有效,但会导致内存泄漏/循环。
[_indesignImport setOperation:self];
// call scanIndesign
NSMutableDictionary* scanIndesignResult = [_indesignImport scanIndesign:parametersINDD errors:errors];
_indesignImport.operation = nil;
这导致了循环。所以我尝试了一个弱小的自我
__weak typeof(self) weakSelf = self;
[_indesignImport setOperation:weakSelf];
// call scanIndesign
NSMutableDictionary* scanIndesignResult = [_indesignImport scanIndesign:parametersINDD errors:errors];
_indesignImport.operation = nil;
同样的错误。然后我试图从AppleScript中释放它(最后):
-- Beginning of script
@property operation: missing value
-- Lots of stuff --
-- End of script
set operation to missing value
同样的东西。如果我没有通过参考,一切正常。没有循环。所以这就是原因。任何帮助表示赞赏。
修改
这是乐器的截图。如您所见,引用计数仍为1(这与weakSelf一致)