From 29de9f06dbfeae9006aa711d3aa2ef785e622eef Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 10 Sep 2009 19:59:09 +0000 Subject: FILE_Cookie fixes. --- libdimensionxx/cookie-fopencookie.cpp | 45 ++++++++++++++++++++++++++++------- libdimensionxx/cookie-tmpfile.cpp | 12 ++++++++-- libdimensionxx/dimensionxx/cookie.hpp | 4 ++-- 3 files changed, 49 insertions(+), 12 deletions(-) diff --git a/libdimensionxx/cookie-fopencookie.cpp b/libdimensionxx/cookie-fopencookie.cpp index b44b5dd..843cc45 100644 --- a/libdimensionxx/cookie-fopencookie.cpp +++ b/libdimensionxx/cookie-fopencookie.cpp @@ -34,8 +34,14 @@ namespace Dimension { - // FILE_Cookie pure virtual destructor: close the file - FILE_Cookie::~FILE_Cookie() { std::fclose(m_file); } + // FILE_Cookie pure virtual destructor + FILE_Cookie::~FILE_Cookie() { + if (m_file) { + // Bug if neither ~iFILE_Cookie nor ~oFILE_Cookie closed the FILE* + dmnsn_error(DMNSN_SEVERITY_HIGH, + "FILE* not closed by FILE_Cookie destructor"); + } + } namespace { @@ -51,7 +57,7 @@ namespace Dimension if (ifcookie.istr().eof() || ifcookie.istr().good()) { return ifcookie.istr().gcount(); // This returns 0 on an immediate EOF - // for us. + // for us. } else { // Some non-EOF error return -1; @@ -140,7 +146,18 @@ namespace Dimension io_funcs.close = 0; // Set the FILE* - file(fopencookie(reinterpret_cast(this), "r", io_funcs)); + file(fopencookie(reinterpret_cast(static_cast(this)), + "r", io_funcs)); + } + + // iFILE_Cookie destructor + iFILE_Cookie::~iFILE_Cookie() + { + if (file()) { + // If the FILE* is open, close it and set it to NULL + fclose(file()); + file(0); + } } // Make an output FILE_Cookie @@ -154,11 +171,19 @@ namespace Dimension io_funcs.close = 0; // Set the FILE* - file(fopencookie(reinterpret_cast(this), "w", io_funcs)); + file(fopencookie(reinterpret_cast(static_cast(this)), + "w", io_funcs)); } - // No-op oFILE_Cookie destructor - oFILE_Cookie::~oFILE_Cookie() { } + // oFILE_Cookie destructor + oFILE_Cookie::~oFILE_Cookie() + { + if (file()) { + // If the FILE* is open, close it and set it to NULL + fclose(file()); + file(0); + } + } // Make an I/O FILE_Cookie ioFILE_Cookie::ioFILE_Cookie(std::iostream& iostr) @@ -170,6 +195,10 @@ namespace Dimension io_funcs.seek = &cookie_seek; io_funcs.close = 0; - file(fopencookie(reinterpret_cast(this), "r+", io_funcs)); + file(fopencookie(reinterpret_cast(static_cast(this)), + "r+", io_funcs)); } + + // No-op ioFILE_Cookie destructor + ioFILE_Cookie::~ioFILE_Cookie() { } } diff --git a/libdimensionxx/cookie-tmpfile.cpp b/libdimensionxx/cookie-tmpfile.cpp index 9e513e6..784e984 100644 --- a/libdimensionxx/cookie-tmpfile.cpp +++ b/libdimensionxx/cookie-tmpfile.cpp @@ -25,8 +25,10 @@ namespace Dimension { - // Close the tmpfile - FILE_Cookie::~FILE_Cookie() { std::fclose(m_file); } + // FILE_Cookie pure virtual destructor + FILE_Cookie::~FILE_Cookie() { + fclose(file()); + } namespace { @@ -110,6 +112,9 @@ namespace Dimension file(tmp); } + // No-op iFILE_Cookie destructor + iFILE_Cookie::~iFILE_Cookie() { } + // Make an output FILE_Cookie oFILE_Cookie::oFILE_Cookie(std::ostream& ostr) : m_ostr(&ostr) @@ -146,4 +151,7 @@ namespace Dimension // Set the FILE* file(tmp); } + + // No-op ioFILE_Cookie destructor + ioFILE_Cookie::~ioFILE_Cookie() { } } diff --git a/libdimensionxx/dimensionxx/cookie.hpp b/libdimensionxx/dimensionxx/cookie.hpp index d5b042e..7df88c8 100644 --- a/libdimensionxx/dimensionxx/cookie.hpp +++ b/libdimensionxx/dimensionxx/cookie.hpp @@ -59,7 +59,7 @@ namespace Dimension { public: iFILE_Cookie(std::istream& istr); - // virtual ~iFILE_Cookie(); + virtual ~iFILE_Cookie(); // Get the C++ streams std::istream& istr() { return *m_istr; } @@ -95,7 +95,7 @@ namespace Dimension { public: ioFILE_Cookie(std::iostream& iostr); - // virtual ~ioFILE_Cookie(); + virtual ~ioFILE_Cookie(); }; } -- cgit v1.2.3