1
\$\begingroup\$

In order to convert a database type DB_NUMERIC to real, I noticed that a call to IDataConvert::Release() does not return 0, which is indicative of some additional pointers to the object dangling somewhere. In order to free the object, I would need a loop, which in my case has exactly 19 iterations. Or can it be IDataConvert is actually stack based? In which case I think it is odd that it would retain a reference count at all instead of Release() returning a fixed value. One more Release() at the end does not crash the program, suggesting a stack based object. Of course I don't want my program to leak memory, so would it be safe to ignore the problem and call Release() just once? And of course it is bad to release pointers that you do not own...

HRESULT hr;
IDataConvert* pConv;
hr = CoCreateInstance(CLSID_OLEDB_CONVERSIONLIBRARY, NULL, CLSCTX_INPROC_SERVER, IID_IDataConvert, (LPVOID*)&pConv);
hr = pConv->CanConvert(DBTYPE_NUMERIC, DBTYPE_R8);
hr = pConv->DataConvert(DBTYPE_NUMERIC, DBTYPE_R8,  (ULONG)sizeof(DB_NUMERIC), &len, (void*)&test.m_cur, (void*)&result, sizeof(result), DBSTATUS_S_OK, &status, test.m_cur.precision, test.m_cur.scale, DBDATACONVERT_DEFAULT);
UINT n;
int i = 0;
while (n = pConv->Release()) // this strange looking code seems the only way to make Release() return 0
{
    i++;
}
//n = pConv->Release(); // for testing purpose only, an extra release does not crash the program. 
\$\endgroup\$
9
  • 1
    \$\begingroup\$ Welcome to Code Review! Frankly, I'm a little confused; does your code work? \$\endgroup\$ – SirPython Mar 26 '16 at 20:54
  • \$\begingroup\$ It does work. I did not want to put this on Stack Overflow because it is not really a question about a problem that I have to solve. For now, I will just assume that the object is constructed on the stack, so a single Release() should do. Thanks anyway for looking at it. \$\endgroup\$ – Bart Mar 29 '16 at 5:29
  • \$\begingroup\$ int i = 0; while (n = pConv->Release()) { i++; } Are you just creating a round-about near-infinite loop? Your compiler doesn't optimize this away? \$\endgroup\$ – Mast Jul 29 '19 at 17:59
  • \$\begingroup\$ @Mast, I don't think the compiler will optimize this, because it can't make assumptions about what is inside the Release() function, which resides inside a system dll. \$\endgroup\$ – Bart Jul 30 '19 at 6:36
  • \$\begingroup\$ By the way, it appears I speak Russian too: qacode.ru/questions/32169/… It is a (possibly plagiarizing) translation of my question in Russian. Maybe a moderator could look into this. \$\endgroup\$ – Bart Jul 30 '19 at 6:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.