I have discovered a bug in the DB2 drivers. When connecting to DB2 in an application that uses both the .NET and C++ APIs a memory access violation occurs on Windows 8.1 & Windows 10. The same error does **not** occur on Windows 7.
We are developing a product that has components that run only on Windows (build in .NET), and components that must run on both Windows & z/OS (created with C++, on windows compiled as DLL). Both types of components separately function without a problem; but together memory corruption errors occur.
The .NET component connects to DB2 using the IBM Data Server Driver Package using the IBM.Data.DB2.DB2Connection class. Connecting and using that connection succeeds.
The C++ component is loaded as DLL into the .NET component and also connects to the same DB2 database (via ODBC) and executes various DML statements. This too succeeds.
However, as soon as the C++ component has connected to DB2, the .NET part of the product cannot use DB2 anymore. Although setting up the .NET connection succeeds, using that connection results in the exception below:
I have attached a VS2015 solution that demonstrates this issue ([DB2DriverCrash.zip], the zipfile includes as README.txt)
Exception:System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at IBM.Data.DB2.UnsafeNativeMethods.DB232.SQLExecDirectADONET(DB2ExecDirectParameters* pParam)
at IBM.Data.DB2.DB2Command.ExecuteNonQueryObject(Boolean skipInitialValidation)
I have worked around this issue by statically linking db2app64.lib to out C++ dll, This separates the memory spaces for the .NET and C++ codes preventing the memory corruption. It’s ugly, but it works. Still, I think IBM should solve this bug.