C++ string not freed when class is destroyed
The memory for a private string variable in a class appears not to be released automatically when the class is destroyed.
Here is a simple example...
#include <string>
#include <mcheck.h>
using namespace std;
class test
{
string x;
public:
void set(const string& y)
{
x=y; // set x the private class string to the value of y
}
test() {}
~test() {}
};
int main()
{
mtrace();
// produces a leak
test X;
X.set("Hi");
{
// doesn't produce a leak
test Y;
Y.set("Hi");
}
muntrace();
return 0;
}
And the memory trace it produces:
Memory not freed:
-----------------
Address Size Caller
0x000000000187f460 0x1c at 0x7f3df25a926d
When the class is destroyed, the memory used by the private string, x, I think should be freed automatically. However it is not, causing a memory leak.
This seems to me to be a bug. Could someone who knows more than I do (i.e, everyone reading this), please confirm or refute my suspicion?
Question information
- Language:
- English Edit question
- Status:
- Solved
- Assignee:
- No assignee Edit question
- Solved by:
- Eliah Kagan
- Solved:
- Last query:
- Last reply: