Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

[ManagedObjectContext release] message sent to deallocated instance... #50

Closed
TheCrab-xx opened this Issue Jan 31, 2014 · 9 comments

Comments

Projects
None yet
3 participants

Sometimes I get this error when iCloud is turned on, here's the log:

2014-01-31 09:33:34.549 iSteccone[31431:6a0f] Log Successfully loaded cloud store.
2014-01-31 09:33:34.550 iSteccone[31431:6a0f] Log Switching cloud enabled -> enabled
2014-01-31 09:33:34.550 iSteccone[31431:6a0f] Log Reloading store...
2014-01-31 09:33:34.550 iSteccone[31431:6a0f] Log Will load cloud store.
2014-01-31 09:33:34.550 iSteccone[31431:6a0f] Log Clearing stores...
2014-01-31 09:33:34.550 iSteccone[31431:6a0f] Log Will clear stores. Notifying application to reset its UI.
2014-01-31 09:33:34.550 iSteccone[31431:70b] *** -[NSManagedObjectContext release]: message sent to deallocated instance 0xe8bf530

Where am I wrong?

Owner

lhunath commented Jan 31, 2014

You have a dangling pointer. Switch to ARC or learn the memory management rules. Actually, do both of those.
https://developer.apple.com/library/mac/documentation/cocoa/conceptual/MemoryMgmt/Articles/mmRules.html#//apple_ref/doc/uid/20000994-BAJHFBGH

@lhunath lhunath closed this Jan 31, 2014

qepic42 commented Mar 3, 2014

Hey, I'm using ARC and have the same problem, where could the 'dangling pointer' be? I'm using almost the sample code of UbiquityStoreManager…

(It occurs during a store-transition local/cloud)

Owner

lhunath commented Mar 3, 2014

Where are you when you're sending a message to a deallocated instance of MSManagedObjectContext? What message are you sending? What is all your code that defines, sets and uses your moc?

qepic42 commented Mar 3, 2014

Thank's for quick reply.

My properties:

@property (strong, nonatomic) UbiquityStoreManager *ubiquityStoreManager;
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

I'm creating MOC here (AppDelegate.m):

- (void)ubiquityStoreManager:(UbiquityStoreManager *)manager didLoadStoreForCoordinator:(NSPersistentStoreCoordinator *)coordinator
                     isCloud:(BOOL)isCloudStore {

    NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
    moc.persistentStoreCoordinator = coordinator;
    moc.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy;
    _managedObjectContext = moc;

    dispatch_async( dispatch_get_main_queue(), ^{
        [cloudContentCorruptedAlert dismissWithClickedButtonIndex:[cloudContentCorruptedAlert cancelButtonIndex] animated:YES];
        [handleCloudContentWarningAlert dismissWithClickedButtonIndex:[handleCloudContentWarningAlert cancelButtonIndex] animated:YES];
    } );
}

MOC interaction methods from AppDelegate.m:

- (void)saveContext
{
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
    [managedObjectContext performBlockAndWait:^{
        NSError *error = nil;
        if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error])
            NSLog( @"Unresolved error: %@\n%@", error, [error userInfo] );

        [managedObjectContext reset];
    }];
}
- (NSManagedObjectContext *)ubiquityStoreManager:(UbiquityStoreManager *)manager
          managedObjectContextForUbiquityChanges:(NSNotification *)note {

    return self.managedObjectContext;
}

- (void)ubiquityStoreManager:(UbiquityStoreManager *)manager willLoadStoreIsCloud:(BOOL)isCloudStore {

    [self saveContext];

    _managedObjectContext = nil;
}

My code causing the crash:

- (IBAction)pushChangeiCloudState:(id)sender
{
    BOOL useiCloud = NO;

    if (![JGAppDelegate sharedAppDelegate].ubiquityStoreManager.cloudEnabled)
    {
        useiCloud = YES;
    }

    [[JGAppDelegate sharedAppDelegate].ubiquityStoreManager setCloudEnabled:useiCloud];
}

Here is the screenshot from the log/thread-stack:
screen shot 2014-03-03 at 22 34 38

Owner

lhunath commented Mar 3, 2014

Can you try nil'ing _managedObjectContext from within the perform block and
when you get another crash, unfold the thread's stack so I can see each
frame (now it skips from 10 to 0) (see the slider on the bottom of the
panel).

On 3 March 2014 16:40, Jan Galler notifications@github.com wrote:

Thank's for quick reply.

My properties:

@property (strong, nonatomic) UbiquityStoreManager *ubiquityStoreManager;
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

I'm creating MOC here (AppDelegate.m):

  • (void)ubiquityStoreManager:(UbiquityStoreManager *)manager didLoadStoreForCoordinator:(NSPersistentStoreCoordinator *)coordinator
    isCloud:(BOOL)isCloudStore {

    NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
    moc.persistentStoreCoordinator = coordinator;
    moc.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy;
    _managedObjectContext = moc;

    dispatch_async( dispatch_get_main_queue(), ^{
    [cloudContentCorruptedAlert dismissWithClickedButtonIndex:[cloudContentCorruptedAlert cancelButtonIndex] animated:YES];
    [handleCloudContentWarningAlert dismissWithClickedButtonIndex:[handleCloudContentWarningAlert cancelButtonIndex] animated:YES];
    } );
    }

MOC interaction methods from AppDelegate.m:

  • (void)saveContext
    {
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
    [managedObjectContext performBlockAndWait:^{
    NSError *error = nil;
    if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error])
    NSLog( @"Unresolved error: %@\n%@", error, [error userInfo] );

    [managedObjectContext reset];
    

    }];
    }

  • (NSManagedObjectContext *)ubiquityStoreManager:(UbiquityStoreManager *)manager
    managedObjectContextForUbiquityChanges:(NSNotification *)note {

    return self.managedObjectContext;
    }

  • (void)ubiquityStoreManager:(UbiquityStoreManager *)manager willLoadStoreIsCloud:(BOOL)isCloudStore {

    [self saveContext];

    _managedObjectContext = nil;
    }

My code causing the crash:

  • (IBAction)pushChangeiCloudState:(id)sender
    {
    BOOL useiCloud = NO;

    if (![JGAppDelegate sharedAppDelegate].ubiquityStoreManager.cloudEnabled)
    {
    useiCloud = YES;
    }

    [[JGAppDelegate sharedAppDelegate].ubiquityStoreManager setCloudEnabled:useiCloud];
    }

Here is the screenshot from the log/thread-stack:
[image: screen shot 2014-03-03 at 22 34 38]https://f.cloud.github.com/assets/524677/2314827/b514fa10-a31b-11e3-8a0d-431233b0b4ad.png


Reply to this email directly or view it on GitHubhttps://github.com/lhunath/UbiquityStoreManager/issues/50#issuecomment-36563332
.

Maarten Billemont (lhunath)
me: http://www.lhunath.com – business: http://www.lyndir.com
http://masterpasswordapp.com

qepic42 commented Mar 3, 2014

Sorry for the thread's stack folding, I forgot about that. Now I did:

- (void)saveContext
{
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
    [managedObjectContext performBlockAndWait:^{
        NSError *error = nil;
        if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error])
            NSLog( @"Unresolved error: %@\n%@", error, [error userInfo] );

        [managedObjectContext reset];
        _managedObjectContext = nil;
    }];
}

Was that what you want me to do?

And get:
screen shot 2014-03-03 at 23 06 35

Some things I found about over-released MOCs:

I'm using my AppDelegate's sharedInstance everywhere to access CoreData's MOC, I don't use fetched result things and so on… I'm new to CoreData… Everything but store-transitions work fine with my CoreData implementation.

Owner

lhunath commented Mar 3, 2014

you probably won't want to nil _managedObjectContext each time you save the
context.

Anyway, unless you're doing weird thread-violating stuff with your MOC, I'm
all out of ideas (remember, your MOC is a NSMainQueueConcurrencyType so you
should only ever use it from the main thread). Try to figure out who posted
that crashing perform block.

On 3 March 2014 17:08, Jan Galler notifications@github.com wrote:

Sorry for the thread's stack folding, I forgot about that. Now I did:

  • (void)saveContext
    {
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
    [managedObjectContext performBlockAndWait:^{
    NSError *error = nil;
    if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error])
    NSLog( @"Unresolved error: %@\n%@
    ", error, [error userInfo] );

    [managedObjectContext reset];
    _managedObjectContext = nil;
    

    }];
    }

Was that what you want me to do?

And get:
[image: screen shot 2014-03-03 at 23 06 35]https://f.cloud.github.com/assets/524677/2315182/48374fa6-a320-11e3-9e21-7e34bbd396c5.png


Reply to this email directly or view it on GitHubhttps://github.com/lhunath/UbiquityStoreManager/issues/50#issuecomment-36566410
.

Maarten Billemont (lhunath)
me: http://www.lhunath.com – business: http://www.lyndir.com
http://masterpasswordapp.com

qepic42 commented Mar 3, 2014

By removing any nil'ling of _managedObjectContext, there is no crash anymore. Thank you, I don't know why that doesn't look strange for me, resetting _managedObjectContext every time I save…
Anyway, you solved my problem, as it looks like.

Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment