How to debug leaking a reference?

58 views Asked by At

We have a complex PHP program that's leaking one object. Is there any way to get callback every time reference count is increased so that I could log the stack trace of all locations that take a reference? The program is using RAII programming model and this leak is causing the destructor to be run much much later than expected (in worst case during process exit).

The code basically is

while (!$done)
{
  # create database connection for each try (serializable transactions with fallback servers)
  $connection = ...;
  $transaction = $connection->newTransaction(...);
  try
  {
    doStuffWithTransaction($transaction);
    $transaction->commit();
    $done = true;
    ...; # additional best-effort cleanup routines
  }
  catch (TransactionMustRetry $e)
  {
    $transaction->rollback();
  }
  catch (\Throwable $e)
  {
    ...
  }
  $connection->disconnect();
}

and $transaction is supposed to automatically rollback if \Throwable is thrown by some nested routine within doStuffWithTransaction(). However, the $transaction has refcount=2 before call to this function and 3 when catching the \Throwable which results in the rollback not getting rolled back automatically.

How to figure which code has acquired reference to $transaction?

0

There are 0 answers