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?

Recommended Answers

All 5 Replies

>destructor will probably not be called
Yes, It will not be called
>what happens to the memory allocated for str?
It will be released by the compiler as soon the object of Sample gets out of scope
>Would this result in a memory leak?
No.

Remark: The compiler assures that every thing you create statically(and whose constructor was successfully called) will get destroyed once the object become out of scope.
To be precise, compiler assures that he will call the destructor every object you create statically(and whose constructor was successfully called) once the object become out of scope.
And thus you should make a note that, the constructor of str will be successfully called( as soon as the constructor of Sample was called) hence it will be destructed by the compiler. But the constructor of Sample was not fully called, so it won't be destructed by the compiler.

Thank you.

One of my colleagues ran valgrind (memory profiler) on the following snippet:

#include <string>
class s{
	std::string ss;
public:
	s(){ throw -1;}
};
int main( int argc, char **v)
{
	s s1;
}

Valgrind reported that a memory leak is possible. Is valgrind misreading this?

==31452== Memcheck, a memory error detector.
==31452== Copyright (C) 2002-2006, and GNU GPL'd, by Julian Seward et al.
==31452== Using LibVEX rev 1658, a library for dynamic binary translation.
==31452== Copyright (C) 2004-2006, and GNU GPL'd, by OpenWorks LLP.
==31452== Using valgrind-3.2.1, a dynamic binary instrumentation framework.
==31452== Copyright (C) 2000-2006, and GNU GPL'd, by Julian Seward et al.
==31452== For more details, rerun with: -v
==31452==
terminate called after throwing an instance of 'int'
==31452==
==31452== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 15 from 1)
==31452== malloc/free: in use at exit: 84 bytes in 1 blocks.
==31452== malloc/free: 2 allocs, 1 frees, 89 bytes allocated.
==31452== For counts of detected errors, rerun with: -v
==31452== searching for pointers to 1 not-freed blocks.
==31452== checked 88,716 bytes.
==31452==
==31452== LEAK SUMMARY:
==31452== definitely lost: 0 bytes in 0 blocks.
==31452== possibly lost: 0 bytes in 0 blocks.
==31452== still reachable: 84 bytes in 1 blocks.
==31452== suppressed: 0 bytes in 0 blocks.
==31452== Reachable blocks (those to which a pointer was found) are not shown.
==31452== To see them, rerun with: --show-reachable=yes

Never mind the first part of my question. I got a little confused. Please answer to the valgrind part though.

Out of scope means when the object is no longer usable.
Consider,

void main()
{
Sample s1;
}//this is where s1 get out of scope.

The string str will be successfully destroyed if you are catching the exceptions. If you are not,( like what your friend was doing) will cause immediate termination of the program and alas the memory will not be freed. This is a horrible situation! I know its painful but even the compiler is help less here.

This makes sense. I've confirmed that by catching the exception in main, valgrind did not report the issue. Thanks a lot for clearing this up.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.