Question

I have the following piece of code and the simple if statement: if (voxels_) where voxels_ should be NULL is failing. The code:

template<class T, typename REAL = float>
class NDIMVoxelStructure
{
public:
    inline NDIMVoxelStructure (): voxels_(NULL){}
    inline virtual ~NDIMVoxelStructure (){ this->clear();}

    /////////////////ERROR occurs at if(voxels_) //////////////////
    inline void 
    clear (){if ( voxels_ ){delete[] voxels_; voxels_ = NULL;}} 


    inline void
    build (const std::vector<REAL> bounds, std::vector<int> num_of_voxels) {
        this->clear();
        // more code
    }

protected:
    T* voxels_;
};

Class ModelLibrary {

    ModelLibrary () {
        hash_table_.build (bounds_vector, num_of_cells_vector);
    }

    struct Quad{
            const ORROctree::Node::Data* first;
            const ORROctree::Node::Data* second;
            const float* f1;
            const float* f2;
    };

    typedef std::list<Quad > quad_list;
    // these two types hide base class types
    typedef std::map<const Model*, quad_list> HashTableCell; 
    typedef NDIMVoxelStructure<HashTableCell, float> HashTable;

protected:
    HashTable hash_table;
};

int main() {
    ModelLibrary library; 
}

I get a segfault in the clear() method. Using gdb I got the address of voxels_ is set to 0xa which is strange. I am initializing it to NULL so if (voxels_) should simply return false. Any Ideas would be helpful. This is driving me crazy

Was it helpful?

Solution

Taking into account that this is just a "miniature" version from real code. Can it be that your actual code is calling NDIMVoxelStructure's copy constructor (e.g. through a function that returns a NDIMVoxelStructure element) and, then, voxels_ is not properly initialized?

In previous situation, if voxels_ is a pointer, default copy constructor would, initially, swallow copy the NULL value but, probably, something else can be running behind the scenes.

I would suggest to also define NDIMVoxelStructure's copy constructor and check if it's called.

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