Fix memory corruption bug while listing directory