I have a very small class (just 4 bytes in size), which I figured wouldn't be to harmful passing it as a parameter. One of it's members is a pointer to an array of data, that I delete using delete[] inside the classes destructor. When I now pass the class as a function's parameter, I guess a flat copy is made and stored on the program stack. Therefore no overloadable operator gets called and the pointer inside my class stays the same. After the function finishes, my destructor gets called, which removes my array. Outside this function the original class therefore now has a dangling pointer (it points to already removed memory).
Is there any way I can capture this copying to the stack, so that I can increase a reference counter?
You need to implement copy constructor and assign operator.
You problem happen because a function that takes parameters by value(it's your case) call copy constructor. If you don't declare copy constructor by yourself then it does compiler.
So compiler does not know that your class keeps pointer, so it just copies the address of the pointer to the another object. And in this situation you have two object that keep pointers on the same address. When the first object is going to destroy it call delete for this pointer, then the second object is going to destroy and try to call delete again for the same pointer.