Question

I am struggling with an issue regarding running a SQL statement to an Oracle database through C++, using occi. My code is as follows:

#include <iostream>
#include "occi.h"

namespace oc = oracle::occi;

int main() {
    std::cout << "Setting up environment...\n";
    oc::Environment * env = oc::Environment::createEnvironment();

    std::cout << "Setting up connection...\n";
    oc::Connection * conn = env->createConnection("user","pass","server");

    std::cout << "Creating statement...\n";
    //Very simply query... 
    oc::Statement * stmt = conn->createStatement("SELECT '1' FROM dual");

    std::cout << "Executing query...\n";
    oc::ResultSet * rs = stmt->executeQuery();

    while(rs->next()) {
            std::cout << rs->getString(1) << std::endl; //Error is thrown at this line, but after printing since I can see '1' on the console.
    }


    stmt->closeResultSet(rs);
    conn->terminateStatement(stmt);
    env->terminateConnection(conn);
    oc::Environment::terminateEnvironment(env);

    return 0;
}

The error that is shown is:

Unhandled exception at 0x1048ad7a (msvcp100d.dll) in MyDatabaseApp.exe: 0xC0000005: Access violation reading location 0xccccccd0.

My program stops inside 'xstring' at the following line of code:

    #if _ITERATOR_DEBUG_LEVEL == 0

    ....

    #else /* _ITERATOR_DEBUG_LEVEL == 0 */
    typedef typename _Alloc::template rebind<_Elem>::other _Alty;

    _String_val(_Alty _Al = _Alty())
            : _Alval(_Al)
            {   // construct allocator from _Al
            ....
            }

    ~_String_val()
            {   // destroy the object
            typename _Alloc::template rebind<_Container_proxy>::other
                    _Alproxy(_Alval);  

            this->_Orphan_all(); //<----------------------Code stops here

            _Dest_val(_Alproxy, this->_Myproxy);
            _Alproxy.deallocate(this->_Myproxy, 1);
            this->_Myproxy = 0;
            }
    #endif /* _ITERATOR_DEBUG_LEVEL == 0 */

If I change my query to:

oc::Statement * stmt = conn->createStatement("SELECT 1 FROM dual"); 

and the loop statement to:

std::cout << rs->getInt(1) << std::endl;

It works fine with no errors. I think this is because getting an integer simply returns a primitive, but when an object is being returned it is blowing up (I think on a destructor, but I'm not sure why...)

I have been playing around with this for hours today, and I am pretty stuck.

Some information about my system:

  • OS - Windows XP
  • Oracle Version - 10g
  • IDE - Microsoft Visual Studio 2010 Express C++

My project properties are as follows:

  • C/C++ - General - Additional Include Directories = C:\oracle\product\10.2.0\client_1\oci\include;%(AdditionalIncludeDirectories)
  • C/C++ - Code Generation - Multi-threaded Debug DLL (/MDd)
  • Linker - General - Additional Library Directories = C:\oracle\product\10.2.0\client_1\oci\lib\msvc\vc8;%(AdditionalLibraryDirectories)
  • Linked - Input - Additional Dependencies = oraocci10.lib;oraocci10d.lib;%(AdditionalDependencies)

I hope I haven't been confusing with too much info... Any help or insight would be great, Thanks in advance!

EDIT If I rewrite my loop, storing the value in a local variable, the error is thrown at the end of the loop:

while(rs->next()) {
    std::string s = rs->getString(1); //s is equal to "1" as expected
    std::cout << s << std::endl; //This is executed successfully
} //Error is thrown here
Was it helpful?

Solution 2

I revisited this issue about a month ago and I found that the MSVC2010 occi library was built for Oracle 11g. We are running Oracle 10g, so I had to use the MSVC2005 library. So I installed the outdated IDE and loaded the Debug library and it worked (for some reason the release version wouldn't work though).

EDIT

For anyone who is having the same problem I was, if downgrading the IDE from MSVC2010 to MSVC2005 with the appropriate libraries doesn't work, you could try upgrading the Oracle client from 10g to 11g and use the MSVC2010 library, as suggested by harvyS. In retrospect this would've probably been the better solution.

OTHER TIPS

Usually such kind of problems come from differences in build environments (IDE) of end user and provider.

Check this.

Related problems:

First try to use correct lib and dll. If compiled in debug mode then all libs and dlls must be debug. Use VC++ Modules view to be sure that proper DLL loaded.

I was lucky with my application to have all libs compiled for MSVC2010. So I just check debug and release mode DLLs and got working application.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top