Skip to content

Commit

Permalink
turn off optimisation in Microsoft Visual Studio for TextlineProjecti…
Browse files Browse the repository at this point in the history
…on::TruncateToImageBounds#pragma optimize( "", on )

fix issue 1496
  • Loading branch information
zdenop committed Aug 16, 2015
1 parent bb19f2c commit 9d359cf
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions textord/textlineprojection.cpp
Expand Up @@ -751,11 +751,17 @@ void TextlineProjection::TransformToPixCoords(const DENORM* denorm,
pt->y = ImageYToProjectionY(pt->y);
}

#ifdef _MSC_VER
#pragma optimize("g", off)
#endif // _MSC_VER
// Helper truncates the TPOINT to be within the pix_.
void TextlineProjection::TruncateToImageBounds(TPOINT* pt) const {
pt->x = ClipToRange<int>(pt->x, 0, pixGetWidth(pix_) - 1);
pt->y = ClipToRange<int>(pt->y, 0, pixGetHeight(pix_) - 1);
}
#ifdef _MSC_VER
#pragma optimize( "", on )
#endif // _MSC_VER

// Transform tesseract image coordinates to coordinates used in the projection.
int TextlineProjection::ImageXToProjectionX(int x) const {
Expand Down

1 comment on commit 9d359cf

@zdenop
Copy link
Contributor Author

@zdenop zdenop commented on 9d359cf Aug 16, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is backround from code.google.com issue 1496:

Ok, after some work I traced this to a Microsoft Visual Studio 2013 Community compiler optimization bug that was skipping operation of calls to TruncateToImageBounds() inside TextlineProjection.cpp. This allowed data access to memory outside of the image's buffer and, usually, an access violation.

This was hard to find because the problem didn't happen in debug mode (no optimization) and would cease happening if I added any kind of program logic before or after the calls to the Truncate...() method. It also only occurred in 32-bit code - no problem in 64-bit build. Also, code would work for some images alone but not when those images were being processed among multiple threads running Tesseract. An annoying problem to isolate (as are many compiler-optimization problems).

For our build (still using VS2013) we used pragmas to disable optimization for the TruncateToImageBounds() method - that seems to work based on our testing.

Please sign in to comment.