Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

_ConnectionPtr - Handle Leaks!!! Help!

Status
Not open for further replies.

yteh

Programmer
Dec 2, 2002
4
US
I'm using the _ConnectionPtr smartpointer in one of my programs and I've noticed major Handle leaks every time I open up a connection. I'm making sure that I'm closing, releasing, and setting the connection to NULL once I'm done with it. This program is a service and has to run 24/7 and these connections are made almost every 2 minutes.

Here's a snippet of what my code looks like:
try
{
m_bstrMessage);m_adoConnPtr.CreateInstance (__uuidof Connection));
m_adoConnPtr->Open (_bstr_t (strConnection), bstrAppUserName, bstrAppPass, adConnectUnspecified);
}
catch (_com_error &e)
{
return false;
}
catch (...)
{
return false;
}
if (m_adoConnPtr.GetInterfacePtr () != NULL)
{
if (m_adoConnPtr->State == adStateOpen)
{
m_adoConnPtr->Close ();
}
m_adoConnPtr.Release ();
m_adoConnPtr = NULL;
}

Any ideas? Help is greatly appreciated
 
Don't call Release on a smart pointer!

Also i can't tell for sure from your code but you probably don't want the connection smart pointer to even be a member variable. Just create one locally and use it then let it go out of scope. That's what the smart pointer design is for.

I use ADO smart pointers in Services and have no problems I have a service that runs 24/7 for over almost 2 years without a single problem.

-pete
 
First of all, thanks for your reply.

My example above uses the connection smart pointer as a member variable. Anyway, I've taken your suggestions into consideration but I'm still unable to get ride of the handle leaks.

Here's a snippet of my new code:
_ConnectionPtr l_adoConnPtr = NULL;

if (FAILED :):CoInitialize (NULL)))
{
return false;
}

// Get connection
try
{
l_adoConnPtr.CreateInstance (__uuidof (Connection));
l_adoConnPtr->Open ("Provider=Sybase.ASEOLEDBProvider;Persist Security Info=True;Data Source=SOURCE", "USERNAME", "PASS", adConnectUnspecified);
}
catch (_com_error &e)
{
m_bstrMessage = "Database error: " + (_bstr_t) (char *)e.Description ();
return false;
}
catch (...)
{
m_bstrMessage = "Unhandled exception creating database connection.";
return false;
}

if (l_adoConnPtr.GetInterfacePtr () != NULL)
{
if (l_adoConnPtr->State == adStateOpen)
{
l_adoConnPtr->Close ();
}
l_adoConnPtr = NULL;
}
::CoUninitialize ();

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top