Consider this situation:
class Sample
{
std::string str;
public:
Sample(string &s)
{
// line 1
// line 2
str = s;
// line 4
throw exception;
}
~Sample() { }
}
So I have a string member variable, and I have assigned it to the parameter passed to the constructor. My understanding is that the copy constructor for str will be called, and data from 's' will be copied to it. If the constructor goes through fully, then I can assume that the object str will be deleted when the destructor of Sample class runs.
Suppose an exception is thrown in the constructor, then the destructor will probably not be called, right? (because the object was never created). So what happens to the memory allocated for str? Would this result in a memory leak?