This is a really weird problem (in my view, anyway), because it only occurs in 
a very particular set of circumstances.  My application is NSDocument based 
(just in case it makes a difference), and my application uses ARC.

I have an NSArray of NSDictionary containing file information, and this is 
displayed in an NSTableView.  

If I open the document and then shut it again without doing anything, all is 
well.  The memory is released as it should be, and the document closes neatly.
If I open the document and then double click on a file in the list, the data in 
that file is unpacked into a file in my application's cache folder and is then 
opened (using [[NSWorkspace sharedWorkspace] openFile:filepath];).  If I then 
close the opened file and then close my document, the memory is released as it 
should be, and the document closes neatly.
If I open the document and then double click on a file in the list, but then 
close my document **before** closing the newly opened file my application 
crashes with "message sent to deallocated instance".

I can't for the life of me work out why this crash is happening. My 
understanding was that, once [[NSWorkspace sharedWorkspace] openFile:filepath] 
completes the newly opened file is entirely independent of the application or 
process that spawned it.  Am I right in supposing that there's some magic 
thread which means that my application knows that it has opened a document, and 
that document hasn't yet been closed?  Is there some observer at work here - 
and, if so, how do I fix it?

The code:
- (void)openAttachmentInViewer:(id)sender
{
    @autoreleasepool
    {
        NSString* filepath;
        
        if ([_attachmentTable selectedRow] < 0 || [_attachmentTable 
selectedRow] >= [attachmentArray count]) return;
        
        if ([_attachmentTable numberOfSelectedRows] == 1)
        {
            id record = [attachmentArray objectAtIndex:[_attachmentTable 
selectedRow]];
            NSArray* paths = 
NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
            
            NSString* pathStem;
            
            //Check for Viewer directory in Caches and create it if it doesn't 
exist
            NSFileManager *fileManager= [NSFileManager defaultManager];
            NSError *error = nil;
            if(![fileManager createDirectoryAtPath:[NSString 
stringWithFormat:@"%@/Viewer",[paths objectAtIndex:0]]
                       withIntermediateDirectories:YES
                                        attributes:nil
                                             error:&error])
            {
                NSLog(@"Failed to create directory \"%@\". Error: %@ - Cached 
file saved in general cache folder.",
                      [NSString stringWithFormat:@"%@/Viewer",[paths 
objectAtIndex:0]],
                      error);
                pathStem = [paths objectAtIndex:0];
            }
            else
            {
                pathStem = [NSString stringWithFormat:@"%@/Viewer",[paths 
objectAtIndex:0]];
            }
            
            filepath = [NSString stringWithFormat:@"%@/%@",pathStem,[record 
objectForKey:@"name"]];
            
            NSData* thisAttachment = [[NSData alloc] initWithData:[record 
objectForKey:@"data"]];
            [thisAttachment writeToFile:filepath atomically:NO];
            
            
            [[NSWorkspace sharedWorkspace] openFile:filepath];
            
        }
    }
}


All ideas and suggestions welcome!
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to