diff --git a/src/TNL/File.cpp b/src/TNL/File.cpp index d6e16b17ac89c0d0cd5c9b3f262f47b7eb2ad28c..3b5fe35141c10a76542c5dc2712e685b8ddec576 100644 --- a/src/TNL/File.cpp +++ b/src/TNL/File.cpp @@ -23,6 +23,14 @@ File :: File() { } +File :: ~File() +{ + // destroying a file without closing is a memory leak + // (an open file descriptor is left behind, on Linux there is typically + // only a limited number of descriptors available to each process) + close(); +} + bool File :: open( const String& fileName, const tnlIOMode mode ) { @@ -60,6 +68,11 @@ bool File :: close() std::cerr << "I was not able to close the file " << fileName << " properly!" << std::endl; return false; } + // reset all attributes + mode = tnlUndefinedMode; + file = NULL; + fileOK = false; + fileName = ""; readElements = writtenElements = 0; return true; }; diff --git a/src/TNL/File.h b/src/TNL/File.h index 1eea42eae61b67867bfeb7218b0a1801932f2c14..67a81b8b82b7531c7f6cf2c8d00c9bc20105d815 100644 --- a/src/TNL/File.h +++ b/src/TNL/File.h @@ -57,6 +57,8 @@ class File File(); + ~File(); + bool open( const String& fileName, const tnlIOMode mode ); diff --git a/src/TNL/Object.cpp b/src/TNL/Object.cpp index 74a4bbb7a18f3658b6815fc60525cd8a6a04fce4..b3ad74f10a7728ce8a1bceb9fe99d7c2528a1ba2 100644 --- a/src/TNL/Object.cpp +++ b/src/TNL/Object.cpp @@ -85,10 +85,7 @@ bool Object :: save( const String& fileName ) const std::cerr << "I am not bale to open the file " << fileName << " for writing." << std::endl; return false; } - const bool status = this->save( file ); - if( ! file. close() ) - std::cerr << "An error occurred when I was closing the file " << fileName << "." << std::endl; - return status; + return this->save( file ); } bool Object :: load( const String& fileName ) @@ -99,10 +96,7 @@ bool Object :: load( const String& fileName ) std::cerr << "I am not bale to open the file " << fileName << " for reading." << std::endl; return false; } - const bool status = this->load( file ); - if( ! file. close() ) - std::cerr << "An error occurred when I was closing the file " << fileName << "." << std::endl; - return status; + return this->load( file ); } bool Object :: boundLoad( const String& fileName ) @@ -113,14 +107,7 @@ bool Object :: boundLoad( const String& fileName ) std::cerr << "I am not bale to open the file " << fileName << " for reading." << std::endl; return false; } - if( ! this->boundLoad( file ) ) - return false; - if( ! file. close() ) - { - std::cerr << "An error occurred when I was closing the file " << fileName << "." << std::endl; - return false; - } - return true; + return this->boundLoad( file ); } void Object::setDeprecatedReadMode() @@ -155,9 +142,7 @@ bool getObjectType( const String& fileName, String& type ) std::cerr << "I am not able to open the file " << fileName << " for detecting the object inside!" << std::endl; return false; } - bool ret_val = getObjectType( binaryFile, type ); - binaryFile. close(); - return ret_val; + return getObjectType( binaryFile, type ); } bool parseObjectType( const String& objectType,