problem probably related to stack being freed.

I have some method (here i edited it to be really small):

  
void ofxDTangibleFinder::findTangibles(ofxCvGrayscaleImage& input) {  
  
    _tangibles.clear();  
    // some loop here  
    ofxCvBlob &blob = contourFinder.blobs.at(i);  
              
    ofxDTangibleBase tang(blob, "ofxDTangibleBase");  
    _tangibles.push_back(tang);  
  
}  

Then in another method i loop over the _tangibles which are passed to the method as a reference.
I create objects with:

  
ofxDTangibleBase* t = &newList[i];  
_globalTangibles.push_back(t);  

But as soon as i run findTangibles again, then the tangibles in the other method get’s messed up.

I wonder how. In findTangibles they are created on the stack, they don’t get released after it’s out of scope since they got pushed to the vector _tangibles right?
Then later when findTangibles is called again the _tangibles get’s cleared but this shouldn’t free the memory since _globalTangibles is still using it?

Or am i wrong with the last.

scopes don’t work like that in c++, if you create something in the stack it is destroyed when the block it’s declared in, which is delimited by { }, ends. no matter if another object gets a reference to it.

read a little on c++/c scopes, heap vs stack and reference/dereference (& and *) operators, all your questions are really a misunderstanding of those 3 topics :slight_smile:

read a little on c++/c scopes, heap vs stack and reference/dereference (& and *) operators, all your questions are really a misunderstanding of those 3 topics :slight_smile:

I’ tired of reading :slight_smile: I thought let’s put it into practice. But good news for you, i just have a new book about c++!
So i will do some more reading. :slight_smile:

if you are used to java think that in java everything is a pointer, there’s nothing allocated in the stack so things in the stack will behave different to what you are used to. also things in java kind of auto delete. in c++ they don’t unless you use shared pointers, in OF ofPtr

yeah i think your correct about java working with pointers, remembering the nullPointerExceptions :slight_smile: