NewtonBodyGetMatrix leads to BSOD

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

NewtonBodyGetMatrix leads to BSOD

Postby Xaymar » Sun Jul 11, 2010 8:50 am

Code: Select all
DLL_EXPORT int DLL_CALL BBNewton_Body_GetMatrix(LP BodyPtr, LP MatrixBank) {
   float MatrixArr[16];
   BBNewton_Body* TempBody = CASTTO(BBNewton_Body*, BodyPtr);
   NewtonBodyGetMatrix(TempBody->nbBody, &MatrixArr[0]);  //This causes Crash
   for(int X = 0; X<16; X++) {
      BBBank_SetFloat(MatrixBank, X*4, MatrixArr[X]);
   }

   return true;
}

This code leads(at the marked spot) to a Bluescreen. But according to the Wiki and multiple Sites this isnt wrong. I'm using MingW C++(Latest) with Library NewtonWin-2.22\sdk\x32\dll_vs9\newton.lib . None of my teammembers that know gnu c++ / vc++ have found any error, mistake or typo.

What is wrong? :S NewtonBodyGetMatrix is the only API function causing it.

(Im using 2.15 still, going to update to 2.23 when i found out what causes that. Yes its a BlitzBasic3D/Plus Wrapper for Newton, which should be free.)
Xaymar
 
Posts: 8
Joined: Sat Jul 10, 2010 7:49 pm

Re: NewtonBodyGetMatrix leads to BSOD

Postby Julio Jerez » Sun Jul 11, 2010 9:18 am

the only way that can cause a crash is if you are using the wrong DLL, you may be usin he double precision and tha will copy twice as mush data, causing stack overrun.
make sure you have the right dll.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: NewtonBodyGetMatrix leads to BSOD

Postby JernejL » Sun Jul 11, 2010 9:20 am

This is plain impossible, newton does not even interact at kernel level with anything that could cause such a system instability.

You should identify if your system has secondary problems that such a condition call could cause a bsod:
- bad ram
- overclocked cpu

Also identify if the particular matrix has any problems (invalid data, absurd values) that could corrupt the graphics engine trying to render a object with such a matrix, causing a instability within the graphics drivers (try to update those drivers too, see if the bsod tells you which driver crashed).
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1587
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: NewtonBodyGetMatrix leads to BSOD

Postby Xaymar » Sun Jul 11, 2010 9:30 am

I enabled the VC++ Windows SDK Realtime Debugger(It debugs anything that has been compiled with any C++ language) and it tells me that im accessing invalid Memory. I'll see if i can get a better result with converting it to Visual C++ fully. i also checked if im using the wrong DLL, none of them changed anything. Edit: Nothing has changed. Even with VC++ i still get either a crash or the debugger tells me im using invalid memory
Xaymar
 
Posts: 8
Joined: Sat Jul 10, 2010 7:49 pm

Re: NewtonBodyGetMatrix leads to BSOD

Postby Xaymar » Sun Jul 11, 2010 11:43 am

@Delfi: Thanks, that fixed it. The Matrix was holding very absurd values(float limit). i dont know what caused that, but after i created a second object everything worked fine.
Xaymar
 
Posts: 8
Joined: Sat Jul 10, 2010 7:49 pm

Re: NewtonBodyGetMatrix leads to BSOD

Postby JernejL » Sun Jul 11, 2010 12:06 pm

interesting, so it was the matrix was causing a bsod in the graphics drivers? it would be good to know which brand drivers this were ;)

btw, the matrix from newton should not come as corrupt, it's a standard 4x4 matrix, can you describe what the simulation does with a body to get such odd values back? did you set the matrix first to a clean state before calling newtonupdate?
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1587
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: NewtonBodyGetMatrix leads to BSOD

Postby Xaymar » Mon Jul 12, 2010 10:35 am

It was Nvidia Forceware(Latest for Nvidia Geforce 9600 GT).

Pseodocode of what it does:
Code: Select all
Program start
-Create NewtonWorld MainWorld
-Load Physics Objects
-Load Scene Objects
-Initialize Renderer
-Load Physics Objects into VertexBuffer
-Mainloop
--Renderscene
--NewtonUpdate(MainWorld)
--Apply Physics forces/torque
-Mainloop end
-Deinitialize Renderer
-Destroy NewtonBodies, NewtonCollisions and NewtonWorlds
-ExitProcess


I got a problem with the callbacks in my BlitzBasic Wrapper now. According to the built-in debugger these are never getting called(GNU C++ and VC++). I've excluded the debugger for better readable code :)

Main.h
Code: Select all
//Includes
#include <windows.h>
#include <string>
#include <sstream>

#include "Newton.h"

#include "version.h"

//Macros
#define DLL_EXPORT extern "C" _declspec(dllexport)
#define DLL_IMPORT extern "C" _declspec(dllimport)
#define DLL_CALL _stdcall

//Extended Macros
#define LP               int                     // Integer Adress
#define STR                 const char*                 // String
#define CASTTO(c, p)        (reinterpret_cast<c>(p))    // Cast anything to Anything
#define CAST(p)             CASTTO(LP, p)               // Cast anything to Integer Adress

//!BB Bank Helper
int BBBank_GetInt(LP Bank, int Offset) {
   return *(int*)(Bank+Offset);
}
unsigned char BBBank_GetByte(LP Bank, int Offset) {
   return *(unsigned char*)(Bank+Offset);
}
float BBBank_GetFloat(LP Bank, int Offset) {
   return *(float*)(Bank+Offset);
}
void BBBank_SetInt(LP Bank, int Offset, int Value) {
   *(int*)(Bank+Offset) = Value;
}
void BBBank_SetByte(LP Bank, int Offset, unsigned char Value) {
   *(unsigned char*)(Bank+Offset) = Value;
}
void BBBank_SetFloat(LP Bank, int Offset, float Value) {
   *(float*)(Bank+Offset) = Value;
}


//!Structures
struct BBNewton_World {
    //Newton World
    NewtonWorld* nwWorld;

    //Settings
    float fWorldMin[3];
    float fWorldMax[3];

    bool bGravityEnabled;
    float fGravity[3];

    float fTimescale;

    //Linked List
    BBNewton_World *pPrevious;
    BBNewton_World *pNext;

    //De-/Constructor
    BBNewton_World();
    ~BBNewton_World();
};
BBNewton_World *pbbnwFirst;
BBNewton_World *pbbnwLast;

BBNewton_World::BBNewton_World() {
   this->nwWorld = NewtonCreate();

   if (pbbnwFirst == NULL) { pbbnwFirst = this; }
   if (pbbnwLast != NULL) {
      pbbnwLast->pNext = this;
      this->pPrevious = pbbnwLast;
      pbbnwLast = this;
   } else {
      pbbnwLast = this;
      this->pPrevious = NULL;
   }
   this->pNext = NULL;
}
BBNewton_World::~BBNewton_World() {
   if (this->nwWorld != NULL) { NewtonDestroy(this->nwWorld); }

   if (this->pPrevious != NULL) {
      this->pPrevious->pNext = this->pNext;
   } else {
      if (this == pbbnwFirst) {
         pbbnwFirst = this->pNext;
      }
   }
   if (this->pNext != NULL) {
      this->pNext->pPrevious = this->pPrevious;
   } else {
      if (this == pbbnwLast) {
         pbbnwLast = this->pPrevious;
      }
   }
}


#define BBNEWTON_COL_NULL      0
#define BBNEWTON_COL_SPHERE      1
#define BBNEWTON_COL_BOX      2

struct BBNewton_Collision {
    //Newton Collision
    BBNewton_World* bbnwWorld;
    NewtonCollision* ncCollision;
    NewtonBody* nbTempBody;

    //Settings


    //Linked List
    BBNewton_Collision *pPrevious;
    BBNewton_Collision *pNext;

    //De-/Constructor
    BBNewton_Collision(BBNewton_World* World, int iColType, LP Data, LP OffsetMatrix);
    ~BBNewton_Collision();
};
BBNewton_Collision *pbbncFirst;
BBNewton_Collision *pbbncLast;

BBNewton_Collision::BBNewton_Collision(BBNewton_World* World, int iColType, LP Data, LP OffsetMatrix) {
   float OffsetMatrixArr[16];

   for(int X = 0; X <= 15; X++) {
      OffsetMatrixArr[X] = BBBank_GetFloat(OffsetMatrix, X*4);
   }

   this->bbnwWorld = World;
   switch (iColType) {
      case BBNEWTON_COL_NULL:
         this->ncCollision = NewtonCreateNull(this->bbnwWorld->nwWorld);
         break;
      case BBNEWTON_COL_SPHERE:
         this->ncCollision = NewtonCreateSphere(this->bbnwWorld->nwWorld, BBBank_GetFloat(Data, 0), BBBank_GetFloat(Data, 4), BBBank_GetFloat(Data, 8), BBBank_GetInt(Data, 12), &OffsetMatrixArr[0]);
         break;
      case BBNEWTON_COL_BOX:
         this->ncCollision = NewtonCreateBox(this->bbnwWorld->nwWorld, BBBank_GetFloat(Data, 0), BBBank_GetFloat(Data, 4), BBBank_GetFloat(Data, 8), BBBank_GetInt(Data, 12), &OffsetMatrixArr[0]);
         break;
   }
   NewtonAddCollisionReference(this->ncCollision);

   if (pbbncFirst == NULL) { pbbncFirst = this; }
   if (pbbncLast != NULL) {
      pbbncLast->pNext = this;
      this->pPrevious = pbbncLast;
      pbbncLast = this;
   } else {
      pbbncLast = this;
      this->pPrevious = NULL;
   }
   this->pNext = NULL;
}
BBNewton_Collision::~BBNewton_Collision() {
   if (this->ncCollision != NULL) { NewtonReleaseCollision(this->bbnwWorld->nwWorld, this->ncCollision); }

   if (this->pPrevious != NULL) {
      this->pPrevious->pNext = this->pNext;
   } else {
      if (this == pbbncFirst) {
         pbbncFirst = this->pNext;
      }
   }
   if (this->pNext != NULL) {
      this->pNext->pPrevious = this->pPrevious;
   } else {
      if (this == pbbncLast) {
         pbbncLast = this->pPrevious;
      }
   }
}


struct BBNewton_Body {
   BBNewton_World* bbnwWorld;
   BBNewton_Collision* bbncCollision;
   NewtonBody* nbBody;

   //Settings
   float Force[3];
   float Torque[3];
   float Matrix[16];

   //Linked List
   BBNewton_Body *pPrevious;
   BBNewton_Body *pNext;

   //Constructor/Deconstructor
   BBNewton_Body(BBNewton_World* World, BBNewton_Collision* Collision);
   ~BBNewton_Body();
};
BBNewton_Body *pbbnbFirst;
BBNewton_Body *pbbnbLast;

BBNewton_Body::BBNewton_Body(BBNewton_World* World, BBNewton_Collision* Collision) {
   this->bbnwWorld = World;
   this->bbncCollision = Collision;
   this->nbBody = NewtonCreateBody(this->bbnwWorld->nwWorld, this->bbncCollision->ncCollision);
   NewtonBodySetUserData(this->nbBody, CASTTO(void*,this));

   if (pbbnbFirst == NULL) { pbbnbFirst = this; }
   if (pbbnbLast != NULL) {
      pbbnbLast->pNext = this;
      this->pPrevious = pbbnbLast;
      pbbnbLast = this;
   } else {
      pbbnbLast = this;
      this->pPrevious = NULL;
   }
   this->pNext = NULL;
}
BBNewton_Body::~BBNewton_Body() {
   if (this->nbBody != NULL) { NewtonDestroyBody(this->bbnwWorld->nwWorld, this->nbBody); }

   if (this->pPrevious != NULL) { this->pPrevious->pNext = this->pNext; } else {
      if (this == pbbnbFirst) {
         pbbnbFirst = this->pNext;
      }
   }
   if (this->pNext != NULL) { this->pNext->pPrevious = this->pPrevious; } else {
      if (this == pbbnbLast) {
         pbbnbLast = this->pPrevious;
      }
   }
}


struct BBNewton_Material {
   BBNewton_World* bbnwWorld;
   NewtonMaterial* nmMaterial;

   //Settings

   //Linked List
   BBNewton_Material *pPrevius;
   BBNewton_Material *pNext;

   //Constructor/Deconstructor
   BBNewton_Material();
   ~BBNewton_Material();
};
BBNewton_Material *pbbnmFirst;
BBNewton_Material *pbbnmLast;


Code: Select all
#include "main.h"

//!Callbacks
static void Wrapper_NewtonApplyForceAndTorque(const NewtonBody* body, float timeStep, int threadId) {
   BBNewton_Body* TempBody = CASTTO(BBNewton_Body*, NewtonBodyGetUserData(body));

   NewtonBodyAddForce(body, &TempBody->Force[0]);
   NewtonBodyAddTorque(body, &TempBody->Torque[0]);

   float force[3]; float mass; float inertia[3];

   NewtonBodyGetMassMatrix(body, &mass, &inertia[0], &inertia[1], &inertia[2]);

   force[0] = TempBody->bbnwWorld->fGravity[0] * mass;
   force[1] = TempBody->bbnwWorld->fGravity[1] * mass;
   force[2] = TempBody->bbnwWorld->fGravity[2] * mass;

   if (TempBody->bbnwWorld->bGravityEnabled == TRUE) { NewtonBodyAddForce(body, &force[0]); }
}
static void Wrapper_NewtonBodyLeaveWorld(const NewtonBody* body, int threadId) {
   NewtonBodySetFreezeState(body, TRUE);
}
static void Wrapper_NewtonSetTransform(const NewtonBody* body, const float* matrix, int threadId) {
   BBNewton_Body* TempBody = CASTTO(BBNewton_Body*, NewtonBodyGetUserData(body));

   NewtonBodySetMatrix(body, matrix);

   for (int X = 0;X<=15;X++) {
      TempBody->Matrix[X] = matrix[X];
   }
}

//!Wrapper World Functions
DLL_EXPORT int DLL_CALL BBNewton_World_Create() {
   BBNewton_World* TempWorld = new BBNewton_World;
   NewtonSetBodyLeaveWorldEvent(TempWorld->nwWorld, Wrapper_NewtonBodyLeaveWorld);
   return CAST(TempWorld);
}
DLL_EXPORT int DLL_CALL BBNewton_World_SetBounds(LP WorldPtr, float fMinX, float fMinY, float fMinZ, float fMaxX, float fMaxY, float fMaxZ) {
   BBNewton_World* TempWorld = CASTTO(BBNewton_World*, WorldPtr);
   TempWorld->fWorldMin[0] = fMinX;   TempWorld->fWorldMin[1] = fMinY;   TempWorld->fWorldMin[2] = fMinZ;
   TempWorld->fWorldMax[0] = fMaxX;   TempWorld->fWorldMax[1] = fMaxY;   TempWorld->fWorldMax[2] = fMaxZ;
   NewtonSetWorldSize(TempWorld->nwWorld, TempWorld->fWorldMin, TempWorld->fWorldMax);
   return true;
}
DLL_EXPORT int DLL_CALL BBNewton_World_SetGravity(LP WorldPtr, float fGravityX, float fGravityY, float fGravityZ) {
   BBNewton_World* TempWorld = CASTTO(BBNewton_World*, WorldPtr);
   TempWorld->fGravity[0] = fGravityX;   TempWorld->fGravity[1] = fGravityY;   TempWorld->fGravity[2] = fGravityZ;
   return true;
}
DLL_EXPORT int DLL_CALL BBNewton_World_SetGravityState(LP WorldPtr, int iGravity) {
   BBNewton_World* TempWorld = CASTTO(BBNewton_World*, WorldPtr);
   TempWorld->bGravityEnabled = iGravity;
   return true;
}
DLL_EXPORT int DLL_CALL BBNewton_World_SetTimeScale(LP WorldPtr, float fTimescale) {
   BBNewton_World* TempWorld = CASTTO(BBNewton_World*, WorldPtr);
   TempWorld->fTimescale = fTimescale;
   return true;
}
DLL_EXPORT int DLL_CALL BBNewton_World_Update(LP WorldPtr, float fTimeStep) {
   BBNewton_World* TempWorld = CASTTO(BBNewton_World*, WorldPtr);
   NewtonUpdate(TempWorld->nwWorld, fTimeStep);
   return true;
}
DLL_EXPORT int DLL_CALL BBNewton_World_SetSolverModel(LP WorldPtr, int iSolverModel) {
   BBNewton_World* TempWorld = CASTTO(BBNewton_World*, WorldPtr);
   NewtonSetSolverModel(TempWorld->nwWorld, iSolverModel);
   return true;
}
DLL_EXPORT int DLL_CALL BBNewton_World_SetFrictionModel(LP WorldPtr, int iFrictionModel) {
   BBNewton_World* TempWorld = CASTTO(BBNewton_World*, WorldPtr);
   NewtonSetFrictionModel(TempWorld->nwWorld, iFrictionModel);
   return true;
}
DLL_EXPORT int DLL_CALL BBNewton_World_Destroy(LP WorldPtr) {
   BBNewton_World* TempWorld = CASTTO(BBNewton_World*, WorldPtr);
   delete TempWorld;
   return true;
}

//!Wrapper Collision Functions
DLL_EXPORT int DLL_CALL BBNewton_Collision_Create(LP WorldPtr, int iColType, LP Data, LP OffsetMatrix) {
   BBNewton_Collision* TempCollision = new BBNewton_Collision(CASTTO(BBNewton_World*, WorldPtr), iColType, Data, OffsetMatrix);
   return CAST(TempCollision);
}
DLL_EXPORT float DLL_CALL BBNewton_Collision_Raycast(LP CollisionPtr, LP StartData, LP EndData, LP NormalData, LP AttributeData) {
   BBNewton_Collision* TempCollision = CASTTO(BBNewton_Collision*, CollisionPtr);
   return NewtonCollisionRayCast(TempCollision->ncCollision, CASTTO(float*, StartData), CASTTO(float*, EndData), CASTTO(float*, NormalData), CASTTO(int*, AttributeData));
}
DLL_EXPORT int DLL_CALL BBNewton_Collision_Destroy(LP CollisionPtr) {
   BBNewton_Collision* TempCollision = CASTTO(BBNewton_Collision*, CollisionPtr);
   delete TempCollision;
   return true;
}

//!Wrapper Body Functions
DLL_EXPORT int DLL_CALL BBNewton_Body_Create(LP WorldPtr, LP CollisionPtr) {
   BBNewton_Body* TempBody = new BBNewton_Body(CASTTO(BBNewton_World*, WorldPtr), CASTTO(BBNewton_Collision*, CollisionPtr));
   NewtonBodySetForceAndTorqueCallback(TempBody->nbBody, &Wrapper_NewtonApplyForceAndTorque);
   NewtonBodySetTransformCallback(TempBody->nbBody, &Wrapper_NewtonSetTransform);
   return CAST(TempBody);
}
DLL_EXPORT int DLL_CALL BBNewton_Body_GetMatrix(LP BodyPtr, LP MatrixBank) {
   BBNewton_Body* TempBody = CASTTO(BBNewton_Body*, BodyPtr);
   NewtonBodyGetMatrix(TempBody->nbBody, CASTTO(float*, MatrixBank));
   return true;
}
DLL_EXPORT int DLL_CALL BBNewton_Body_SetMatrix(LP BodyPtr, LP MatrixBank) {
   BBNewton_Body* TempBody = CASTTO(BBNewton_Body*, BodyPtr);
   NewtonBodySetMatrix(TempBody->nbBody, CASTTO(float*, MatrixBank));
   return true;
}
DLL_EXPORT int DLL_CALL BBNewton_Body_GetMatrixT(LP BodyPtr, LP MatrixBank) {
   BBNewton_Body* TempBody = CASTTO(BBNewton_Body*, BodyPtr);
   for (int X = 0;X<=15;X++) {
      BBBank_SetFloat(MatrixBank, X*4, TempBody->Matrix[X]);
   }
   return true;
}
DLL_EXPORT int DLL_CALL BBNewton_Body_SetMatrixT(LP BodyPtr, LP MatrixBank) {
   BBNewton_Body* TempBody = CASTTO(BBNewton_Body*, BodyPtr);
   for (int X = 0;X<=15;X++) {
      TempBody->Matrix[X] = BBBank_GetFloat(MatrixBank, X*4);
   }
   return true;
}
DLL_EXPORT int DLL_CALL BBNewton_Body_SetFreeze(LP BodyPtr, int State) {
   BBNewton_Body* TempBody = CASTTO(BBNewton_Body*, BodyPtr);
   NewtonBodySetFreezeState(TempBody->nbBody, State);
   return true;
}
DLL_EXPORT int DLL_CALL BBNewton_Body_GetFreeze(LP BodyPtr) {
   BBNewton_Body* TempBody = CASTTO(BBNewton_Body*, BodyPtr);
   return NewtonBodyGetFreezeState(TempBody->nbBody);
}
DLL_EXPORT int DLL_CALL BBNewton_Body_Destroy(LP BodyPtr) {
   BBNewton_Body* TempBody = CASTTO(BBNewton_Body*, BodyPtr);
   delete TempBody;
   return true;
}

//!Wrapper Helper Functions
DLL_EXPORT STR DLL_CALL BBNewton_Wrapper_Version() {
   std::stringstream Version;
   Version<<"BBNewton "<<AutoVersion::STATUS<<" "<<AutoVersion::STATUS_SHORT<<AutoVersion::FULLVERSION_STRING<<std::endl;
   Version<<"Newton "<<NEWTON_MAJOR_VERSION<<"."<<NEWTON_MINOR_VERSION<<std::endl;
   Version<<"Date "<<AutoVersion::DATE<<"."<<AutoVersion::MONTH<<"."<<AutoVersion::YEAR;
   return Version.str().c_str();
}
DLL_EXPORT STR DLL_CALL BBNewton_Wrapper_Credits() {
   std::stringstream Credits;
   Credits<<"$%"<<"FF7E00"<<" - Newton BB Wrapper - "<<std::endl;
   Credits<<"$%"<<"FFFFFF"<<"Michael Dirks (www.levelnull.de)"<<std::endl;
   Credits<<"$%"<<"FFFFFF"<<std::endl;
   Credits<<"$%"<<"FF7E00"<<" - Many thanks go to - "<<std::endl;
   Credits<<"$%"<<"FFFFFF"<<"Lukas Banana (www.hlc-games.de)"<<std::endl;
   Credits<<"$%"<<"FFFFFF"<<"Newton Engine Team (www.newtongamedynamics.com)";
   return Credits.str().c_str();
}

//!WinAPI
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
    switch (fdwReason) {
        case DLL_PROCESS_ATTACH:
            // attach to process
            // return FALSE to fail DLL load

            break;
        case DLL_PROCESS_DETACH:
            // detach from process
            break;

        case DLL_THREAD_ATTACH:
            // attach to thread
            break;

        case DLL_THREAD_DETACH:
            // detach from thread
            break;
    }
    return TRUE; // succesful
}


I cant find any error nor mistake.

did you set the matrix first to a clean state before calling NewtonUpdate?

What do you mean by that? Setting it to a [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]] Matrix?
Xaymar
 
Posts: 8
Joined: Sat Jul 10, 2010 7:49 pm


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 1 guest

cron