Skip to content
Snippets Groups Projects
Commit 0a559644 authored by Jakub Klinkovský's avatar Jakub Klinkovský
Browse files

The File class should close itself from the destructor

Doing this in a destructor is the only place where we can guarantee that
no open file descriptors will be left behind.
parent 17bac77e
No related branches found
No related tags found
No related merge requests found
...@@ -23,6 +23,14 @@ File :: File() ...@@ -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, bool File :: open( const String& fileName,
const tnlIOMode mode ) const tnlIOMode mode )
{ {
...@@ -60,6 +68,11 @@ bool File :: close() ...@@ -60,6 +68,11 @@ bool File :: close()
std::cerr << "I was not able to close the file " << fileName << " properly!" << std::endl; std::cerr << "I was not able to close the file " << fileName << " properly!" << std::endl;
return false; return false;
} }
// reset all attributes
mode = tnlUndefinedMode;
file = NULL;
fileOK = false;
fileName = "";
readElements = writtenElements = 0; readElements = writtenElements = 0;
return true; return true;
}; };
......
...@@ -57,6 +57,8 @@ class File ...@@ -57,6 +57,8 @@ class File
File(); File();
~File();
bool open( const String& fileName, bool open( const String& fileName,
const tnlIOMode mode ); const tnlIOMode mode );
......
...@@ -85,10 +85,7 @@ bool Object :: save( const String& fileName ) const ...@@ -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; std::cerr << "I am not bale to open the file " << fileName << " for writing." << std::endl;
return false; return false;
} }
const bool status = this->save( file ); return this->save( file );
if( ! file. close() )
std::cerr << "An error occurred when I was closing the file " << fileName << "." << std::endl;
return status;
} }
bool Object :: load( const String& fileName ) bool Object :: load( const String& fileName )
...@@ -99,10 +96,7 @@ 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; std::cerr << "I am not bale to open the file " << fileName << " for reading." << std::endl;
return false; return false;
} }
const bool status = this->load( file ); return this->load( file );
if( ! file. close() )
std::cerr << "An error occurred when I was closing the file " << fileName << "." << std::endl;
return status;
} }
bool Object :: boundLoad( const String& fileName ) bool Object :: boundLoad( const String& fileName )
...@@ -113,14 +107,7 @@ 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; std::cerr << "I am not bale to open the file " << fileName << " for reading." << std::endl;
return false; return false;
} }
if( ! this->boundLoad( file ) ) return 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;
} }
void Object::setDeprecatedReadMode() void Object::setDeprecatedReadMode()
...@@ -155,9 +142,7 @@ bool getObjectType( const String& fileName, String& type ) ...@@ -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; std::cerr << "I am not able to open the file " << fileName << " for detecting the object inside!" << std::endl;
return false; return false;
} }
bool ret_val = getObjectType( binaryFile, type ); return getObjectType( binaryFile, type );
binaryFile. close();
return ret_val;
} }
bool parseObjectType( const String& objectType, bool parseObjectType( const String& objectType,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment