[Solved] Userdata lost

alberts

29-08-2006 12:58:14

Hi

I'm having problems with the setUserData method. I've defined a class to store a set of values. It works well with simple values (int, float, vector3...). I can recover them using the getUserData method. The problem is that the values of an array of floats are lost. Here are the code involved:


float liftCoefficient[9] = {-0.54f, -0.2f, 0.2f, 0.57f, 0.92f, 1.21f, 1.43f, 1.4f, 1.0f};
float dragCoefficient[9] = {0.01f, 0.0074f, 0.004f, 0.009f, 0.013f, 0.023f, 0.05f, 0.12f, 0.21f};
float angleCoefficient[9] = {-8.0f, -4.0f, 0.0f, 4.0f, 8.0f, 12.0f, 16.0f, 20.0f, 24.0f};

mWing = makeSimpleBox(*mWingSize,5,Ogre::Vector3(1.5,0.8,0),orient,"wing");
mWing->setCustomForceAndTorqueCallback<Airplane>( &Airplane::AerodynamicForceCallback, this );
AerodynamicProperties* ap = new AerodynamicProperties("WING",mWing,mSceneManager,&Ogre::Vector3(mWingSize->x,0,0),&Ogre::Vector3(0,0,mWingSize->z),&Ogre::Vector3(0,1,0)
,liftCoefficient,dragCoefficient,angleCoefficient,coefficientCount);
mWing->setUserData(ap);


In AerodynamicProperties constructor:


...
mName = name;
mBody = body;

mCoefficientCount = CoefficientCount;
mLiftCoefficient = LiftCoefficient;
for(int i=0;i<CoefficientCount;i++)
{
mLiftCoefficient[i] = LiftCoefficient[i];
}
...


Later, in callback


AerodynamicProperties* ap = (AerodynamicProperties*)me->getUserData();
netForce = ap->CalculateAirfoilForce(relWindVeloc);


But inside CalculateAirfoilForce method of AerodynamicProperties, the arrays have strange values. The rest of variables of AerodynamicProperties, like "name" are kept well. :shock:

What could be the problem?

Thanks in advance and I apologize for my english :)

Edit: It was my fault.
I was not reserving memory in the constructor for the arrays. I thought it was ref counted or something :oops: (It's my first projects in c++ for years :) )

Thanks!

OvermindDL1

30-08-2006 23:14:49

Yep, if you want to ref-count things, Shared_Ptr's are the way to go. Weak_Ptr's are used with them, used properly, you will never have to worry about circular dependencies either.

alberts

31-08-2006 11:08:24

Yep, if you want to ref-count things, Shared_Ptr's are the way to go. Weak_Ptr's are used with them, used properly, you will never have to worry about circular dependencies either.

Looks interesting... But too complicated for me now :)

OvermindDL1

31-08-2006 20:45:04

Nah, you use shared_ptr's like pointers, the usual -> and all. Auto pointers you extract a shared_ptr out of to use it, the shared_ptr will be zero if the object is already deleted. Use auto_ptr to keep track of things that you may use if they exist. Shared_ptr's to keep an active reference.