Repository navigation
Description
I found that there would be memory leaks when a scene is being imported through aiImportFileExWithProperties() with a custom IO system.
If we pass aiFileIO to aiImportFileExWithProperties(), then the import function opens the model file using a custom IO system, CIOSystemWrapper, that takes aiFileIO pointer. aiFileIO contains two user defined callback functions (for file opening and closing) and a char pointer for auxiliary user data. Then, CIOSystemWrapper::Open() opens the model file using the user defined callback function, and returns a file instance within boost::scoped_ptr so that the file instance is automatically freed when it runs out of scope.
However, destructing the IOStream instance is not able to release all the resource that is allocated by the file opening callback function. In order to deallocate all the resources, we should call the file closing callback function by calling CIOSystemWrapper::Close().
Here I copied some code segments of Assimp relevant to what I described above:
const aiScene* aiImportFileExWithProperties( const char* pFile, unsigned int pFlags,
aiFileIO* pFS,
const aiPropertyStore* props)
{
...
// setup a custom IO system if necessary
if (pFS) {
imp->SetIOHandler( new CIOSystemWrapper (pFS) );
}
// and have it read the file
scene = imp->ReadFile( pFile, pFlags);
...
return scene;
}
ColladaParser::ColladaParser( IOSystem* pIOHandler, const std::string& pFile)
: mFileName( pFile)
{
mRootNode = NULL;
mUnitSize = 1.0f;
mUpDirection = UP_Y;
// We assume the newest file format by default
mFormat = FV_1_5_n;
// open the file
boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile));
if( file.get() == NULL)
throw DeadlyImportError( "Failed to open file " + pFile + ".");
// generate a XML reader for it
boost::scoped_ptr<CIrrXML_IOStreamReader> mIOWrapper( new CIrrXML_IOStreamReader( file.get()));
mReader = irr::io::createIrrXMLReader( mIOWrapper.get());
if( !mReader)
ThrowException( "Collada: Unable to open file.");
// start reading
ReadContents();
// Possible MEMORY LEAKS here since pIOHandler->Close( pFile) is not called.
}
class CIOSystemWrapper : public IOSystem
{
public:
...
// ...................................................................
IOStream* Open(const char* pFile,const char* pMode = "rb") {
aiFile* p = mFileSystem->OpenProc(mFileSystem,pFile,pMode);
if (!p) {
return NULL;
}
return new CIOStreamWrapper(p);
}
// ...................................................................
void Close( IOStream* pFile) {
if (!pFile) {
return;
}
mFileSystem->CloseProc(mFileSystem,((CIOStreamWrapper*) pFile)->mFile);
delete pFile;
}
...
};So I would like to propose to call CIOSystemWrapper::Close() whenever the file instance is created by CIOSystemWrapper::Open(). As an example:
ColladaParser::ColladaParser( IOSystem* pIOHandler, const std::string& pFile)
: mFileName( pFile)
{
mRootNode = NULL;
mUnitSize = 1.0f;
mUpDirection = UP_Y;
// We assume the newest file format by default
mFormat = FV_1_5_n;
// open the file
IOStream* file = pIOHandler->Open( pFile);
if( file == NULL)
throw DeadlyImportError( "Failed to open file " + pFile + ".");
// generate a XML reader for it
boost::scoped_ptr<CIrrXML_IOStreamReader> mIOWrapper( new CIrrXML_IOStreamReader( file));
mReader = irr::io::createIrrXMLReader( mIOWrapper.get());
if( !mReader)
ThrowException( "Collada: Unable to open file.");
// start reading
ReadContents();
// close the file
pIOHandler->Close(file);
}Please let me know if I'm missing something here.
Activity
/issues/702: fix resource leak and use initializer list for all
kimkulling commentedon Dec 6, 2015
Fixed on latest master!
jslee02 commentedon Dec 6, 2015
Thank you for the quick fix! 👍
PaulSJ commentedon Aug 9, 2016
This also happens in XFileImporter.cpp:112 in XFileImporter::InternReadFile the file is opened but not explicitly closed.
Update CMakeLists.txt
Update CMakeLists.txt (#5379)
Update CMakeLists.txt (#5379)