Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ADO "QueryInterface" memory leakage

0 views
Skip to first unread message

Ricardo Vazquez

unread,
Jun 10, 2004, 7:07:12 AM6/10/04
to
In the code below (nearly cut & pasted from MSDN) each time I call
"QueryInterface" an amount of 16, 20 or 24 KB is given by the Operating
System to my proccess. And this memory is never released. As I retrieve data
quite often, my process, after some hours, grows from 12 MB up to 300 MB!

My MDAC version is 2.62.7400.1

Am I forgetting to call some releasing or freeing function? Am I doing
something wrong in here?

Thank you!

////////////////////////////////
// My class to be bound to the recordset that DB returns
//
class CUsuariosADO : public CADORecordBinding
{
public:
BEGIN_ADO_BINDING(CUsuariosADO)

ADO_VARIABLE_LENGTH_ENTRY2(1, adBigInt, UsuarioId, sizeof(UsuarioId),
fieldStatus[0], FALSE)
ADO_VARIABLE_LENGTH_ENTRY2(2, adVarChar, Nombre, sizeof(Nombre),
fieldStatus[1], FALSE)
(...)
ADO_VARIABLE_LENGTH_ENTRY2(17, adBigInt, PermisosEmpresa,
sizeof(PermisosEmpresa),
fieldStatus[16], FALSE)
ADO_VARIABLE_LENGTH_ENTRY2(18, adVarChar, Empresa, sizeof(Empresa),
fieldStatus[17], FALSE)

END_ADO_BINDING()

public:

LONG UsuarioId;
CHAR Nombre[64];
(...)
LONG PermisosEmpresa;
CHAR Empresa [64];


LONG fieldStatus[18];

};


(...)

////////////////////////////////
// Global variables for the DB connection
//

static _ConnectionPtr dbConn1;
static _RecordsetPtr dbRset1 = NULL;
static _CommandPtr dbCmd1 = NULL;
static HRESULT hr;
static IADORecordBinding *interfazRs = NULL;
inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};

(...)

////////////////////////////////
// DB connection
//
LONG size = 512;
char errorMsg[64];

hr = S_OK;

_bstr_t cs((LPCSTR)m_sConnStr);

try
{
hr = dbConn1.CreateInstance( __uuidof( Connection ) );
if (!SUCCEEDED(hr)) return (FALSE);

hr = dbConn1->Open(cs, "", "", -1);
if (!SUCCEEDED(hr)) return (FALSE);

dbRset1 = NULL;
}

catch(_com_error &e)
{
return (FALSE);
}

(...)

////////////////////////////////
// Data retrieval function
//

CUsuariosADO myRs;

strcpy(myRs.Nombre, "");
(...)
strcpy(myRs.Empresa, "");

char errorMsg [64];
_bstr_t bstrNumero = (const char *)csExtension;

try
{
TESTHR(dbCmd1.CreateInstance (__uuidof ( Command ) ));

_bstr_t strSQL = "qry_sel_Extension_by_Numero2";

dbCmd1->CommandText = strSQL;
dbCmd1->CommandType = adCmdStoredProc;
dbCmd1->ActiveConnection = dbConn1;

dbCmd1->Parameters->Append(dbCmd1->CreateParameter("pNumero", adVarWChar,
adParamInput, 5, bstrNumero));

TESTHR(dbRset1.CreateInstance( __uuidof( Recordset ) ));


dbRset1 = dbCmd1->Execute(NULL, NULL, adCmdStoredProc);

}

catch(_com_error &e)
{
return (FALSE);
}


//
// If we comment next 3 lines NO extra memory is allocated ==> NO leak
problems.
//
if (FAILED(hr = dbRset1->QueryInterface
(__uuidof(IADORecordBinding),(LPVOID*)&interfazRs)))
{
if (!SUCCEEDED(hr)) return FALSE;
}

if (FAILED(hr = interfazRs->BindToRecordset(&myRs)))
{
if (!SUCCEEDED(hr)) return FALSE;
}


if (!dbRset1->adoEOF && !dbRset1->BOF)
{
ulID = myRs.UsuarioId;
csNombre = myRs.Nombre;
(...)
csEmpresa = myRs.Empresa;
}

if (dbRset1 != NULL)
{
if (dbRset1->State != adStateClosed)
{
dbRset1->Close();
}
}
if (dbRset1 != NULL)
{
dbRset1.Release();
}

return TRUE;

////////////////////////////////
// I will call this Data retrieval function many times on a loop: so memory
of my process grows and grows.
//


Stephen Kellett

unread,
Jun 10, 2004, 8:28:45 AM6/10/04
to
In message <upsV8vtT...@TK2MSFTNGP12.phx.gbl>, Ricardo Vazquez
<rvaz...@dummy.com> writes

>Am I forgetting to call some releasing or freeing function? Am I doing
>something wrong in here?

if (InterfazRs != NULL)
InterfazRs->Release();

perhaps?

Stephen
--
Stephen Kellett
Object Media Limited http://www.objmedia.demon.co.uk
RSI Information: http://www.objmedia.demon.co.uk/rsi.html

Ricardo Vazquez

unread,
Jun 10, 2004, 11:37:27 AM6/10/04
to
YESSS
That made it!

THANK YOU VERY MUCH!

(I did not find the IADORecordBinding documentation on MSDN and did not know
of this method of its)


"Stephen Kellett" <sn...@objmedia.demon.co.uk> escribió en el mensaje
news:AOc6GzA9...@objmedia.demon.co.uk...

Simon Trew

unread,
Jun 16, 2004, 3:28:08 AM6/16/04
to
"Ricardo Vazquez" <rvaz...@dummy.com> wrote in message
news:%23VMkYDw...@tk2msftngp13.phx.gbl...

> (I did not find the IADORecordBinding documentation on MSDN and did not
know
> of this method of its)


All interfaces have the Release() method, which they inherit from IUnknown.
It's a basic rule of COM that if you QueryInterface or AddRef on an
interface, you must have a corresponding Release().


0 new messages