Fix move assignments

- Add self-assignment checks
- Free resources of object which is going to be replaced to avoid
  resource leaks
This commit is contained in:
Dmitry Marakasov 2014-11-30 01:22:22 +03:00
parent 8471330bbb
commit 93a77542d8
8 changed files with 37 additions and 0 deletions

View File

@ -51,6 +51,9 @@ AudioDevice::AudioDevice(AudioDevice&& other) noexcept : device_id_(other.device
}
AudioDevice& AudioDevice::operator=(AudioDevice&& other) noexcept {
if (&other == this)
return *this;
if (device_id_)
SDL_CloseAudioDevice(device_id_);

View File

@ -37,6 +37,12 @@ AudioDevice::LockHandle::LockHandle(AudioDevice::LockHandle&& other) noexcept :
}
AudioDevice::LockHandle& AudioDevice::LockHandle::operator=(AudioDevice::LockHandle&& other) noexcept {
if (&other == this)
return *this;
if (device_ != nullptr)
SDL_UnlockAudioDevice(device_->device_id_);
device_ = other.device_;
other.device_ = nullptr;

View File

@ -149,6 +149,10 @@ RWops::RWops(RWops&& other) noexcept : rwops_(other.rwops_) {
}
RWops& RWops::operator=(RWops&& other) noexcept {
if (&other == this)
return *this;
if (rwops_ != nullptr)
Close();
rwops_ = other.rwops_;
rwops_->hidden.unknown.data2 = static_cast<void*>(this);
other.rwops_ = nullptr;

View File

@ -47,6 +47,10 @@ Renderer::Renderer(Renderer&& other) noexcept : renderer_(other.renderer_) {
}
Renderer& Renderer::operator=(Renderer&& other) noexcept {
if (&other == this)
return *this;
if (renderer_ != nullptr)
SDL_DestroyRenderer(renderer_);
renderer_ = other.renderer_;
other.renderer_ = nullptr;
return *this;

View File

@ -65,6 +65,10 @@ Texture::Texture(Texture&& other) noexcept : texture_(other.texture_) {
}
Texture& Texture::operator=(Texture&& other) noexcept {
if (&other == this)
return *this;
if (texture_ != nullptr)
SDL_DestroyTexture(texture_);
texture_ = other.texture_;
other.texture_ = nullptr;
return *this;

View File

@ -38,6 +38,12 @@ Texture::LockHandle::LockHandle(Texture::LockHandle&& other) noexcept : texture_
}
Texture::LockHandle& Texture::LockHandle::operator=(Texture::LockHandle&& other) noexcept {
if (&other == this)
return *this;
if (texture_ != nullptr)
SDL_UnlockTexture(texture_->Get());
texture_ = other.texture_;
pixels_ = other.pixels_;
pitch_ = other.pitch_;

View File

@ -47,6 +47,12 @@ Wav::Wav(Wav&& other) : audio_buffer_(other.audio_buffer_), audio_length_(other.
}
Wav& Wav::operator=(Wav&& other) {
if (&other == this)
return *this;
if (audio_buffer_ != nullptr)
SDL_FreeWAV(audio_buffer_);
spec_ = std::move(other.spec_);
audio_buffer_ = other.audio_buffer_;
audio_length_ = other.audio_length_;

View File

@ -41,6 +41,10 @@ Window::Window(Window&& other) noexcept : window_(other.window_) {
}
Window& Window::operator=(Window&& other) noexcept {
if (&other == this)
return *this;
if (window_ != nullptr)
SDL_DestroyWindow(window_);
window_ = other.window_;
other.window_ = nullptr;
return *this;