-
March 4th, 2009, 06:56 PM
#1
std::string and exceptions in constructor
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?
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|