#2525 fix for detachAll so it supports cycles

I have made it so that the public api hasn't changed. Added a map that is carried along during a single detachAll call which tracks objects that have already been detached and reuses those. 

I have written a unit test to test it and I have already tried the code in my own setup and it looks like it is working fine.