C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 4th, 2005, 12:00 PM
BadBoyZ BadBoyZ is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2005
Posts: 2 BadBoyZ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 44 m 42 sec
Reputation Power: 0
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!

Reply With Quote
  #2  
Old October 5th, 2005, 12:23 PM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information.
 
Join Date: Sep 2005
Posts: 699 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 2 Days 23 h 16 m 33 sec
Reputation Power: 4
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?

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Destructor never gets called (beginner)


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT