| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Destructor never gets called (beginner)
Hey all!
Firstly thanx to Itsacon for replying to my previous post. The error just went away so I didn't know whether or not to post on it again.Now for my next problem. I'm writing an OpenGL program which makes use of my own Vector class. I've attached the header file to the bottom so have a look if you want. Inside this header is a float pointer (an array) which contains my x, y and z coordinates. I also have a destructor which delete[]s the float array. In my OpenGL code I call the following: Code:
for () {
Vector v1(x, y, z);
Vector v2(x, y, z);
Vector v3(x, y, z);
// OpenGL calls (nothing interesting, just accessing the vector through getVertex())
}
The problem is the memory is never cleared. I know this because in the Task Manager my memory usage increases by almost 5 MB a second. I'm sure it's the vector's fault because replacing the pointer with three floats and getVertex with getX, getY and getZ works perfectly. I've also tried the following, but I get the same problem. Code:
for () {
Vector v1 = new Vector(x, y, z);
Vector v2 = new Vector(x, y, z);
Vector v3 = new Vector(x, y, z);
// OpenGL calls (nothing interesting, just accessing the vector through getVertex())
delete v1;
delete v2;
delete v3;
}
Here's Vector.h's relevant parts and some relevant methods: Code:
class Vector
{
private:
// Vector location.
float* position;
public:
// Constructors.
Vector();
Vector(float, float, float);
~Vector();
Vector(const Vector&);
Vector& operator=(const Vector&);
// Accessors.
float getX() const;
float getY() const;
float getZ() const;
float* getVertex() const;
// Modifiers.
void setX(float);
void setY(float);
void setZ(float);
void setPosition(float, float, float);
Code:
Vector::Vector()
{
position = new float[3];
}
Vector::Vector(float x, float y, float z)
{
position = new float[3];
position[0] = x;
position[1] = y;
position[2] = z;
}
Vector::~Vector()
{
delete[] position;
}
float Vector::getX() const
{
return position[0];
}
float Vector::getY() const
{
return position[1];
}
float Vector::getZ() const
{
return position[2];
}
float* Vector::getVertex() const
{
return position;
}
Ps. I never use the copy-constructor nor the assignment operator. Any and all help will be appreciated! |
|
#2
|
||||
|
||||
|
I copy pasted your code but I experience no memory leaks, which is expected I guess. The code you've shown looks correct, I'm suspecting your problem is somewhere in the rest of the (GL?) code. If your program is not to big I'd be willing to look into it if you send me all of it?
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Destructor never gets called (beginner) |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|