I'm not really very familiar with retain cycles in Node.js coming more from Objective-C/iOS, but I wrote a bit of code that I'm hoping will not lead to a retain cycle. I'm not sure how intelligently V8 deals with garbage collection or how intelligent I'm supposed to be at this!
In my example, I create an object that is supposed to handle making a data connection and feeding that data back to the parent object. In order to do this, I use the EventEmitter .on functions to register parent object functions. The parent object gets released when all of the data is processed, but after that will this create a retain cycle between the parent and the child objects? Sample code
// Create the SonarData with the necessary information
this.sonardata = new SonarData( this.key, this.size )
// Hook events
.on( 'error', this.handleError.bind( this ) )
.on( 'done', this.handleDone.bind( this ) )
.on( 'data', this.handleData.bind( this ) );
When the parent object is released, if there are no other references to the child object and no active message handlers on the child object, then the child object will be garbage collected just fine. Javascript garbage collectors don't have issues with retain cycles. They work by whether code is still reachable by any other active code or not.
So, two objects that each refer to each other, but neither of which are reachable by other code will get successfully garbage collected.