tpunt / pht

A new threading extension for PHP
BSD 3-Clause "New" or "Revised" License
179 stars 14 forks source link

cyclic references will leak? #5

Open ClosetGeek-Git opened 5 years ago

ClosetGeek-Git commented 5 years ago

The README states "cyclic references will leak" in the data structure section. Can you detail this further?

tpunt commented 5 years ago

Yeah sure. Cyclic references occur when you nest an object (or an array reference) within itself. In normal PHP code, this looks something like the following:

$o = new StdClass();
$o->prop = $o;

So the prop property references the object itself, forming a cycle. PHP has a mark-and-sweep cycle detector to handle these cycles, but I haven't written this for pht (and I don't really plan to, in the interest of simplicity). So if you do something like the following in pht:

$queue = new Queue();
$queue->push($queue);

Then this will leak, since the extension handles garbage collection of these object across threads (so it avoids using PHP's garbage collection mechanism).

Due to the simplicity of the data structures, I don't see any strong reasons as to why cyclic referencing would be needed anyway.