Prevent possible segfault when copying image