我在非ARC模式下运行。我在初始化myObj的行中收到来自静态分析器的警告,说“对象的潜在泄漏”。如果我在ARC模式下编译,我不会看到此警告。知道这里出了什么问题吗?
#import <Foundation/Foundation.h>
@interface MyClass : NSObject
-(instancetype) initWithData:(NSData *)data error:(NSError **)error;
@end
@implementation MyClass
-(instancetype) initWithData:(NSData *)data error:(NSError **)error
{
if (data == nil)
{
if (error != NULL)
{
*error = [NSError errorWithDomain:@"MyClass" code:1 userInfo:@{NSLocalizedDescriptionKey:@"Data is nil."}];
}
return nil;
}
self = [super init];
return self;
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSError *error = nil;
NSData *data = nil;
MyClass *myObj = [[MyClass alloc] initWithData:data error:&error];
NSLog(@"myObj: %@", myObj);
[myObj release];
}
return 0;
}