0

I am building an application using the GDAL/OGR library and have faced a really strange issue, namely getting an access violation error when running my program compiled with Multi-threaded Debug DLL (/MDd) runtime library option. The following example is a minimal sample to demonstrate the problem:

#include <ogr_geometry.h>

int main(int argc, char* argv[])
{
    OGRPoint *point = new OGRPoint;
    delete point; // segmentation fault
    return 0;
}

Similar problem occurs with other OGR types in the GDAL/OGR library, so it is not related to the OGRPoint class. However there is no issue when the memory is allocated on the stack and not on the heap.

The code executes fine when I use the Multi-threaded DLL configuration instead. (So basically Release mode is good, but Debug mode runs into the mentioned access violation error.) The program also runs fine on Linux compiled with g++.

The callstack is the following:

ntdll.dll!_RtlReportCriticalFailure@8() Unknown
ntdll.dll!_RtlpHeapHandleError@4()  Unknown
ntdll.dll!_RtlpLogHeapFailure@24()  Unknown
ntdll.dll!RtlFreeHeap() Unknown
AcLayers.dll!6d7158bf() Unknown
[Frames below may be incorrect and/or missing, no symbols loaded for AcLayers.dll]  
gdal201.dll!0f87bdbd()  Unknown
TestProject.exe!main(int argc, char * * argv) Line 7    C++
[External Code] 

Any ideas on what is causing this problem?

mcserep
  • 3,231
  • 21
  • 36
  • Your conclusion, that everything is ok with the release version of the CRT is likely wrong. It merely exhibits different behavior due to a bug. Probably an uninitialized pointer that is zero-initialized in a release build, but gets a specific pattern (0xcdcdcdcd) in a debug build. – IInspectable Feb 28 '16 at 00:19
  • I also thought about that @IInspectable, therefore I checked the source code, since it is an open source library. Although the `OGRPoint` class has a member pointer, it is properly initialized to `NULL` in the constructor and checked before deletion in the destructor. – mcserep Feb 28 '16 at 00:37
  • Also, if the issue would be what you mentioned, a segfault should also arise when the object in created on the stack. However it works fine in that case. – mcserep Feb 28 '16 at 00:41
  • Not familiar with the `GDAL/OGR library` but is it using the (same) CRT itself, and are there different LIBs to link in debug vs. release configs? – dxiv Feb 28 '16 at 00:43
  • 1
    @dxiv: the build script of the `GDAL/OGR` library does not produce different libs for debug/release configs. Previously I only worked with the `GDAL` part of the library, where I did not encountered this heap corruption issue. However I started searching now based on your comment and it looks like you have a good point. Although unfortunately it seems like they are not intending to deal with this issue: https://trac.osgeo.org/gdal/ticket/3346 – mcserep Feb 28 '16 at 02:04

0 Answers0