Qt: Flesh out VFileDevice more, to avoid leak

This commit is contained in:
Vicki Pfau 2021-01-30 16:01:06 -08:00
parent 5b8d64b0b5
commit 80fb86a930
2 changed files with 13 additions and 0 deletions

View File

@ -91,6 +91,10 @@ VFileDevice::VFileDevice(const QString& filename, QIODevice::OpenMode mode, QObj
} }
} }
VFileDevice::~VFileDevice() {
close();
}
void VFileDevice::close() { void VFileDevice::close() {
if (!m_vf) { if (!m_vf) {
return; return;
@ -117,6 +121,13 @@ VFileDevice& VFileDevice::operator=(VFile* vf) {
return *this; return *this;
} }
VFile* VFileDevice::take() {
VFile* vf = m_vf;
m_vf = nullptr;
QIODevice::close();
return vf;
}
qint64 VFileDevice::readData(char* data, qint64 maxSize) { qint64 VFileDevice::readData(char* data, qint64 maxSize) {
return m_vf->read(m_vf, data, maxSize); return m_vf->read(m_vf, data, maxSize);
} }

View File

@ -20,6 +20,7 @@ Q_OBJECT
public: public:
VFileDevice(VFile* vf = nullptr, QObject* parent = nullptr); VFileDevice(VFile* vf = nullptr, QObject* parent = nullptr);
VFileDevice(const QString&, QIODevice::OpenMode, QObject* parent = nullptr); VFileDevice(const QString&, QIODevice::OpenMode, QObject* parent = nullptr);
virtual ~VFileDevice();
virtual void close() override; virtual void close() override;
virtual bool seek(qint64 pos) override; virtual bool seek(qint64 pos) override;
@ -29,6 +30,7 @@ public:
VFileDevice& operator=(VFile*); VFileDevice& operator=(VFile*);
operator VFile*() { return m_vf; } operator VFile*() { return m_vf; }
VFile* take();
static VFile* wrap(QIODevice*, QIODevice::OpenMode); static VFile* wrap(QIODevice*, QIODevice::OpenMode);
static VFile* wrap(QFileDevice*, QIODevice::OpenMode); static VFile* wrap(QFileDevice*, QIODevice::OpenMode);