question about my deconstructor

1.
Will my deconstructor delete the object `next` and `previous` or just the pointer? I want it only to delete the pointer.
2.
let's say i have
1
2
ofxDTangibleBase a;
ofxDTangibleBase b;


and `a.next` points to `b` and `b.previous` points to `a`. If i deconstruct `a`.
Will `b.previous` point to a NULL or does it point to the location in memmory where `a` was before?

3.
My program stops on the last line in the deconstructor with `EXC_BAD_ACCESS`.
What could be a possible reason for this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  class ofxDTangibleBase : public ofxCvBlob {
    
    public:
        
        // used in globalTangibleList
        ofxDTangibleBase *previous = NULL;
        ofxDTangibleBase *next = NULL;
        
        ofxCvGrayscaleImage *thresholdImage;
    
        const char *className;
        
        ofxDTangibleBase(ofxCvBlob& base, const char *className) : ofxCvBlob(base) {
            this->className = className;
        }
    
        ofxDTangibleBase(ofxDTangibleBase& base, const char *className) : ofxCvBlob(base) {
            this->className = className;
          }
    
        ~ofxDTangibleBase() {
            delete previous;
            delete next;
            delete thresholdImage;
        }
};
delete deletes the object and does not change where the pointer points to.
Normally, when an objects holds a pointer to another object, and that other object dies, the pointer still points to the memory location the object occupied (not set to null magically).
But, I assume that if b.previous points to a, then a.next points to b. So when you loose a (it goes out of scope or is deleted) it fires destructor that in turn deletes b, and b deletes its next - c and so on. Entire chain is destroyed. And since the destruction goes both ways it inevitably reaches the point when someone orders to delete somebody who just deleted that someone ;) And when that happens, the program crashes.
What you really need to do in destructor is set object's neighbour's next and previous pointers to null, not delete them.
1
2
previous.next = null;
next.previous = null;
Last edited on
So if i understand correct, pointers get deleted when you delete an object. But the things they point to stay there unless you use delete.

Pointer DO NOT GET deleted. They hold addresses, like visit cards. When you call delete you are telling the compiler: "Here, look at that visit card. Go under that address and kill whoever lives there". The compiler kills the object, and you keep your visit card. It still has the same address printed on, so you should be carefull not to tell compiler again to go and kill whoever lives there, because at that time there will be someone else, and you don't even know who. Basically, the visit cards gets useless, or even dangerous untill you correct the information printed on it.
So just to be sure, after you call delete, you take a black marker and cross out the address from visit card, just to be sure no one gets killed accidentally.
Or in other words, assign null to the pointer.

Pointers themselves do not need to be deleted. Remember, as soon as they go out of scope, they dissapear.
Last edited on
i just wanna say that it is destructor not deconstructor, lol :D
<offtop>
Silvestar, you barbarian! Some peaople just want to keep things civil, and not blindly destroy anything around. I for example would call that assassin (as per example above);)
</offtop>
Topic archived. No new replies allowed.