About SQLCE3.0 OLEDB (memory Leak?)
Oct 9, 2007Env. eVC++4.0 / SQLCE3.0 / PDA OS : WinCE5.0
I modifed some sample source (NorthwindDB and downloaded source... etc)
It looks like working very well.. but system memory continuously increase when execute query.
So, I tested memory usage after ConnectDB and DisConnectDB. (just connect/disconnect)
Result, memory increased similarity.
Before system memory usage : 13068 KB
1st.
After ConnectDB : 14160 KB
After DisconnectDB : 13156 KB (increase about 88KB)
2nd.
After ConnectDB : 14164 KB
After DisconnectDB : 13160 KB
3rd.
After ConnectDB : 14168 KB
After DisconnectDB : 13164 KB
I don't know what has caused the problem.
Needs your comment... Thanks in advance.
Code Block
// GLOBAL VARIABLE
// Provider Interfaces
IDBInitialize *pIDBInitialize = NULL;
IUnknown *pIUnknownSession = NULL;
IDBCreateSession *pIDBCreateSession = NULL;
IDBProperties *pIDBProperties = NULL;
// SQL query command text handle object
IDBCreateCommand *pIDBCrtCmd = NULL;
ICommandText *pICmdText = NULL;
HRESULT CPBCls::ConnectDB(LPTSTR lpszDBName)
{
HRESULT hr = NOERROR;
DBPROPSET dbpropset[2]; // Property Set used to initialize provider
DBPROP dbprop[2]; // property array used in property set to initialize provider
DBPROP sscedbprop[1];
// Create the SQL Server CE provider
hr = CoCreateInstance(CLSID_SQLSERVERCE_3_0,
0,
CLSCTX_INPROC_SERVER,
IID_IDBInitialize,
(void**)&pIDBInitialize);
if(FAILED(hr))
AfxMessageBox(_T("CoCreateInstance Failed!!"));
VariantInit(&sscedbprop[0].vValue);
VariantInit(&dbprop[0].vValue);
VariantInit(&dbprop[1].vValue);
// Initialize a property with name of database
dbprop[0].dwPropertyID = DBPROP_INIT_DATASOURCE;
dbprop[0].dwOptions = DBPROPOPTIONS_REQUIRED;
dbprop[0].vValue.vt = VT_BSTR;
dbprop[0].vValue.bstrVal = SysAllocString(lpszDBName);
// Validation
if(NULL == dbprop[0].vValue.bstrVal)
{
hr = E_OUTOFMEMORY;
goto CleanExit;
}
// Specify the property with name of the database
dbprop[1].dwPropertyID = DBPROP_INIT_MODE;
dbprop[1].dwOptions = DBPROPOPTIONS_REQUIRED;
dbprop[1].vValue.vt = VT_I4;
dbprop[1].vValue.lVal = DB_MODE_READWRITE;
// Specify the property for TRANSACTION COMMIT MODE --------- it seems does not work
// sscedbprop[0].dwPropertyID = DBPROP_SSCE_TRANSACTION_COMMIT_MODE;
// sscedbprop[0].dwOptions = DBPROPOPTIONS_REQUIRED;
// sscedbprop[0].vValue.vt = VT_I4;
// sscedbprop[0].vValue.lVal = DBPROPVAL_SSCE_TCM_FLUSH;
sscedbprop[0].dwPropertyID = DBPROP_SSCE_FLUSH_INTERVAL;
sscedbprop[0].dwOptions = DBPROPOPTIONS_REQUIRED;
sscedbprop[0].vValue.vt = VT_I4;
sscedbprop[0].vValue.lVal = 1;
// Initialize the property set
dbpropset[0].guidPropertySet = DBPROPSET_DBINIT;
dbpropset[0].rgProperties = dbprop;
dbpropset[0].cProperties = sizeof(dbprop)/sizeof(dbprop[0]);
//Initialize property set DBPROPSET_SSCE_SESSION --------- it seems does not work
// dbpropset[1].guidPropertySet = DBPROPSET_SSCE_SESSION;
// dbpropset[1].rgProperties = sscedbprop;
// dbpropset[1].cProperties = sizeof(sscedbprop)/sizeof(sscedbprop[0]);
dbpropset[1].guidPropertySet = DBPROPSET_SSCE_DBINIT;
dbpropset[1].rgProperties = sscedbprop;
dbpropset[1].cProperties = sizeof(sscedbprop)/sizeof(sscedbprop[0]);
// Set the properties into the provider's data source object.
hr = pIDBInitialize->QueryInterface(IID_IDBProperties, (void **)&pIDBProperties);
if(FAILED(hr))
{
goto CleanExit;
}
hr = pIDBProperties->SetProperties(sizeof(dbpropset)/sizeof(dbpropset[0]), dbpropset);
if(FAILED(hr))
{
goto CleanExit;
}
// Free the used memory
SysFreeString(dbprop[0].vValue.bstrVal);
// Initialize the SQL Server CE provider.
hr = pIDBInitialize->Initialize();
if(FAILED(hr))
{
goto CleanExit;
}
// Create new database session...
hr = pIDBInitialize->QueryInterface(IID_IDBCreateSession, (void **)&pIDBCreateSession);
if(FAILED(hr))
{
goto CleanExit;
}
hr = pIDBCreateSession->CreateSession(NULL, IID_IUnknown, &pIUnknownSession);
if (FAILED(hr))
{
goto CleanExit;
}
CleanExit:
// Release the used memory
VariantClear(&dbprop[0].vValue);
VariantClear(&dbprop[1].vValue);
VariantClear(&sscedbprop[0].vValue);
free(&dbprop[0].colid);
free(&dbprop[1].colid);
free(&sscedbprop[0].colid);
free(&dbpropset[0].rgProperties);
free(&dbpropset[1].rgProperties);
free(&dbpropset[0]);
free(&dbpropset[1]);
CoTaskMemFree(&dbpropset[0].rgProperties);
CoTaskMemFree(&dbpropset[1].rgProperties);
CoTaskMemFree(&dbpropset[0]);
CoTaskMemFree(&dbpropset[1]);
if(pIDBProperties != NULL)
{
pIDBProperties->Release();
pIDBProperties = NULL;
}
if(pIDBInitialize != NULL)
{
pIDBInitialize->Release();
pIDBInitialize = NULL;
}
if (FAILED(hr))
{
DisconnectDB();
}
return hr;
}
HRESULT CPBCls::DisconnectDB(void)
{
if(pIDBCreateSession != NULL)
{
pIDBCreateSession->Release();
pIDBCreateSession = NULL;
}
if(pIUnknownSession != NULL)
{
pIUnknownSession->Release();
pIUnknownSession = NULL;
}
if(pIDBProperties != NULL)
{
pIDBProperties->Release();
pIDBProperties = NULL;
}
if(pIDBCrtCmd != NULL)
{
pIDBCrtCmd->Release();
pIDBCrtCmd = NULL;
}
if(pICmdText != NULL)
{
pICmdText->Release();
pICmdText = NULL;
}
// Release interfaces
if(pIDBInitialize != NULL)
{
pIDBInitialize->Release();
pIDBInitialize = NULL;
}
return S_OK;
}
INT CPBCls::ReadSimpleData(ST_SIMPLE_DATA *strSimpleData, int *iDataCount)
{
HRESULT hr = NOERROR;
// for Rowset Process
INT iTotalRows=0;
CString temp;
CHAR *temp2;
ULONG lColumn = 0;
ULONG lNumCols = 0;
ULONG lCount = 0;
ULONG lNumRowsRetrieved = 0;
ULONG ConsumerBufColOffset = 0;
IAccessor *pIAccessor = NULL;
IColumnsInfo *pIColumnsInfo = NULL;
DBCOLUMNINFO *pDBColumnInfo = NULL;
DBBINDING *pBindings = NULL;
HACCESSOR hAccessor = NULL;
HROW hRows[10];
HROW *pRows = &hRows[0];
BYTE *pBuffer = NULL;
WCHAR *pStringsBuffer = NULL;
TCHAR szBuff [2048] = {NULL};
ConnectDB();
memset(szBuff, TEXT('