Application Crash When Saving Document After Adding FreeText Annotation

I am finding that our application crashes consistently when saving the document after the user adds a FreeText annotation. The error is EXC_BAD_ACCESS and the stack trace only shows a reference to an “unnamed_function”. I’ve tried modifying the code in various ways but cannot fix the error. Do you have any idea as to what might be happening or how I can go about tracking down what is causing the crash?

Thanks,

David

If you are viewing PDFDoc object in PDFViewCtrl while you add the FreeText annotation, then you need to make sure that PDFViewCtrl.DocLock and PDFViewCtrl.DocUnlock are called around code to add the annotation. If you are using the builtin tools, then this would be done for you already, but if you have your own code, or heavily modified the tools code you should double check this.

Otherwise, we need the following info to diagnose further.

  1. Can your reproduce the crash using our SDK sample project, or does it only occur in your project?
  2. Occurs with any file? If not, can we get a file in question.
  3. Does the text input affect it?
  4. What platform/OS/Language?
  5. Code used to create the annot and save the document.

Hi Ryan,

I’m using the built in tools for the FreeText annotation, with some very minor unrelated changes, so the document locking is covered. I’ve been stepping through the code and making sure that the lock/unlock pairs are happening in the right order in both the tools code and my code to save the document.

  1. Cannot reproduce in CompleteReader, I have spent some time comparing its code and mine and cannot locate what I am doing differently that would be causing this issue.
  2. Occurs with any file.
  3. Text input does not affect things.
  4. Running on an iPad running iOS 9.3. Project is created using Objective C.
  5. Relevant code is shown below.

The user taps a button that calls the toggleFreeTextToolbar method to initiate creating a FreeText annotation.

`

  • (IBAction)toggleFreeTextToolbar:(id)sender
    {
    if (![selectedTool isKindOfClass:[FreeTextCreate class]])
    {
    [self setSelectedTool:[[FreeTextCreate alloc] initWithPDFViewCtrl:pdfViewCtrl] withTextAnnotationTypeFlag:NO withBackToPanToolAfterUseFlag:NO];
    }

PanToolToggleAction ptta = freeTextToolBar.isHidden ? e_ptta_none : e_ptta_show;
[self toggleToolbar:freeTextToolBar withToggleSideBarSwipeIn:NO withTogglePanTool:ptta withSaveDocument:YES withIsFillColorType:NO];
}
`

When the user is finished making the FreeText annotation the toggleToolbar method is called again and initiates the call to SaveDoc.

`

  • (void)toggleToolbar:(UIToolbar *)toolbar withToggleSideBarSwipeIn:(BOOL)toggleSideBarSwipeIn withTogglePanTool:(PanToolToggleAction)togglePanTool withSaveDocument:(BOOL)saveDocument withIsFillColorType:(BOOL)isFillColorTypeFlag
    {
    // get the height of the toolbar
    float delta = toolbar.frame.size.height;
    // check if toolbar was visible or hidden before the animation
    BOOL isHidden = [toolbar isHidden];

isFillColorType = isFillColorTypeFlag;

if (toggleSideBarSwipeIn)
{
[self toggleSidebarSwipeIn:!isHidden];
[[self sideBarTab] setHidden:isHidden];
[[self.navigationItem leftBarButtonItem] setEnabled:!isHidden];
}

if (togglePanTool == e_ptta_show)
{
[self setSelectedTool:[[PanTool alloc] initWithPDFViewCtrl:pdfViewCtrl] withTextAnnotationTypeFlag:NO withBackToPanToolAfterUseFlag:YES];
}
else if (togglePanTool == e_ptta_hide)
{
selectedTool = nil;
[[pdfViewCtrl toolDelegate] setTool:nil];
}

// if toolbar was visible set delta to negative value so that animation goes up not down
// and save document if required
if (!isHidden)
{
delta *= -1;

if (saveDocument)
{
[self saveDoc];
}
}
else
{
// if toolbar was hidden then make it visible and bring to front before animation begins
toolbar.hidden = NO;
[self.view bringSubviewToFront:toolbar];
}

// run animation with duration of 0.2 second with zero delay
[UIView animateWithDuration: 0.2
delay: 0.0
options: UIViewAnimationOptionTransitionNone
animations:^{
toolbar.frame = CGRectOffset(toolbar.frame, 0.0, delta);
}
completion:^(BOOL finished) {
if (!isHidden) {
toolbar.hidden = YES;
}
}];

}
`

`

  • (void)saveDoc
    {
    BOOL isStartUpDocument = [[docPath lastPathComponent] isEqualToString:[NSString stringWithFormat:@"%@.pdf", StartupDocumentNamePortrait]] || [[docPath lastPathComponent] isEqualToString:[NSString stringWithFormat:@"%@.pdf", StartupDocumentNameLandscape]];

if (currentDoc.IsModified & !isStartUpDocument)
{
@try
{
if(![pdfViewCtrl IsFinishedRendering:NO])
{
[pdfViewCtrl CancelRendering];
}

[pdfViewCtrl DocLock: YES];
[currentDoc SaveToFile:docPath flags:e_ptlinearized];

}
@catch (NSException *exc)
{
[theAppDelegate logException:exc withMessage:[NSString stringWithFormat:@"%@ was not saved.", [docPath lastPathComponent]]];
}
@finally
{
[pdfViewCtrl DocUnlock];
}

// Reload the document - to avoid random errors
[self setAndConfigureDocument:NO];
}
}
`

As a matter of interest the same error happens if I initiate the FreeText annotation by long pressing and selecting “Text” on the pop-up toolbar.

Thanks,

David

This is strange. Am I correct to assume that the crash is occurring in a secondary thread for which the call stack is entirely unhelpful?

Hi James,

Call stack (obtained by typing ‘BT’ at the console):

  • thread #12: tid = 0x42b05, 0x0000000001017110, stop reason = EXC_BAD_ACCESS (code=1, address=0x1017110)
  • frame #0: 0x0000000001017110
    frame #1: 0x0000000100cd341c DocsOnTap___lldb_unnamed_function13307$$DocsOnTap + 60 frame #2: 0x0000000100cd1b50 DocsOnTap___lldb_unnamed_function13287$$DocsOnTap + 560
    frame #3: 0x0000000100cd2aa0 DocsOnTap___lldb_unnamed_function13300$$DocsOnTap + 52 frame #4: 0x0000000100de7d88 DocsOnTap___lldb_unnamed_function15878$$DocsOnTap + 164
    frame #5: 0x0000000100df51b0 DocsOnTap___lldb_unnamed_function15974$$DocsOnTap + 200 frame #6: 0x0000000100c4d8d4 DocsOnTap___lldb_unnamed_function12034$$DocsOnTap + 516
    frame #7: 0x0000000100d24790 DocsOnTap___lldb_unnamed_function14153$$DocsOnTap + 1460 frame #8: 0x0000000100d25fb8 DocsOnTap___lldb_unnamed_function14155$$DocsOnTap + 164
    frame #9: 0x0000000100ff9e4c DocsOnTap___lldb_unnamed_function26060$$DocsOnTap + 1980 frame #10: 0x0000000100863ee4 DocsOnTap___lldb_unnamed_function135$$DocsOnTap + 196
    frame #11: 0x000000018079bb28 libsystem_pthread.dylib_pthread_body + 156 frame #12: 0x000000018079ba8c libsystem_pthread.dylib_pthread_start + 156
    frame #13: 0x0000000180799028 libsystem_pthread.dylib`thread_start + 4

Thanks,

David

Hi James,

Call stack (obtained by typing ‘BT’ at the console):

  • thread #12: tid = 0x42b05, 0x0000000001017110, stop reason = EXC_BAD_ACCESS (code=1, address=0x1017110)
  • frame #0: 0x0000000001017110
    frame #1: 0x0000000100cd341c DocsOnTap___lldb_unnamed_function13307$$DocsOnTap + 60 frame #2: 0x0000000100cd1b50 DocsOnTap___lldb_unnamed_function13287$$DocsOnTap + 560
    frame #3: 0x0000000100cd2aa0 DocsOnTap___lldb_unnamed_function13300$$DocsOnTap + 52 frame #4: 0x0000000100de7d88 DocsOnTap___lldb_unnamed_function15878$$DocsOnTap + 164
    frame #5: 0x0000000100df51b0 DocsOnTap___lldb_unnamed_function15974$$DocsOnTap + 200 frame #6: 0x0000000100c4d8d4 DocsOnTap___lldb_unnamed_function12034$$DocsOnTap + 516
    frame #7: 0x0000000100d24790 DocsOnTap___lldb_unnamed_function14153$$DocsOnTap + 1460 frame #8: 0x0000000100d25fb8 DocsOnTap___lldb_unnamed_function14155$$DocsOnTap + 164
    frame #9: 0x0000000100ff9e4c DocsOnTap___lldb_unnamed_function26060$$DocsOnTap + 1980 frame #10: 0x0000000100863ee4 DocsOnTap___lldb_unnamed_function135$$DocsOnTap + 196
    frame #11: 0x000000018079bb28 libsystem_pthread.dylib_pthread_body + 156 frame #12: 0x000000018079ba8c libsystem_pthread.dylib_pthread_start + 156
    frame #13: 0x0000000180799028 libsystem_pthread.dylib`thread_start + 4

Thanks,

David

I'm also having this issue. For me this was due to triggering a save while the FreeTextCreate annotation tool is active (still displaying the keyboard). Depending on when the keyboard is dismissed and the annotation saves I either get the crash described or the save completes before the annotation is actually added to the document. I'm working around this issue by disabling the save button while the FreeTextCreate tool is active.

Thanks David. I’ve reproduced something similar but wanted to ask, are you able to reproduce the problem if you’re not saving a linearized document, ie passing in e_ptremove_unused to the save method?

Hi James,

If I change to e_ptremove_unused then the error goes away. What are the implications of making this change to the save flag?

Kind regards,

David

Hi Corey,

My error was happening regardless of whether the keyboard was open or not. Although the error was a little different if the keyboard was closed already.

Regards,

David

We found the source of the error and have fixed it, and will provide an update tomorrow.

Saving with e_ptremove_unused will remove unused objects from the PDF, whereas linearized saving makes it “fast web view” compatible which allows the PDF to be streamed, meaning it can be loaded and viewed in an incremental manner. If you aren’t using such functionality, saving with e_ptremove_unused should be absolutely fine.