diff options
author | Tavian Barnes <tavianator@gmail.com> | 2009-09-10 19:59:09 +0000 |
---|---|---|
committer | Tavian Barnes <tavianator@gmail.com> | 2009-09-10 19:59:09 +0000 |
commit | 29de9f06dbfeae9006aa711d3aa2ef785e622eef (patch) | |
tree | 75e19f8f495364f14e688e0e63d12cd08ffb7a02 | |
parent | 080d202c75f0c0887d11daf5186ee51c1e82b6c3 (diff) | |
download | dimension-29de9f06dbfeae9006aa711d3aa2ef785e622eef.tar.xz |
FILE_Cookie fixes.
-rw-r--r-- | libdimensionxx/cookie-fopencookie.cpp | 45 | ||||
-rw-r--r-- | libdimensionxx/cookie-tmpfile.cpp | 12 | ||||
-rw-r--r-- | 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<void*>(this), "r", io_funcs)); + file(fopencookie(reinterpret_cast<void*>(static_cast<FILE_Cookie*>(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<void*>(this), "w", io_funcs)); + file(fopencookie(reinterpret_cast<void*>(static_cast<FILE_Cookie*>(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<void*>(this), "r+", io_funcs)); + file(fopencookie(reinterpret_cast<void*>(static_cast<FILE_Cookie*>(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(); }; } |