Jump to content
  • Advertisement
Sign in to follow this  
Conny14156

Can't delete a pointer that I declared in a constructor

This topic is 1820 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,

I have this really wierd problem, where its not possible for me to free a certain memory.

 

My Initializilation code start like

Global->EntityController.NewEntity("test");
Entity* TestObject;
TestObject = Global->EntityController.RequestEntity("test");
TestObject.name = "blan";
TestObject->AddComponent_S(cFactory.ComponentInstantiate_S<RenderComponent>());
TestObject->AddComponent_S(cFactory.ComponentInstantiate_S<ModelHandler>());
TestObject->GetComponent_S<RenderComponent>()->StandardBox();

And heres the body of the Newentity function body

void EntityHandler::NewEntity(std::string name)
{
	Entity testing;
	testing.name = name;
	TotalEntity.push_back(testing);
}

I Just made a simple one where I just push back the value into a vector. The constructor for my Entity is

Entity::Entity()
{
	FactoryComponent cFactory;
	AddComponent(cFactory.ComponentInstantiate<TransformComponent>());
	AddComponent_S(cFactory.ComponentInstantiate_S<TransformComponent>());//<----- This one is the problem
}

I use a simple template system where I just return a "new" pointer into a vector inside Entity

template<class Component>
Component* ComponentInstantiate_S()
{
	Component* tC;	
	tC = new Component;
	return tC;
}

Works great for me, and I get no error. And I have no problem getting it added into my vector

void Entity::AddComponent_S(BaseComponent* type)
{
	if(ComponentList_S.count(type->getTypeName()) == 0)
	{
		type->setHolder(this);
		ComponentList_S.insert(std::make_pair(type->getTypeName(),type));
	}
}

The way I made it, is that all entity need to have a transform component, so I add one transform component whenever a entity gets initlized. 

The wierd part is that, I get no error and the transform component gets added to the vector. And seeing how I use push back on "testing" the object get copied into the vector. at the end of the function testing already reach the end of its life and ehm "die" and will call upon the destructor. The wierd part here is that I get no error.

for(std::map<std::string,BaseComponent*>::iterator it = ComponentList_S.begin(); it != ComponentList_S.end();it++)
{
	if(it->second)
	{
		it->second->Release();
		delete it->second;
		it->second = 0;
	}
}
ComponentList_S.clear();

But for some reason, just this one entity "Blan" where I creates with "NewEntity()" it pop up a error saying that I can't access this memory when the program reach to the delete section of the code.

I also want to point out that I added render component and modelhander outside of the constructor and both memory got freed without error's.

And when I try to remove the auto add transform component from the constructor and manual add it under the initializilation function

Global->EntityController.NewEntity("blan");
Entity* TestObject;
TestObject = Global->EntityController.RequestEntity("blan");

TestObject->AddComponent_S(cFactory.ComponentInstantiate_S<TransformComponent>());//<--- Like so

TestObject->AddComponent_S(cFactory.ComponentInstantiate_S<RenderComponent>());
TestObject->AddComponent_S(cFactory.ComponentInstantiate_S<ModelHandler>());
TestObject->GetComponent_S<RenderComponent>()->StandardBox();

Than it works as "intended" except the part that I need to add it manually.

 

Some help and info on this matter would be greatly appecirated,  and sorry for the bad english, not my mother tongue. 

 

Thanks you 

Edited by Conny14156

Share this post


Link to post
Share on other sites
Advertisement

But for some reason, just this one entity "Blan" where I creates with "NewEntity()" it pop up a error saying that I can't access this memory when the program reach to the delete section of the code.

Your NewEntity method create the entity on the stack and not on the heap:

Entity testing;
	testing.name = name;
	TotalEntity.push_back(testing);

Once your NewEntity methods returns, the testing entity will be gone resulting most likely in a followup error once you try to access/delete it.

 

The correct way would be something like this:

Entity* testing = new Entity();
    testing->name = name;
    TotalEntity.push_back(*testing);

Update:

I know, that a vector::push_back creates a copy of the data, but this does not prevent you from using pointers as type of the vector. Can you give more information, like the definition of TotalEntity.

 

Update:

It seems that I'm on the wrong track...

Edited by Ashaman73

Share this post


Link to post
Share on other sites

Yeha. From the begining I started to expriment with vector containing pointers instead for object/value(whatever the word is XD),but somewhere along I just thougth mayaswell do it without pointers cause I didn't need to smile.png, and my NewEntity doesn't return anything :l, maybe I should make it return a bool sometime in the future to know if I succed or not. And yeha ask anything that may help you guys help me solving this :l 

 

P.S Edit:

 
private:
std::vector<Entity> TotalEntity;
TotalEntity is just a vector of entity
Edited by Conny14156

Share this post


Link to post
Share on other sites
Entity* EntityHandler::RequestEntity(std::string name)
{
	std::vector<Entity>::iterator it;

	if(!name.empty())
		name = Renaming(name);	
	for(int i = 0;i < TotalEntity.size();i++)
		if(TotalEntity[i].name == name)
			return &TotalEntity[i];
	return NULL;

}

Its pretty simple I just for loop the whole vector and compare the two name, and return that value.

Share this post


Link to post
Share on other sites

You are crashing in the destructor for a BaseComponent, right? Is there a reason you choose not to share that class?

Am crashing at 

for(std::map<std::string,BaseComponent*>::iterator it = ComponentList_S.begin(); it != ComponentList_S.end();it++)
{
	if(it->second)
	{
		it->second->Release();
		delete it->second; //<--- Crashing here
		it->second = 0;
	}
}
ComponentList_S.clear();

And this part is not a deconstructor, it that before I end the program I clean up and free some memory


class BaseComponent
{
public:	
	BaseComponent();
	virtual ~BaseComponent(){}	
	virtual void Update(){};
	virtual void Release();
	std::string getTypeName();
	void setHolder(Entity*);
	Entity* thisObject;
	std::string typeName;
};

Basecomponent class don't have anything in it :l but here it is.

Share this post


Link to post
Share on other sites

What does Release() do for the dodgy object?

Well, Its usually did what the deconstructor does. But I moved the code from the release function > deconstructor for each class. It just a leftover code for now, am still quietly new to this so I "experiment" and do alot of stupid stuff. at the time I didn't think I could use the deconstructor to release my object so I did it manually :l. 

But for the transform component, It doesn't have a override release(), so it does nothing actually 

Share this post


Link to post
Share on other sites

Adding 'TransformComponent' in the Entity's constructor like you want, try this:

Global->EntityController.NewEntity("blan"); //TransformComponent already created in Entity's constructor.
Entity* TestObject = Global->EntityController.RequestEntity("blan");

assert(TestObject->GetComponent_S<TransformComponent>(), "Oops, it was already deleted");

See whether the assert is triggered or not.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!