Search code examples
objective-ccocoaprintingnsprintoperation

EXC_BAD_ACCESS when trying to print


So, I'm trying to test the printing function and I keep getting a EXC_BAD_ACCESS error pointing me at this part of the code :

[op runOperationModalForWindow:(NSWindow*)[core window]
                      delegate:self
                didRunSelector:
 @selector(printOperationDidRun:success:contextInfo:)
                   contextInfo:NULL];

The above piece of code is straight out of Apple's developer documentation.

I've put a breakpoint before this code block, and it seems that [core window] is absolutely NOT NULL and allocated.

So, what's wrong? Any ideas?


UPDATE :

Tried this :

[self setPrintView:[[[NSTextView alloc] initWithFrame:NSMakeRect(0,0,200,200)]
  retain]];

[[self printView] setString:[[[[core editor] currentDocument] editorView] string]];
[[self printView] setHidden:NO];

NSPrintOperation *op = [NSPrintOperation
                        printOperationWithView:[[self printView] retain]
                        printInfo:nil];

[op setCanSpawnSeparateThread:YES];
[op setShowsPrintPanel:NO];
[op runOperation];

And I'm now getting :

*** -[NSStorage insertElement:atIndex:]: message sent to deallocated instance 0x121267ff0

Solution

  • I just found it :

    The trick is to release my NSTextView at the end of the printing function.

           ...
           [printView release];
    }