memory leak in occi

转帖于

http://hi.baidu.com/walterfan/blog/item/94d9a938605d87c0d462256c.html


一个简单的OCCI小程序充,用valgrind 检查也会报一大堆错误
valgrind --tool=memcheck --leak-check=yes ./occiexam

据oralce forum上的一位kyrptt老兄说, 这并不是程序的问题,而是OCCI库本身的问题

"You do not have a Leak.

The OCCI library allocates space for something in setUserName with:

new[] and then de-allocates it with delete instead of delete[].

Actually the OCCI library outputs alot of messages with Valgrind. but I have not noticed nay particularly damaging leaks.

I suggest you use a Suppresions file to ignore the specific OCCI related errors so you dont miss leaks of your own."

害得我花了这么长时间去研究这个问题,

#include <iostream>
#include <string>
#include <vector>

#include <occi.h>

using namespace std;
using namespace oracle::occi;


int main()
{

Environment* env;
env = Environment::createEnvironment(Environment::OBJECT);
cout << "Enviorment created " << endl;

Connection* conn;
Statement *stmt;
ResultSet *rs;
vector<MetaData> vMD;
vector<MetaData> vArgMD;
int dataType;
char tmpChar[1024];

Statement::Status status;
try
{

conn = env->createConnection("iris","tpstps", "iris10gr1");
cout << "connection created " << endl;
stmt = conn->createStatement();

string sStmt = "begin :1 := PKGNETWORK.spGetNetworkInfoByIP(:2, :3); end;";
// 1st sp
stmt->setSQL(sStmt);
stmt->registerOutParam(1, OCCIINT);
stmt->setString(2,"192.168.6.12");
stmt->setMaxParamSize(2,20);
stmt->registerOutParam(3,OCCICURSOR);

status = stmt->execute();
int retVal = stmt->getInt(1);
rs = stmt->getCursor(3);//use ResultSet to fetch rows
vMD = rs->getColumnListMetaData();
dataType = vMD[0].getInt(MetaData::ATTR_DATA_TYPE);

cout << "Attribute count : " << vMD[0].getAttributeCount() << endl;
dataType=vMD[0].getInt(MetaData::ATTR_DATA_TYPE);
cout << "Attribute type : " << dataType << endl;
cout << "Attribute name : " << vMD[0].getString(MetaData::ATTR_NAME)<< endl;
cout << "Size : " << vMD.size() << endl;
while (rs->next())
{
cout << "next row";
cout << "NetID : " << rs->getString(1).c_str() << endl;
}

cout << " vector filled for SP1" << endl;
cin >> tmpChar;

vMD.clear();
stmt->closeResultSet(rs);
conn->terminateStatement(stmt);

cout << "1st sp executed" << endl;

// 2nd sp
string sStmt1 = "begin :1 := PKGJUNK.SpGetTblAccount(:2); end;";
stmt = conn->createStatement();
stmt->setSQL(sStmt1);
stmt->registerOutParam(1, OCCIINT);
stmt->registerOutParam(2,OCCICURSOR);
status = stmt->execute();

retVal = stmt->getInt(1);
rs = stmt->getCursor(2);//use ResultSet to fetch rows
int loop = 1;
vMD = rs->getColumnListMetaData();
dataType = vMD[0].getInt(MetaData::ATTR_DATA_TYPE);

cout << "Attribute count : " << vMD[0].getAttributeCount() << endl;
dataType=vMD[0].getInt(MetaData::ATTR_DATA_TYPE);
cout << "Attribute type : " << dataType << endl;
cout << "Attribute name : " << vMD[0].getString(MetaData::ATTR_NAME)<< endl;
cout << "Size : " << vMD.size() << endl;
while (rs->next())
{
cout << "next row";
cout << "NetID : " << rs->getString(1).c_str() << endl;
}

stmt->closeResultSet(rs);
cout << "2nd sp executed" << endl;

conn->terminateStatement(stmt);

// sp-3
string sStmt2 = "begin :1 := PKGONELINK.spValidateAccount(:2,:3,:4,:5,:6); end;";
stmt = conn->createStatement();
stmt->setSQL(sStmt2);
stmt->registerOutParam(1, OCCIINT);
stmt->setString(2,"37863 ");
stmt->setMaxParamSize(2,100);
stmt->registerOutParam(3,OCCISTRING, 20);
stmt->registerOutParam(4,OCCISTRING, 2);
stmt->registerOutParam(5,OCCISTRING, 3);
stmt->registerOutParam(6,OCCISTRING, 3);

status = stmt->execute();

cout << "3rd sp executed" << endl;

conn->terminateStatement(stmt);
conn->flushCache();
env->terminateConnection(conn);

vMD.clear();

cout << "Demo completed...data deleted" << endl;
cin >> tmpChar;
}
catch (SQLException &ex)
{//cleanup
cout << "Error, cleaning up..." << ex.getMessage() << endl;
conn->terminateStatement(stmt);
env->terminateConnection(conn);
cout << "terminating env" << endl;
Environment::terminateEnvironment(env);
// throw;//will be caught by outer handler
}

Environment::terminateEnvironment(env);

return 0;
}

Reference
http://forums.oracle.com/forums/thread.jspa?messageID=1493167

你可能感兴趣的:(memory leak in occi)