LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

CA_DiscardObjHandle() and possible memory leak

I am using ADO 2.5 (by creating ActiveX controller) in my project and have some issues with memory leak.
I am able to run the same project using C++ with out any problems.

Does the CA_DiscardObjHandle() sets the object to NULL after calling release method?

In C++ I did like this for a recordset
_RecordsetPtr m_pRecordset;

m_pRecordset.Release();
m_pRecordset = NULL; // set smart pointer to NULL

Thanks,
Mano.S
0 Kudos
Message 1 of 5
(3,346 Views)
Hello

CA_DiscardObjHandle effectivly releases the object like you should do with Release(). But You need to make sure you call CA_DiscardObjHandle() for every object that you return. It wont get cleared automatically like a smart pointer would. You would need to keep track of the items.
Also make sure if you are passing around variants, that you call CA_VariantClear() to clear then after you're done with them. Same with CA_FreeBSTR() for bstrings

I hope this helps

Bilal Durrani
NI
Bilal Durrani
NI
0 Kudos
Message 2 of 5
(3,346 Views)
Hello Bilal,
Thanks for the reply. I checked my CVI code and am doing all the things you said. But when I run the code I see a big leap in the memory usage after 1000 iterations or so. I monitored the private bytes using Windows Performance monitor.

Here is the sample code.
CVI Version 7.0
OS: Windows 2000
Database: MS Access 2000
ADO Version: 2.53.6200.0

CAObjHandle connectionHandle = S_OK;
CAObjHandle rsHandle = S_OK;
LPDISPATCH lpDispatch;
VARIANT dbConnection;
VARIANT source;

// Create connection object
ADODB_New_Connection (NULL, 1, LOCALE_NEUTRAL, 0, &connectionHandle);
// Open Connection
ADODB__ConnectionOpen (connectionHandle, NULL, "DSN=MyDatabase;UID="";PWD="";", "", "", -1);
// Get active connection into variant
CA_GetInterfa
ceFromObjHandle (connectionHandle, 0, 0, &lpDispatch, NULL);
dbConnection = CA_VariantDispatch (lpDispatch);

for ( i = 0; i < 3000; i++) {
// Create record set object
ADODB_New_Recordset (NULL, 1, LOCALE_NEUTRAL, 0, &rsHandle);
// Set SQL string
CA_VariantSetCString (&source, "SELECT * FROM mytable"));
// Open recordset
ADODB__RecordsetOpen (rsHandle, &err, source, dbConnection, ADODBConst_adOpenStatic,ADODBConst_adLockReadOnly, -1));
// close recordset
ADODB__RecordsetClose (rsHandle, NULL);
// Clear SQL variant
CA_VariantClear(&source);
// Discard recordset object
CA_DiscardObjHandle(rsHandle);
}

// Clear connection variant
CA_VariantClear(&dbConnection);
// Close connection
ADODB__ConnectionClose(connectionHandle, NULL);
// Discard connection object
CA_DiscardObjHandle(connectionHandle);

Thanks in advance for your help.
Mano
0 Kudos
Message 3 of 5
(3,346 Views)
I tried the sample you sent with ADO 2.7 and CVI 7 and XP Pro, and I noticed that when I started the loop, the memory did go up. But it went back to down after I got done with the loop. The memory might not be released immediately, but it does go back to its normal level after all the handles are released. Try updating to the newest ado. I found alot of references on google about leaks with ADO, perhaps this is one of the behaviors you noticed. Also make sure you have all the service packs and patches for Windows 2k.

Bilal
Bilal Durrani
NI
0 Kudos
Message 4 of 5
(3,346 Views)
Hello Bilal
Thank you for your message. My problem is that I am running the code with in the loop in a server which runs continuosly for days and I cannot break the loop while the server is running. Like you noticed the memory keeps on increasing day by day.

But I am not seeing the same problem when I run the project in VC++. If the problem is due to ADO, then I should be able to reproduce it in VC ++. Is it not?

I am developing a wrapper dll in VC++ for the ADO. I will use it in my CVI project to see if I have the same problem.

Thanks
Mano
0 Kudos
Message 5 of 5
(3,346 Views)