MongoDB.live, free & fully virtual. June 9th - 10th. Register Now

Unable To DeleteRealm after .Dispose() Called

Environment - .Net Core C#

Loading in my data

Uri.TryCreate(realmPath, UriKind.Relative, out var outPathUri);
RealmConfigurationBase configuration = new FullSyncConfiguration(outPathUri, _currentUser, _realmFile)
{
    ObjectClasses = typesInSelectedRealm,
    SchemaVersion = 1
};

_realmInstance = Realm.GetInstance(configuration);

if(_realmInstance != null) _realmInstance.RealmChanged += LoadDataOnChange;

Trying to copy .realm file with same format from a USB Drive

if(_realmInstance != null) 
{
    _realmInstance.RealmChanged -= LoadDataOnChange; // figure it's good to clear all external references to Realm object
    _realmInstance.Dispose();
    GC.Collect();
    Thread.Sleep(1000); // both of these are me trying to just make darn sure that everything is cleaned up and has had enough time to do so
    Realm.DeleteRealm(_realmInstance.Config); // error
}

Error thrown: The process cannot access the file '...\test.realm' because it is being used by another process.

I’ve checked for IsClosed on the _realmInstance, and it is for sure working. Help! Why is Realm maintaining the lock on that file? Possible ideas include Visual Studio doing some nastiness, but I wanted to ask here in case someone knew what was going on.

If I try and copy without deleting the .realm file I get a E_FAIL com component error. So I figure I have to solve this before I try anything else.

Also reported her: https://github.com/realm/realm-dotnet/issues/1970 where it’s answered.
Please make it clear when cross posting.
Thanks!

1 Like