xBimTeam / XbimEssentials

A .NET library to work with data in the IFC format. This is the core component of the Xbim Toolkit
https://xbimteam.github.io/
Other
494 stars 173 forks source link

Resources leak in EsentCursor and EsentModel #521

Closed ChernyshevDS closed 1 year ago

ChernyshevDS commented 1 year ago

EsentCursor does not handle exceptions in its constructor, so when trying to open a malformed xBIM file with some missing tables an exception is raised and database stays open.

Assemblies and versions affected:

Xbim.Essentials 5.1.341 (actually, all of them, I think).

Steps (or code) to reproduce the issue:

Open any large enough IFC model and terminate application when model.SaveAs writes an xbim file:

using (var model = IfcStore.Open("file.ifc"))
{
    model.SaveAs("file.xbim", StorageType.Xbim);
}

You will end up with a malformed xBIM file. An attempt to open it with IfcStore.Open raises an exception EsentObjectNotFoundException: No such table or object. Fair enough. However, the database file stays locked and you are unable to delete it until you close the application.

Expected behavior:

An attempt to open the malformed file should raise an exception, but the database should be closed.

Actual behavior or exception details:

xbim database file is locked even after failed attempt to open it.

Additional Details

I've met this issue because we use xbim on a device which may accidentally shut down because of the power loss (some people are not very careful with power wires...). On this device we convert models from .ifc to .xbim to optimize subsequent access. As a result in rare cases I end up with broken .xbim files which I'd like to delete and run convertation again.

The issue in my case arises mainly in EsentModel.GetStepFileHeader method which temporarily opens the database, tries to read nonexistent table, throws an exception an leaves the database open.

I've made a PR to fix this behaviour: https://github.com/xBimTeam/XbimEssentials/pull/522

martin1cerny commented 1 year ago

Thank you very much. I have accepted the PR, so I'm closing this as resolver. Thank you again!