🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Problems with my implention (container system)

Started by
2 comments, last by mushr00m 17 years, 3 months ago
Hi. I been working on game using AngelScript for a couple of months now and ran into problems when I tried a new way of handling objects. Now I will try to explain the system im using as good as possible. If you know what is causing my problems or a better way to implent it, I would really appreciate the help. The objects look like this: container --> smart pointer --> true pointer The container holds a pointer to a smart pointer. When there is no references to the smart pointer and it self delete. The factory that creates the true object also deletes the true object. If it is deleted the smart pointer's true pointer is set to NULL. And the factory releases it's reference to the smart pointer. This means: * You can destroy a objects true content even though there is still references to it. For example a mesh can be removed from the scene. * That means the smart pointer doesn't handle the deletion of the true pointer, it only keeps track wether or not it's valid. * The smart pointer self deletes when there is no containers that has references to it anymore and the factory ahs released its reference. * You can create empty containers that holds no references. And assign containers to each other. Example: Entity myEntity;

void main()
{
  myEntity.move(); // does nothing but generates a warning for empty container

  // Creates a smart pointer and assign it to the container
  myEntity = createEntity("myEntity", "entity.mesh"); 

  Entity otherEntity;

  // otherEntity is now equal to myEntity (they have the same smart pointer).
  otherEntity = myEntity;

  // The mesh entity.mesh is now destroyed and the factory has released it's 
  // reference. The smart pointer will remain as long as the containers exists
  // or gets assigned a new one. 
  destroyEntity(otherEntity);
}
Now to start with I had alot of problems with bad pointers in assignment. But those are resorted now. Problem now is that get a exception every time AngelScript tries to delete a temporary object (used when copy from one object to another.). The exception is caused at then end of the destructor. Some of the code:


// assignment

ret = m_pEngine->RegisterObjectBehaviour("Entity", asBEHAVE_ASSIGNMENT,
 "Entity& f(const Entity& in)", asFUNCTION(Script_OgreEntity::assign), asCALL_CDECL_OBJFIRST); ERR_AS(ret);

Script_OgreEntity& Script_OgreEntity::assign( Script_OgreEntity* self, Script_OgreEntity* other )
{
	self->set( other->get() );
	return *self;
}



// destructor

Script_OgreEntity::~Script_OgreEntity()
{
	if (m_pObject) 
		m_pObject->release(); 
}

On a side note im using ogre 3d engine which has it's own memory manager. Im not sure if thats causing the problem. Got any ideas what I might be doing wrong? I can paste more code if needed.
Advertisement
Most likely you're not updating the reference counter correctly, so that the object is destroyed before it's time.

Your constructor function must set the reference counter to 1 to account for the reference being constructed (of course, if you're storing a reference somewhere else that should be accounted for as well).

The assignment operator must decrement the reference of the previous object, and increment the reference of the new object. You'll probably want to increment for the new object first and then decrement for the old object, in order to support assignment of the same object again.

The destructor must decrement the reference.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Do the OGRE objects use reference handling to perform memory management? If it does, then you should be able to register them directly with AngelScript without the need for containers and smart pointers.

The scripts can then work directly with the objects via object handles, which work exactly as smart pointers.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Sorry for the late responce, ive been away :D

No afaik know ogre doesn't use reference handling in that way.

It's memory manager overrides the new and delete operations. And replaces it with it's own.

I think you where right about the referenses. Some of my exceptions where sorted out when I increased the referenses. Now I only have to find the right balance.

This topic is closed to new replies.

Advertisement