fix segmentation faults because of aggressive GC behaviour and lost references