Hi all

I have a heap corruption problem in getting STL containers data through OCCI methods .
For instance if i do an assignment of this type

while(rs->next() )
			{

			SRecord r; '' struct object
			r.PDM_APPLICATION_NO= rs->getString(1);
			r.PDM_FIRST_NAME=rs->getString(2);; //Error Heap Corruption
			r.PDM_LAST_NAME= rs->getString(3);						
			r.PDM_F_FIRST_NAME= rs->getString(4);
			}

I use Visual C++8 (VS 2005)[Windows 32-bit] oracle 10.

Dani AI

Generated

This is a classic C++/Windows ABI/runtime mismatch, not a problem with the loop itself. The OCCI libraries you link to are prebuilt for specific Visual C++ versions; using an OCCI binary built for a different MSVC/CRT (or mixing debug vs release CRTs) lets STL objects and heap allocations cross incompatible boundaries and eventually produces “heap corruption.” Oracle’s docs show which MSVC versions each release targets (10g R2 targets VC7.x; VS2005/VC8 support appears in later releases). Oracle 10g R2 docs and Oracle 11g OCCI notes. (docs.oracle.com)

Why this blows up only after many rows (or only in some builds): MSVC runtimes maintain different heaps and the STL object layout can differ across compiler versions and between debug/release. That means copying or destroying an std::string produced by one runtime from code built with another runtime is undefined and will corrupt memory. See discussion and examples about std::string / STL across DLL boundaries and CRT issues. (stackoverflow.com)

Practical checklist to resolve it

  • Confirm what DLLs and CRT are actually loaded at runtime (use Process Explorer or Dependency Walker to inspect the running process). (learn.microsoft.com)
  • If the OCCI in your ORACLE_HOME is not built for VC8, either install an Oracle client/Instant Client that ships VC8-compatible OCCI (or upgrade to a release that does), or build/run with the compiler that matches the OCCI you have. Oracle’s C (OCI) API is an alternative if you cannot match compilers. (oracle.com)
  • Avoid passing STL objects across module boundaries. If you must interoperate with a binary built with another toolset, use a C wrapper or plain C buffers, or switch to OCI (C API) to eliminate C++ ABI problems. Also avoid buffering the entire result set in memory — stream rows and use prepared statements with bind variables for inserts.

Debugging tips

  • Turn on the CRT debug heap and break on the offending allocation to find who corrupts memory. Example:
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>

int main() {
  _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
  // optionally: _CrtSetBreakAlloc(<allocation_number>);
}
#endif

See the MSVC debug-CRT reference for details. (learn.microsoft.com)

Summary: as suspected, the fast root cause is a binary/runtime mismatch. Start by checking which OCCI/CRT DLLs are loaded, then either use matching OCCI binaries for VS2005 or move to a C-based interface / compatible client build.

Recommended Answers

All 6 Replies

That's not enough information to solve your problem.

Hi all

I have a heap corruption problem in getting STL containers data through OCCI methods .
For instance if i do an assignment of this type

while(rs->next() )
{

SRecord r; '' struct object
r.PDM_APPLICATION_NO= rs->getString(1);
r.PDM_FIRST_NAME=rs->getString(2);; //Error Heap Corruption
r.PDM_LAST_NAME= rs->getString(3); 
r.PDM_F_FIRST_NAME= rs->getString(4);
}

I use Visual C++8 (VS 2005)[Windows 32-bit] oracle 10.


I get information from oracle table 'sample'. In this table 11 fields and 100000 records. program run without error in release mode vs 2005. 12 thousand records are fatched and then Heap corruption problem message dispaly and exit from program.

More information please?..

#define WIN32COMMON


#include <iostream>
#include <Time.h>
#include <occi.h>
using namespace oracle::occi;
using namespace std;

struct SRecord
{

	string PDM_APPLICATION_NO; 
	string PDM_FIRST_NAME; 
	string PDM_LAST_NAME; 
	string PDM_F_FIRST_NAME; 
	string PDM_F_LAST_NAME; 
	string PDM_SPOUSE_NAME; 
	string PDM_DOB; 
	string PDM_ADD_LINE_1_RES; 
	string PDM_ADD_LINE_2_RES; 
	string PDM_ADD_LINE_3_RES; 
	string PDM_CITY_RES; 


};
int main()
{

	char tmpbuf[128];
	vector<SRecord> row1;

		try{ 
_strtime(tmpbuf);
cout<<endl<<" database open start"<<tmpbuf;

				Environment* env = Environment::createEnvironment(Environment::OBJECT); 
				Connection* conn = env->createConnection( "system", "dedupe","" ); 
_strtime(tmpbuf);
cout<<endl<<" database open end"<<tmpbuf;
			


				//string sqlQuery = "select * from timetest"; 

_strtime(tmpbuf);
cout<<endl<<" dynaset open start"<<tmpbuf;

				Statement* stmt = conn->createStatement("select * from timetest");
				ResultSet *rs = stmt->executeQuery();
_strtime(tmpbuf);
cout<<endl<<" dynaset open end"<<tmpbuf;

			
				
	_strtime(tmpbuf);

	cout<<endl<<"select Start Time "<<tmpbuf;
	//ResultSet::Status status;
	//while((status = rs->next()) == (ResultSet::DATA_AVAILABLE))

	int i=0;
	while(rs->next() )
			{


					SRecord r;

					r.PDM_APPLICATION_NO= rs->getString(1);
					r.PDM_FIRST_NAME= rs->getString(2);
					r.PDM_LAST_NAME= rs->getString(3);						//string str = d->rs->getString(2);
					r.PDM_F_FIRST_NAME= rs->getString(4);
					r.PDM_F_LAST_NAME= rs->getString(5); 					//	cout<<"ID   : "<<r.id;
					r.PDM_SPOUSE_NAME= rs->getString(6);					//	cout<<"   Name  : "<<r.name <<endl;
					r.PDM_DOB=rs->getString(7);

					r.PDM_ADD_LINE_1_RES= rs->getString(8);						//str=d->rs->getString(2);
					r.PDM_ADD_LINE_2_RES=rs->getString(9);						//cout<<str ;
					r.PDM_ADD_LINE_3_RES= rs->getString(10); 
					r.PDM_CITY_RES=rs->getString(11);


					row1.push_back(r);

			//	cout<<i++<<endl;
				
			}

	_strtime(tmpbuf);
cout<<endl<<" select End Time "<<tmpbuf<<endl;

	_strtime(tmpbuf);
	cout<<"Insert   Start Time "<<tmpbuf<<endl;

				
		string str1;
	
	//	vector<SRecord>::iterator i;
		stmt->setAutoCommit(true);
			//for (i = row1.begin(); i != row1.end(); i++)
			for( long iCtr=0 ; iCtr < row1.size()  ; iCtr++)
			{
					
				SRecord  &ObjRec1 =row1[iCtr];

				str1="INSERT INTO dest_timetest VALUES('" ;
				str1+= ObjRec1.PDM_APPLICATION_NO ;
				str1 +=	"','";
				str1 += ObjRec1.PDM_FIRST_NAME ;
				str1 += "','";
				str1 +=  ObjRec1.PDM_LAST_NAME;
				str1 +=  "','" ;
				str1 += ObjRec1.PDM_F_FIRST_NAME ;
				str1 += "','" ;
				str1 += ObjRec1.PDM_F_LAST_NAME ;
				str1 += "','" ;
				str1 += ObjRec1.PDM_SPOUSE_NAME ;
				str1 +=  "','" ;
				str1 += ObjRec1.PDM_DOB ;
				str1 += "','";
				str1 += ObjRec1.PDM_ADD_LINE_1_RES ;
				str1 += "','" ;
				str1 += ObjRec1.PDM_ADD_LINE_2_RES ;
				str1 += "','" ;
				str1 += ObjRec1.PDM_ADD_LINE_3_RES ;
				str1 +="','" ;
				str1 += ObjRec1.PDM_CITY_RES ;
				str1 +="' )";
	stmt = conn->createStatement(str1);
	stmt->execute();
conn->terminateStatement(stmt);	
stmt =conn->createStatement("commit");
stmt->execute();
conn->terminateStatement(stmt);	
			
			} 

			_strtime(tmpbuf);
			cout<<endl<<"End Time "<<tmpbuf<<endl;
		env->terminateConnection(conn);
	
					Environment::terminateEnvironment(env);
	
					cout << "Closing connection." << endl;
	
	
				}
				catch(SQLException ex)
				{
					 //cout<<"Exception thrown for createConnection"<<endl;
					cout<<"Error number: "<<  ex.getErrorCode() << endl;
					cout<<ex.getMessage() << endl;
				}
	
		getchar();
	return 0;

this is my code i run this code in vs 2005 using oracle 10g. this code run but when read data from table that time some record read and dispaly message Heap corruption problem .

please help me

when we install oracle many dlls are copied to home direcory of oracle.
OCCI( oracle call for c++ interface) provides the means of comunication with the oracle through c++. It has 4 dlls to provide access to oracle database. like kpucb.dll,oci.dll,ociw32.dll,oraocci10.dll

i have installed oracle10g on m/c(32bit).
but i don't know in which vc++ version it is built.
I have created the appl. in vs2005 (vc++ 8). and it is giving problem.

but the strange thing is when i use that in vc++ 6.0, it works fine.
I guess it is bcoz occi dlls are built in vc++6.0.

And i think you are right. I am using it in vs2005(vc++8) that's why it is giving problem.

Plz give me some solution.
Thanks in Advance!!!

I am facing same issue, did you get the resolution by any chance?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.