Board index » cppbuilder » ADO Memory Leak - Somewhat inexperienced

ADO Memory Leak - Somewhat inexperienced


2005-09-03 02:53:28 AM
cppbuilder69
I am somewhat new to ADO.
I am trying to resolve a slight memory leak in ADO using the CreateParameter call. It seems to manifest itself when I use an adVarChar as the type. When I comment out the 2nd CreateParameter call the Leak seems to disappear. I知 not sure if I am creating and destroying these objects properly. Could some one let me know. The following is a snip it of my code.
BSTR bstrCom = SysAllocString(WideString("_hdmSingletonHDMM"));
BSTR bstrTableNo = SysAllocString(WideString("TableNoField"));
BSTR bstrDVValue = SysAllocString(WideString("TableNoField"));
BSTR bstrDVValue2 = SysAllocString(WideString(""));
if (!cmd) {
if (SUCCEEDED (hr))
hr = CoCreateInstance(CLSID_CADOCommand, NULL,
CLSCTX_INPROC_SERVER,
IID_IADOCommand, (LPVOID *) &cmd);
If (SUCCEEDED(hr))
hr = cmd->putref_ActiveConnection(conn);
if(SUCCEEDED(hr)) {
hr = cmd->put_CommandText(bstrCom);
hr = cmd->put_CommandType(adCmdStoredProc);
}
int iFieldSize = 2;
hr = cmd->CreateParameter(bstrTableNo/*Param1*/,
adSmallInt, adParamInput, iFieldSize,
Variant(2),&ppiPrm);
ADOParameters *Prms = 0;
cmd->get_Parameters(&Prms);
Prms->Append(ppiPrm);
//Set up Parameter one - This seems to be the cause
iFieldSize = 40;
hr = cmd->CreateParameter(/*Param1*/bstrDVValue, adVarChar,
adParamInput, iFieldSize, Variant(bstrDVValue2), &ppiPrm2);
Prms->Append(ppiPrm2);
.
.
.
The following is how I cleanup
if (ppiPrm2) {
ppiPrm2->Release();
}
if (ppiPrm) {
ppiPrm->Release();
}
if (Prms)
Prms->Release();
SysFreeString(bstrCommand);
SysFreeString(bstrTableNoField);
SysFreeString(bstrMrNum);
if (cmd) {
cmd->Release();
cmd = 0;
}
I hope that I do not have too many issues with this code. I do not have a real clear understanding on how to free up memory with Com objects. I had an earlier Post on AUG 19th. The suggestions there got me steered in the right direction.
Any additional suggestions are much appreciated
 
 

Re:ADO Memory Leak - Somewhat inexperienced

Go to aso group, and find the thread:
Problem on memory leak with TADOQuery
25. jul 2005 13:48
... you may be interested in it.
--
Best regards,
Vladimir Stefanovic
"Steven Brown" < XXXX@XXXXX.COM >wrote in message
Quote

I am somewhat new to ADO.
I am trying to resolve a slight memory leak in ADO using the
CreateParameter call. It seems to manifest itself when I use an adVarChar
as the type. When I comment out the 2nd CreateParameter call the Leak
seems to disappear. I知 not sure if I am creating and destroying these
objects properly. Could some one let me know. The following is a snip it
of my code.


BSTR bstrCom = SysAllocString(WideString("_hdmSingletonHDMM"));
BSTR bstrTableNo = SysAllocString(WideString("TableNoField"));
BSTR bstrDVValue = SysAllocString(WideString("TableNoField"));
BSTR bstrDVValue2 = SysAllocString(WideString(""));

if (!cmd) {
if (SUCCEEDED (hr))
hr = CoCreateInstance(CLSID_CADOCommand, NULL,
CLSCTX_INPROC_SERVER,
IID_IADOCommand, (LPVOID *) &cmd);
If (SUCCEEDED(hr))
hr = cmd->putref_ActiveConnection(conn);

if(SUCCEEDED(hr)) {
hr = cmd->put_CommandText(bstrCom);
hr = cmd->put_CommandType(adCmdStoredProc);
}

int iFieldSize = 2;
hr = cmd->CreateParameter(bstrTableNo/*Param1*/,
adSmallInt, adParamInput, iFieldSize,
Variant(2),&ppiPrm);
ADOParameters *Prms = 0;
cmd->get_Parameters(&Prms);
Prms->Append(ppiPrm);
//Set up Parameter one - This seems to be the cause
iFieldSize = 40;
hr = cmd->CreateParameter(/*Param1*/bstrDVValue, adVarChar,
adParamInput, iFieldSize, Variant(bstrDVValue2), &ppiPrm2);
Prms->Append(ppiPrm2);
.
.
.

The following is how I cleanup

if (ppiPrm2) {
ppiPrm2->Release();
}
if (ppiPrm) {
ppiPrm->Release();
}
if (Prms)
Prms->Release();
SysFreeString(bstrCommand);
SysFreeString(bstrTableNoField);
SysFreeString(bstrMrNum);
if (cmd) {
cmd->Release();
cmd = 0;
}


I hope that I do not have too many issues with this code. I do not have a
real clear understanding on how to free up memory with Com objects. I had
an earlier Post on AUG 19th. The suggestions there got me steered in the
right direction.
Any additional suggestions are much appreciated

 

Re:ADO Memory Leak - Somewhat inexperienced

aso should be ADO group ;)
--
Best regards,
Vladimir Stefanovic
"Vladimir Stefanovic" < XXXX@XXXXX.COM >wrote in message
Quote
Go to aso group, and find the thread:

Problem on memory leak with TADOQuery

25. jul 2005 13:48

... you may be interested in it.




--
Best regards,
Vladimir Stefanovic
"Steven Brown" < XXXX@XXXXX.COM >wrote in message
news:43189fa8$ XXXX@XXXXX.COM ...
>
>I am somewhat new to ADO.
>I am trying to resolve a slight memory leak in ADO using the
>CreateParameter call. It seems to manifest itself when I use an adVarChar
>as the type. When I comment out the 2nd CreateParameter call the Leak
>seems to disappear. I知 not sure if I am creating and destroying these
>objects properly. Could some one let me know. The following is a snip it
>of my code.
>
>
>BSTR bstrCom = SysAllocString(WideString("_hdmSingletonHDMM"));
>BSTR bstrTableNo = SysAllocString(WideString("TableNoField"));
>BSTR bstrDVValue = SysAllocString(WideString("TableNoField"));
>BSTR bstrDVValue2 = SysAllocString(WideString(""));
>
>if (!cmd) {
>if (SUCCEEDED (hr))
>hr = CoCreateInstance(CLSID_CADOCommand, NULL,
>CLSCTX_INPROC_SERVER,
>IID_IADOCommand, (LPVOID *) &cmd);
>If (SUCCEEDED(hr))
>hr = cmd->putref_ActiveConnection(conn);
>
>if(SUCCEEDED(hr)) {
>hr = cmd->put_CommandText(bstrCom);
>hr = cmd->put_CommandType(adCmdStoredProc);
>}
>
>int iFieldSize = 2;
>hr = cmd->CreateParameter(bstrTableNo/*Param1*/,
>adSmallInt, adParamInput, iFieldSize,
>Variant(2),&ppiPrm);
>ADOParameters *Prms = 0;
>cmd->get_Parameters(&Prms);
>Prms->Append(ppiPrm);
>//Set up Parameter one - This seems to be the cause
>iFieldSize = 40;
>hr = cmd->CreateParameter(/*Param1*/bstrDVValue, adVarChar,
>adParamInput, iFieldSize, Variant(bstrDVValue2), &ppiPrm2);
>Prms->Append(ppiPrm2);
>.
>.
>.
>
>The following is how I cleanup
>
>if (ppiPrm2) {
>ppiPrm2->Release();
>}
>if (ppiPrm) {
>ppiPrm->Release();
>}
>if (Prms)
>Prms->Release();
>SysFreeString(bstrCommand);
>SysFreeString(bstrTableNoField);
>SysFreeString(bstrMrNum);
>if (cmd) {
>cmd->Release();
>cmd = 0;
>}
>
>
>I hope that I do not have too many issues with this code. I do not have a
>real clear understanding on how to free up memory with Com objects. I had
>an earlier Post on AUG 19th. The suggestions there got me steered in the
>right direction.
>Any additional suggestions are much appreciated
>


 

{smallsort}

Re:ADO Memory Leak - Somewhat inexperienced

Thanks for responding Vladimr
I have checked out TADOQuery posting and have similar experience working with the Variants I have found
If I set a parameter like this: ADOCheck(ppiPrm2->put_Value(Variant(WideString(sCmd)))); sCmd would never get freed. So I manage it . Allocating with SysAllocString and Free it using SysFreeString. This has cleared that problem up. But the hr = cmd->CreateParameter(bstrTableNo/*Param1*/,
adSmallInt, adParamInput, iFieldSize,
Variant(2),&ppiPrm); as described above I feel helpless in trying to solve the memory leak it causes.
I have a similar problem with the Execute statement The memory leaks are small but they eventually grow an cause problems over time.
VARIANT vtEmpty;
VARIANT vtEmpty2;
vtEmpty.vt = VT_ERROR;
vtEmpty.scode = DISP_E_PARAMNOTFOUND;
vtEmpty2.vt = VT_ERROR;
vtEmpty2.scode = DISP_E_PARAMNOTFOUND;
rs = NULL;
ADOCheck(cmd2->Execute( &vtEmpty, &vtEmpty2, adCmdStoredProc | adExecuteNoRecords, &rs));
cmd2->Release();
cmd2 = 0;
It is difficult to solve these issues on my own. I知 asking for anymore suggestions or approaches to solve theses issues.
Thanks for your Attention to this dilemma.
"Vladimir Stefanovic" < XXXX@XXXXX.COM >wrote:
Quote
aso should be ADO group ;)


--
Best regards,
Vladimir Stefanovic
"Vladimir Stefanovic" < XXXX@XXXXX.COM >wrote in message
news:43197934$ XXXX@XXXXX.COM ...
>Go to aso group, and find the thread:
>
>Problem on memory leak with TADOQuery
>
>25. jul 2005 13:48
>
>... you may be interested in it.
>
>
>
>
>--
>Best regards,
>Vladimir Stefanovic
>"Steven Brown" < XXXX@XXXXX.COM >wrote in message
>news:43189fa8$ XXXX@XXXXX.COM ...
>>
>>I am somewhat new to ADO.
>>I am trying to resolve a slight memory leak in ADO using the
>>CreateParameter call. It seems to manifest itself when I use an adVarChar
>>as the type. When I comment out the 2nd CreateParameter call the Leak
>>seems to disappear. I知 not sure if I am creating and destroying these
>>objects properly. Could some one let me know. The following is a snip it
>>of my code.
>>
>>
>>BSTR bstrCom = SysAllocString(WideString("_hdmSingletonHDMM"));
>>BSTR bstrTableNo = SysAllocString(WideString("TableNoField"));
>>BSTR bstrDVValue = SysAllocString(WideString("TableNoField"));
>>BSTR bstrDVValue2 = SysAllocString(WideString(""));
>>
>>if (!cmd) {
>>if (SUCCEEDED (hr))
>>hr = CoCreateInstance(CLSID_CADOCommand, NULL,
>>CLSCTX_INPROC_SERVER,
>>IID_IADOCommand, (LPVOID *) &cmd);
>>If (SUCCEEDED(hr))
>>hr = cmd->putref_ActiveConnection(conn);
>>
>>if(SUCCEEDED(hr)) {
>>hr = cmd->put_CommandText(bstrCom);
>>hr = cmd->put_CommandType(adCmdStoredProc);
>>}
>>
>>int iFieldSize = 2;
>>hr = cmd->CreateParameter(bstrTableNo/*Param1*/,
>>adSmallInt, adParamInput, iFieldSize,
>>Variant(2),&ppiPrm);
>>ADOParameters *Prms = 0;
>>cmd->get_Parameters(&Prms);
>>Prms->Append(ppiPrm);
>>//Set up Parameter one - This seems to be the cause
>>iFieldSize = 40;
>>hr = cmd->CreateParameter(/*Param1*/bstrDVValue, adVarChar,
>>adParamInput, iFieldSize, Variant(bstrDVValue2), &ppiPrm2);
>>Prms->Append(ppiPrm2);
>>.
>>.
>>.
>>
>>The following is how I cleanup
>>
>>if (ppiPrm2) {
>>ppiPrm2->Release();
>>}
>>if (ppiPrm) {
>>ppiPrm->Release();
>>}
>>if (Prms)
>>Prms->Release();
>>SysFreeString(bstrCommand);
>>SysFreeString(bstrTableNoField);
>>SysFreeString(bstrMrNum);
>>if (cmd) {
>>cmd->Release();
>>cmd = 0;
>>}
>>
>>
>>I hope that I do not have too many issues with this code. I do not have a
>>real clear understanding on how to free up memory with Com objects. I had
>>an earlier Post on AUG 19th. The suggestions there got me steered in the
>>right direction.
>>Any additional suggestions are much appreciated
>>
>
>