简体   繁体   中英

dealloc in NSOperation

i have a NSOperationQueue with NSOperation, in my NSOperation .hi have this property:

@interface MyOperationClass : NSOperation 
@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (strong, nonatomic) NSManagedObject *myObject;
@property (nonatomic, retain) NSMutableArray *myArray;

@end

and this in the dealloc of the NSOperation in the .m file:

- (void)dealloc {
[__fetchedResultsController release];
[__managedObjectContext release];
[myObject release];
[myArray release];
[super dealloc];
}

in another class i add the operation in the queue in this way:

MyOperationClass *myOperation = [[MyOperationClass alloc] init];
[myOperationQueue addOperation:myOperation];
[myOperation release];

but give me a bad_exc_access on the line of [myArray release]; what i wrong?

EDIT: i notice that in the code i do this:

wikiEpisodeArray = [NSMutableArray arrayWithArray:otherArray];

maybe is this? i don't have initialized it with [NSMutableArray alloc] ?

EDIT 2: i have another similar problem, i have also this variable:

@property (nonatimc, retain) NSString *previousTime;

and i initialized it in this way:

previousTime = [[NSString alloc] init];

and in the code i never release it, only in the dealloc, and now i receive a bad exc access on this line:

[previousTime release];

in the dealloc... why?

您可能没有为要释放的对象分配内存

If you don't have arc, then you have to use (nonatomic, retain) (or assign if not retaining it).

Also, any object that you don't explicitly alloc, should be returned autoreleased. so don't release them.

If you want to create a mutable array that you own with another one you should do

[NSMutableArray alloc] initWithArray:aArray];

for the NSString, use (nonatomic, copy), also, again, when you assign a nsstring with @"something" you are assigning an autoreleased one overriding the previous [NSString alloc] init].

So, if you want to own the string you should do:

[NSString alloc] initWithString:aString]; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM