Sign in to follow this  

Destructors Question

This topic is 3632 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

Instead of releasing buffers at the end of my code, I want to make a destructor for a struct. Currently:
void cleanD3D(void) //Last thing called before exit.
{
    Object1.Mesh->Release(); //Only important line here (I intend to remove/replace it)
    d3ddev->Release();
    d3d->Release();

    return;
}

If I have my struct set up like this (may be wrong :P ):
struct Object{
	std::string Name;
	LPD3DXMESH Mesh;
	D3DMATERIAL9* Material;
	LPDIRECT3DTEXTURE9* Texture;
	DWORD numMaterials;
	Object(char * a);
	~Object()
};

Object::Object(char * a) {
	Name = a;
}
//Important part:
Object::~Object() {
	Mesh->Release();
}

Do I have to use the destructor explicitly or will the structs I make be automatically destroyed at exit or what?

Share this post


Link to post
Share on other sites
Whether or not you need to destroy your object explicitly depends entirely on how you allocate the object; not how the object is defined.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Whether or not you need to destroy your object explicitly depends entirely on how you allocate the object; not how the object is defined.


So unless I create some Object structs dynamically, they will be automatically destroyed at exit (like an int or float) and the destructor will be run(?) automatically, thus releasing my dynamically allocated LPD3DXMESH member?

Share this post


Link to post
Share on other sites
Quote:
Original post by justcallmedrago
Quote:
Original post by SiCrane
Whether or not you need to destroy your object explicitly depends entirely on how you allocate the object; not how the object is defined.


So unless I create some Object structs dynamically, they will be automatically destroyed at exit (like an int or float) and the destructor will be run(?) automatically, thus releasing my dynamically allocated LPD3DXMESH member?


Yes. This actually has a name.

Incidentally, you're safe even if you allocate dynamically so long as you use a smart pointer (std::auto_ptr for strict ownership semantics, or boost::shared_ptr if there is more than one owner).

Share this post


Link to post
Share on other sites
Quote:
Original post by TheUnbeliever
Incidentally, you're safe even if you allocate dynamically so long as you use a smart pointer (std::auto_ptr for strict ownership semantics, or boost::shared_ptr if there is more than one owner).

Using boost::shared_ptr won't guarantee that your objects will be destroyed. If you form a cycle of references shared_ptr won't collect the objects.

Share this post


Link to post
Share on other sites

This topic is 3632 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this