In Java, there is
no distinction between variables/members containing a
pointer/reference to an object and variables/members containing the
object directly; the latter doesn't really exist in Java (except for primitive types). Instead, in Java, you are
always dealing with
references to objects; the objects always live in the "heap" space and your variables contain
references to those objects. End of story.
In C++ this is quite different! You
can allocated objects on the "heap" space, by using the
new
operator, which will give you a
pointer to the object; such objects
must explicitly be destroyed, using the
delete
operator, or else you will get a memory leak. Keep in mind that there is
no garbage collection! But, just as well, in C++ you can have objects that "live" directly in the variable/member. This is called "automatic storage duration" and does
not require or allow explicit destruction; such objects are destroy as soon as they go out of scope.
In your
Results()
function, the local
VectOfByteVect
-object
interim
has "automatic storage duration"; it lives on the stack (
not heap) and it will be destroyed as soon as the surrounding function returns. Storing a
pointer to
interim
in the member variable
Cache
therefore means that you are storing a pointer that becomes
invalid ("dangling") at the very moment when the
Results()
function returns!
You probably want to change the type of member variable
Cache
from
VectOfByteVect*
to
VectOfByteVect
, so that it can store the actual object, rather than just a pointer to an object. However, be aware that, when assigning a member variable of type
VectOfByteVect
from another
VectOfByteVect
object, then this will invoke the
assignment operator, i.e. it is an actual "copy" operation.
https://cplusplus.com/articles/y8hv0pDG/