From c3b2f29288c8687cab2554889c5d9b3690e3ef86 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 18 Oct 2018 12:27:09 +0700 Subject: [PATCH] add FileIO operator = to prevent memory leak --- libraries/FileSystem/src/FileIO.cpp | 12 +++++++++++- libraries/FileSystem/src/FileIO.h | 1 + 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/libraries/FileSystem/src/FileIO.cpp b/libraries/FileSystem/src/FileIO.cpp index 92e63704..fe37e143 100644 --- a/libraries/FileSystem/src/FileIO.cpp +++ b/libraries/FileSystem/src/FileIO.cpp @@ -58,6 +58,16 @@ File::File (char const *filename, uint8_t mode, FileSystemClass &fs) open(filename, mode); } +File& File::operator = (const File &rhs) +{ + // close if currently opened + if ( _hdl ) close(); + + memcpy(this, &rhs, sizeof(File)); + + return *this; +} + File::~File () { @@ -139,7 +149,7 @@ void File::close (void) _name = NULL; } -File::operator bool () +File::operator bool (void) { return _hdl != NULL; } diff --git a/libraries/FileSystem/src/FileIO.h b/libraries/FileSystem/src/FileIO.h index 3c2a4e4a..3713671f 100644 --- a/libraries/FileSystem/src/FileIO.h +++ b/libraries/FileSystem/src/FileIO.h @@ -51,6 +51,7 @@ class File: public Stream public: File (FileSystemClass &fs); File (char const *filename, uint8_t mode, FileSystemClass &fs); + File & operator = (const File &rhs); ~File (); bool open (char const *filename, uint8_t mode);