From b37787a4140d2b993e35ae1d0b019d0af04c2fd1 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Sat, 7 Oct 2017 19:14:18 -0400 Subject: [PATCH] Ensures lifetime-linked track flushing without relying on virtual calls within a destructor. --- Storage/Disk/Disk.hpp | 4 +--- Storage/Disk/Drive.cpp | 5 +++++ Storage/Disk/Drive.hpp | 1 + 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Storage/Disk/Disk.hpp b/Storage/Disk/Disk.hpp index 95e5bc4a9..c929bff3a 100644 --- a/Storage/Disk/Disk.hpp +++ b/Storage/Disk/Disk.hpp @@ -23,9 +23,7 @@ namespace Disk { class Disk { public: - virtual ~Disk() { - flush_tracks(); - } + virtual ~Disk() {} /*! @returns the number of discrete positions that this disk uses to model its complete surface area. diff --git a/Storage/Disk/Drive.cpp b/Storage/Disk/Drive.cpp index 14866185f..e94df592a 100644 --- a/Storage/Disk/Drive.cpp +++ b/Storage/Disk/Drive.cpp @@ -21,7 +21,12 @@ Drive::Drive(unsigned int input_clock_rate, int revolutions_per_minute, int numb available_heads_(number_of_heads) { } +Drive::~Drive() { + if(disk_) disk_->flush_tracks(); +} + void Drive::set_disk(const std::shared_ptr &disk) { + if(disk_) disk_->flush_tracks(); disk_ = disk; has_disk_ = !!disk_; diff --git a/Storage/Disk/Drive.hpp b/Storage/Disk/Drive.hpp index 266ad7d54..7f321068c 100644 --- a/Storage/Disk/Drive.hpp +++ b/Storage/Disk/Drive.hpp @@ -24,6 +24,7 @@ namespace Disk { class Drive: public Sleeper, public TimedEventLoop { public: Drive(unsigned int input_clock_rate, int revolutions_per_minute, int number_of_heads); + ~Drive(); /*! Replaces whatever is in the drive with @c disk.