Objects allocated with help newmust be freed with help deleteor there will be leaks. Highlighting with newis independent of function calls - you can create something with newin one function and release it with help deletein another without any problems.
If you need an object allocated in the function and freed when you exit the function, simply do the following:
void foo(...) {
MyClass myobj(...);
return;
}
If you need to pass a pointer to an object in a function, you do not need to use newthis; you can use the operator &:
std::vector<int> myvec(pV);
foo->func(&myvec);
In this case myobj, it is an automatic variable that is pushed onto the stack and is automatically deleted when it exits. In this case, there is no need to use new.