nem0 / OpenFBX

Lightweight open source FBX importer
MIT License
1.15k stars 135 forks source link

Memory Leak #55

Closed maxattack closed 4 years ago

maxattack commented 4 years ago

Visual Studio reported memory leaks when I integrated OpenFBX into my hobby engine. Upon investigation, I've identified the problem.

Objects are allocated using Scene's m_allocator which releases the Object memory when a scene is destroyed, but doesn't call destructors, and therefore doesn't release memory that's allocated by objects outside of the allocator.

In particular ofbx.add() allocates GeometryImpl::NewVertex records with vanilla operator new, but because GeometryImpl's destructor is never called, these records are never deallocated.

One possible fix is to manually call object destructors in scene's destructor:

~Scene() override 
{
    for(auto ptr : m_all_objects)
        ptr->~Object();
}

I've confirmed that this fixes all outstanding memory leaks in the latest version, and Visual Studio has stopped complaining :)

mafiesto4 commented 4 years ago

I confirm the leak. The provided code fixes the problem.

nem0 commented 4 years ago

That's what I get for using stl, fixed in ee23e88f2d4534d1ffbdd3c039ffe97e061779cc