Fix CPP implementation to prevent the callback function from getting GC'd