uiteoi/node-sqlserver — pull request #2 by uiteoi (2012-07-27 17:50:11+00:00)
Title: Fix connection memory leak

This is a response to a memory leak that was witnessed in our stress
testing.

Below is a list of reading that should illuminate this change:
http://create.tpsitulsa.com/wiki/V8/Persistent_Handles

https://developers.google.com/v8/embed#handles

Basically, we were using a persistent handle to hold a reference to
ourselves in the Connection class. This prevented the Collection
class from being collected properly. The solution is to use a weak
persistent handle.

The node::ObjectWrap class, which the Connection class inherits from,
is a WeakHandle and manages the lifetime properly, calling the
destructor (provided it is virtual) of our instance when it is being
collected. A destructor was added to the Connection class that closes
the ODBC connection if necessary.

Since there are now two paths into closing a connection, it is
possible for a race condition to arise. A critical section has been
put around the code that disconnects from SQL Server to prevent this.

For now this critical section code is Windows specific. It will be
rewritten as cross platform at a later time.

Also, we were potentially leaking a persistent handle to the
OdbcConnection class in the OpenOperation class, meaning that the
OpenConnection class would never be collected. To fix this, we call
the Dispose member function of the persistent handle to notify V8 that
we no longer need the pointer.

Microsoft Internal Review Id: jaykint-559c37537fb54de28ea1481d9bdf55cb