pointer to pointer help

Discussion about everything. New games, 3d math, development tips...
Post Reply
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

pointer to pointer help

Post by Seven »

a little help with pointer to pointer usage please...
this obviously does not work but i do not understand why.
when the createobject function returns, theObj is not valid.

can someone point out the error of my ways?

Code: Select all


class object
{
    int id;
}


int createObject(object** obj)
{
    // somehow get a new number
    int id = 100;

    // create an object somewhere else using new()
    object* o = new object();
    
    // if the object is valid
    if (o)
    {
         // set the object id to some value
         o->id = id;

         // set the passed in pointer to a pointer to this address 
         obj = &o;
     }

     // return the id we are using
     return id;
}


  main()
  {
        // declare an object pointer set to NULL
        object* theObj = NULL;

        // create the object
        int value = createObject( &theObj );
        
        printf(" value = %d /n", value);
  
        if (theObj) printf(" theObj is valid");
            else printf("theObj is NOT valid");       
  }

Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

I guess you'll have to dereference the pointer for the assignment :lol: :

Code: Select all

// set the passed in pointer to a pointer to this address 
*obj = o; 
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

my thought too, but the program crashes when i do this

Code: Select all


  *obj = o;

any thoughts on why?
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

for me it works...
but your class definition is also wrong !!!
it should be:

Code: Select all

class object{
  public:
    int id;
};
remember the semicolon at the end of the class !!! ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

yeah, you are right, i just typed it in quickly is all.... so it works for you? hmmmmmm..... lemme try a few things.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

Seven wrote:so it works for you?
yup:
Image
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

hm... still no good for me... I create the object in a DLL though so maybe that has something to do with it.... I will have to investigate.

Code: Select all

// create an object from class name
int IrrObjectFactory::createObjectByTypeAndDLL(core::stringw dllname, core::stringw objtype, IrrObject** obj, bool addtomanager)
{
	// make sure our pointers are valid
	IRR_CHECK_INT(m_ClassManager,L"IrrObjectFactory::CreateObjectByTypeAndDLL() - ClassManager is not valid");
	IRR_CHECK_INT(m_ObjectManager,L"IrrObjectFactory::CreateObjectByTypeAndDLL() - ObjectManager is not valid");

	// get the data structure for this object definition
	IrrObjectData* data = m_ClassManager->findClassInfo(dllname, objtype);
	IRR_CHECK_INT(data,L"IrrObjectFactory::CreateObjectByTypeAndDLL() - unable to locate class definition");

	// get the dll path name to load
	core::stringw filename = m_ClassManager->getObjectDirectory() + data->DLLName;

	// attempt to load the dll
	HMODULE hwnd = LoadLibrary(filename.c_str());
	IRR_CHECK_INT(hwnd,L"IrrObjectFactory::CreateObjectByTypeAndDLL() - unable to load DLL file");
	
	// find the object create function 
	typedef void* (*CREATEOBJECT)(core::stringw objtype);
	CREATEOBJECT CreateProc = (CREATEOBJECT) GetProcAddress(hwnd, "CreateObject"); 

	// create the object
	IrrObject* theObj = (IrrObject*)CreateProc(data->ObjectName);
	IRR_CHECK_INT(theObj,L"IrrObjectFactory::CreateObjectByTypeAndDLL() - object creation failed");

	// let the object manager set the object's ID. we can assure unique ID's this way
	int id = m_ObjectManager->getUniqueId();
	theObj->setId(id);

	// dual creation allows us to check for errors that we cannot detect in the constructor
	if (!theObj->create(getLevel()))
	{
		delete(theObj);
		return -1;
	}
	else
	{
		// set the address
		*obj = theObj;

		// add it to the object manager
		if (addtomanager) m_ObjectManager->add(theObj);
		
		// object created and added to list successfully
		return theObj->getId();
	}
}

Code: Select all

		IrrObject* obj3;  
		getObjectFactory()->createObjectByType(L"IrrObject_Sphere", &obj3, false);
		if (obj3)
		{
			printf("HERE!!!!!!!!!!!!!!!\n");
			obj3->setScale(vector3df(130,100,110));
			obj3->setPosition(vector3df(10,100,10));
			obj2->addChild(obj3);
		}
		else
			printf("NOT HERE!!!!!!!!!!!!!!!\n");

fails at
obj3->setScale(vector3df(130,100,110));
because obj3 is not valid.....
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

are you sure this is the right function ???

Code: Select all

int IrrObjectFactory::createObjectByTypeAndDLL(core::stringw dllname, core::stringw objtype, IrrObject** obj, bool addtomanager) 
but you call

Code: Select all

getObjectFactory()->createObjectByType(L"IrrObject_Sphere", &obj3, false);
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

sorry for this lengthy post, but i really could use some help on this......

the factory scans the object DLL directory and finds all of the dll's that contain objects. it then uses the list to allow me to create any object simply by name. this all works great, allowing me to drop in object dll files during runtime and the game automatically detects them and makes them available for use. so far all of this works as expected....

unfortunately, I decided to allow objects to have children, and as always, the children make things difficult.

originally, all objects were accessed by their ID values. Since the objectmanager maintains the list of objects, anytime i need an object that has been created, i simply call the getobjectpointer() function....

Code: Select all

int id = getFactory()->createObjectByType("IrrObject_Box")
IrrObject* obj = getFactory()->getObjectPointer( id );
if (obj)
{
    obj->setScale(vector3df(10,10,10));
    obj->setPosition(vector);
    obj->setRotation(vector);

    obj->setVariable(L"Health", 10);
     etc....
}

unfortunately, now that i want to add children to the objects, I would like to return not only the assigned ID from the createObject() function, but also store the actual object address. that is why I am trying to use the IrrObject** variable..........but it is not working for me. the stored object is not valid upon return.... i will need to do more testing i think. I am not sure if the DLL scheme is the trouble or not, but all objects classes are defined in their own DLL file, which also has a createObject() function. Since the object is created in the DLL using new(), maybe I have some issue with assigning that memory location to the main program variable? i dont know.... sigh.....


as for the create function, you can have objects named the same and residing in different dll's if you want. if you do, you need to call createobjectbytypeanddll() otherwise it will find the first instance and use that dll...

Code: Select all

int IrrObjectFactory::createObjectByType(core::stringw type, IrrObject **obj, bool addtomanager)
{
	IRR_CHECK_INT(m_ClassManager,L"IrrObjectFactory::CreateObjectByType() - ClassManager is not valid");
	IRR_CHECK_INT(m_ObjectManager,L"IrrObjectFactory::CreateObjectByType() - ObjectManager is not valid");

	IrrObjectData* data = m_ClassManager->findClassInfo(type);
	IRR_CHECK_INT(data,L"IrrObjectFactory::AddObjectByType() - unable to locate class definition");

	return createObjectByTypeAndDLL(data->DLLName,data->ObjectName, obj, addtomanager);
}
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

sorry, now I'm also out of clues... :cry:
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

thanks for the assist acki...
i wrote asmall test app that throws objects around inside different dll files and it works just fine, so there is something wrong with this specific application. I assume a linking error now and will need to track it down. In any event, since the test app works as expected, I will need to do this alone :(

thanks again for the assist.
Post Reply