fix image out of memory error. fix race condition.