Fixing managed pointer corruption bug