Jump to content
  • Advertisement
Sign in to follow this  
Asuralm

Will the memory leak in this case?

This topic is 3623 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all: If I declare a class like this:
MyClass {
public:
    MyClass(){
        x = new double;
        y = new double;
        z = new double;
    };
    ~Myclass() {};
.
.
.
private:
    double* x,y,z;

}


When initialize a new object say: for (int i=0; i < 1000000000; i++){ MyClass obj; } without explicit deconstructor for removing the reference of x, y and z, will this cause the memory leak problem please? Thanks [Edited by - Asuralm on July 23, 2008 11:03:43 AM]

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Asuralm
without explicit deconstructor for removing the reference of x, y and z, will this cause the memory leak problem please?


As written, your code can create a memory leak even with the explicit destructor delection code (and will certainly leak memory without it).

Share this post


Link to post
Share on other sites
Quote:
Original post by Telastyn
Absolutely.


So does it mean I should write

~MyClass(){ delete x; delete y; delete z; }

like this?

Share this post


Link to post
Share on other sites
Quote:
Original post by Asuralm
Quote:
Original post by Telastyn
Absolutely.


So does it mean I should write

~MyClass(){ delete x; delete y; delete z; }

like this?


It means you should be using idiomatic C++ concepts, such as RAII and SC++L instead of manually handling your memory.

Share this post


Link to post
Share on other sites
Quote:
Original post by Asuralm
~MyClass(){ delete x; delete y; delete z; }


No, that would still leak memory. Welcome to the wonderful world of C++ manual memory management!

A valid implementation of your class would be:
class MyClass 
{
double *x, *y, *z;
MyClass &operator=(const MyClass&); // Do not implement
MyClass(const MyClass&); // Do not implement
public:
MyClass() : x(0), y(0), z(0)
{
try
{
x = new double;
y = new double;
z = new double;
}
catch (...)
{
delete x;
delete y;
delete z;
throw;
}
}

~Myclass()
{
delete x;
delete y;
delete z;
}
};



Of course, it could be just as easy to let standard library classes
handle this for you:
class MyClass 
{
std::auto_ptr<double> x, y, z;
public:
MyClass() : x(new double), y(new double), z(new double) {}
};


And, if this was real-life code instead of an example:
class MyClass { double x, y, z; }

Share this post


Link to post
Share on other sites
Nitpick: This is a compile error:
y = new double;
z = new double;
Since only x is a pointer. y and z are just double's.

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
Quote:
Original post by Asuralm
~MyClass(){ delete x; delete y; delete z; }


No, that would still leak memory. Welcome to the wonderful world of C++ manual memory management!


What do you mean? In what way would it still leak?

Share this post


Link to post
Share on other sites
Quote:
Original post by Decrius
Quote:
Original post by ToohrVyk
Quote:
Original post by Asuralm
~MyClass(){ delete x; delete y; delete z; }


No, that would still leak memory. Welcome to the wonderful world of C++ manual memory management!


What do you mean? In what way would it still leak?


If an exception was thrown, the memory would not get released.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!