Skip to content

Possible memory leaks with custom IO system #702

Closed

Description

@jslee02

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

  1. added a commit that references this issue on Dec 6, 2015
  2. kimkulling commented on Dec 6, 2015

    @kimkulling
    Member

    Fixed on latest master!

  3. jslee02 commented on Dec 6, 2015

    @jslee02
    ContributorAuthor

    Thank you for the quick fix! 👍

  4. PaulSJ commented on Aug 9, 2016

    @PaulSJ

    This also happens in XFileImporter.cpp:112 in XFileImporter::InternReadFile the file is opened but not explicitly closed.

  5. added a commit that references this issue on Dec 11, 2023
  6. added a commit that references this issue on Feb 5, 2024
  7. added
    Leak
    on Feb 1, 2026
  8. added a commit that references this issue on Jul 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Leak

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions