New to C++ here. Can the following cause a memory leak?
// debug is optional, for debugging purposes.
// Say my_string is an internal string class my company uses.
int DoSomething(my_string *debug) {
const my_string& s = GetString();
if (debug != nullptr) *debug = s; // Could this cause a memory leak?
return DoSomethingElse(s);
}
I wanted to check my understanding here: I think at the commented line the region of memory pointed to by debug will just get overwritten with (a copy of) the contents of s, unless there is a copy-constructor implemented for my_string, in which case it might do some other stuff too.
If there is not a special copy-constructor for my_string, then if my_string points to any dynamically allocated memory in its internal representation (which it probably does since it can hold strings of any length), then the above code would cause memory leaks.
Also, another question about expected C++ "etiquette" I guess — should I be able to assume that whoever wrote my_string wrote a copy-constructor to avoid memory leaks in cases like this? That is, since I don't have any new in my own code, would it be reasonable to say it wasn't my "fault" if this code causes memory leaks?
EDIT: I think maybe I meant copy assignment operator above instead of copy constructor.