我的文件中有一些内存泄漏,valgrind 指向 .cc 文件中下面给出的代码
sqlcxt(&ctx, &sqlctx, &sqlstm, &sqlfpn);
代码没有这个直接。代码是
SQLDA *bind_dp;
SQLDA *select_dp;
/* Allocate memory for the select and bind descriptors. */
if (!Db::alloc_descriptors(MAX_ITEMS, MAX_VNAME_LEN, MAX_INAME_LEN, bind_dp, select_dp))
goto errexit;
EXEC SQL WHENEVER SQLERROR GOTO errexit_select;
EXEC SQL PREPARE S FROM :query;
EXEC SQL DECLARE C CURSOR FOR S;
Db::set_bind_variables(bind_dp, ¶mList);
EXEC SQL OPEN C USING DESCRIPTOR bind_dp;
Db::process_select_list(select_dp, dbQueryRsp);
/* When done, free the memory allocated for
pointers in the bind and select descriptors. */
for (int i = 0; i < MAX_ITEMS; i++)
{
if (bind_dp->V[i] != (char *) 0)
free(bind_dp->V[i]);
free(bind_dp->I[i]); /* MAX_ITEMS were allocated. */
if (select_dp->V[i] != (char *) 0)
free(select_dp->V[i]);
free(select_dp->I[i]); /* MAX_ITEMS were allocated. */
}
/* Free space used by the descriptors themselves. */
SQLSQLDAFree( ctx, bind_dp);
SQLSQLDAFree( ctx, select_dp);
EXEC SQL WHENEVER SQLERROR CONTINUE;
/* Close the cursor. */
EXEC SQL CLOSE C;
alloc_descriptors 如下:
bool Db::alloc_descriptors(int size, int max_vname_len, int max_iname_len, SQLDA *&bind_dp, SQLDA *&select_dp)
{
GET_CONTEXT_DEFINE_SQLCA;
EXEC SQL CONTEXT USE :ctx;
if ((bind_dp =
SQLSQLDAAlloc(ctx, size, max_vname_len, max_iname_len)) ==
(SQLDA *) 0)
{
logMsg(DEBUG, 2,"Cannot allocate memory for bind descriptor.");
return false; /* Have to exit in this case. */
}
if ((select_dp =
SQLSQLDAAlloc (ctx, size, max_vname_len, max_iname_len)) ==
(SQLDA *) 0)
{
logMsg(DEBUG, 2,"Cannot allocate memory for select descriptor.");
return false;
}
select_dp->N = MAX_ITEMS;
/* Allocate the pointers to the indicator variables, and the
actual data. */
for (int i = 0; i < MAX_ITEMS; i++) {
bind_dp->I[i] = (short *) malloc(sizeof (short));
select_dp->I[i] = (short *) malloc(sizeof(short));
bind_dp->V[i] = (char *) malloc(1);
select_dp->V[i] = (char *) malloc(1);
}
return true;
}
我可以在 .cc 文件中给出的总代码是
/* EXEC SQL OPEN C USING DESCRIPTOR bind_dp; */
{
struct sqlexd sqlstm;
sqlstm.sqlvsn = 12;
sqlstm.arrsiz = 1;
sqlstm.sqladtp = &sqladt;
sqlstm.sqltdsp = &sqltds;
sqlstm.stmt = "";
sqlstm.iters = (unsigned int )1;
sqlstm.offset = (unsigned int )24;
sqlstm.selerr = (unsigned short)100;
sqlstm.cud = sqlcud0;
sqlstm.sqlest = (unsigned char *)&sqlca;
sqlstm.sqlety = (unsigned short)4352;
sqlstm.occurs = (unsigned int )0;
sqlstm.sqcmod = (unsigned int )0;
sqlstm.sqhstv[0] = (unsigned char *)bind_dp;
sqlstm.sqhstl[0] = (unsigned long )0;
sqlstm.sqhsts[0] = ( int )0;
sqlstm.sqindv[0] = ( short *)0;
sqlstm.sqinds[0] = ( int )0;
sqlstm.sqharm[0] = (unsigned long )0;
sqlstm.sqadto[0] = (unsigned short )0;
sqlstm.sqtdso[0] = (unsigned short )0;
sqlstm.sqphsv = sqlstm.sqhstv;
sqlstm.sqphsl = sqlstm.sqhstl;
sqlstm.sqphss = sqlstm.sqhsts;
sqlstm.sqpind = sqlstm.sqindv;
sqlstm.sqpins = sqlstm.sqinds;
sqlstm.sqparm = sqlstm.sqharm;
sqlstm.sqparc = sqlstm.sqharc;
sqlstm.sqpadto = sqlstm.sqadto;
sqlstm.sqptdso = sqlstm.sqtdso;
sqlcxt(&ctx, &sqlctx, &sqlstm, &sqlfpn);
}
DbT::process_select_list(select_dp, dbQueryRsp);
有人可以解释这是什么意思。
valgrind 的转储是这样的:
==1212== 124 bytes in 1 blocks are possibly lost in loss record 2,103 of 3,407
==1212== at 0x4A078B8: malloc (vg_replace_malloc.c:270)
==1212== by 0x643CC59: sqlalc (in /home/product/11.2.0/client_1/lib/libclntsh.so.11.1)
==1212== by 0x7D32749: sqlbrl (in /home/product/11.2.0/client_1/lib/libclntsh.so.11.1)
==1212== by 0x6D96B7A: sqlbds (in /home/product/11.2.0/client_1/lib/libclntsh.so.11.1)
==1212== by 0x7D34543: sqlnst (in /home/product/11.2.0/client_1/lib/libclntsh.so.11.1)
==1212== by 0x7D3068A: sqlcxt (in /home/product/11.2.0/client_1/lib/libclntsh.so.11.1)
==1212== by 0x9440C25: execute(std::string const&, cil_cch_namespace::ParamTypeListList const&, cil_cch_namespace::QueryType&, long&, long&, ExtError&) (Db.cc:608)
==1212== by 0x94430B0: execute(std::string const&, std::string&, long&, ExtError&) (Db.cc:474)