Question

I get some super annoying asserts when I try to delete the leveldb instance and I'm not sure why it's happening!

The assert is happening in the version_set.cc file:

void VersionSet::AppendVersion(Version* v) {
  // Make "v" current
  assert(v->refs_ == 0); // <---??? how do I avoid this assertion?
  // the rest of the source code is available in the link to version_set.cc
}

Additionally, it asserts in another place in the same file:

Version::~Version() {
  assert(refs_ == 0); // <-- Again... how do I avoid this one too?
  // the rest of the source code is available in the link to version_set.cc
}

Here is more background details on the usage in my system, I have a:

  • class ExtStorage (Extended Storage) which has a LevelDB::DB instance.
  • class EextStorageDotNet, which is the C++/CLI wrapper around the ExtStorage.
  • class AltStorage, which holds a pointer to an ExtStorage class (passed through the constructor):
  • class AltStorageDotNet, which is the C++/CLI wrapper around the AltStorage.

The alternate storage class looks like this:

class AltStorage{
    ExtStorage* instance;
public:
    AltStorage(ExtStorage* extStorage):instance(extStorage){}

    ~AltStorage(){
        delete instance;
        instance = NULL;
    }
};

The ExtStorage class looks like this:

class ExtStorage{
    leveldb::DB* mydb;
public:
    ExtStorage(/*some parameters*/){
         mydb = new leveldb::DB(/*parameters*/);
    }

    // Destructor
    ~ExtStorage() {
        Close();
    }

    // deletes the leveldb::DB instance
    void Close() {
        if(mydb == NULL) {
            delete mydb; // <-- Asserts every time I get here when using with the AltStorageDotNet
            mydb= NULL;

            // Close the L1 and L2 caches
            // only once (
        }
    }
}

The AltStorageDotNet class looks like this:

public ref class AltStorageDotNet{
    AltStorage* altInstance;
    ExtStorageDotNet^ extInstance;
public:
    AltStorageDotNet() {
        ExtStorage extStorage = new ExtStorage(/*params*/);
        altInstance = new AltStorage(extStorage);
        extInstance = gcnew ExtStorageDotNet(extStorage);
    }

    ~AltStorageDotNet(){
        delete altInstance;
        altInstance = NULL;
        // no need to delete extInstance since it was created with gcnew 
    }

    !AltStorageDotNet(){
        delete altInstance;
        altInstance = NULL;
        // no need to delete extInstance since it was created with gcnew
    }

    inline ExtStorageDotNet^ GetExtInstance(){return extInstance;}
};

The DotNet wrappers look like this:

public ref class ExtStorageDotNet{
private:
    ExtStorage* instance;
public:
    ExtStorageDotNet(ExtStorage* extStorage){
        instance = extStorage;
    }

    ~ExtStorageDotNet(){
        delete instance;
        instance = NULL;
    }

    !ExtStorageDotNet(){
        delete instance;
        instance = NULL;
    }

    void Close(){instance->Close();}
};

Whenever I use the ExtStorageDotNet wrapper in my C# application everything works good and there are no asserts. However, when I use the AltStorageDotNet wrapper and I access the ExtStorageDotNet wrapper, then I get the asserts when closing the database. This is all part of a test suite in which I initialize an instance for each test case and close it after each test case; the associated database files get deleted before a new test case begins. I don't see any reason why it should happen and the assertion is not helpful in tracking down the problem.

Was it helpful?

Solution

I got rid of the nested references, but that didn't solve the problem. As it turns out, the issue causing this problem was not quite where I was looking at. The issue occurs when a user gets an iterator to the database and fails to delete the iterator prior to deleting the database. This was discussed in a google group relating to level db.

// Caller should delete the iterator when it is no longer needed.
// The returned iterator should be deleted before this db is deleted.
virtual Iterator* NewIterator(const ReadOptions& options) = 0; 

When the user gets the iterator they should delete it prior to deleting the db, otherwise they will get the assertion mentioned above.

OTHER TIPS

I don't know whether it's related to your asserts or not, but this code is guaranteed to cause memory corruption. Both ExtStorageDotNet and AltStorageDotNet are disposable and finalizable, and both (directly or indirectly) delete the instance of ExtStorage -- invariably, one will delete it after the other already has.

Also, as I said in my comment, the comment in your code saying no need to delete extInstance since it was created with gcnew is way off -- see this answer for details. (I assume you know C# and thus about IDisposable; if you don't, then you really need to do some reading.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top