Skip to content

Segfault upon destruction using hashmap of hashmaps #2

@charlesgregory

Description

@charlesgregory

In trying to make a hashmap of hashmaps using khash.d, I run into the issue of a segfault when using the require function.
Segfault happens here as a double free (I think):

dklib/source/dklib/khash.d

Lines 145 to 147 in 8b3fa92

kfree(cast(void*) this.keys);
kfree(cast(void*) this.flags);
kfree(cast(void*) this.vals);

The initval is an empty hashmap.
Example:

auto kmers = khash!(string, khash!(string, int))();

// for loop {

    // if not present create empty hash
    // initval is empty hashmap
    kmers.require(kmer1, khash!(string, int)());

    // if not present make count zero
    kmers[kmer1].require(kmer2, 0);
  
    kmers[kmer1][kmer2]++;

//}

The fix I found was by removing the free statements in the destructor. My theory is that D's automatic destructor frees the pointers as soon as they are out of scope. Or the other theory is that the empty hashmaps are freed before ever having the pointers set to any memory and are therefore the pointers are null or invalid.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions