21-06-2018, 08:33 AM
(This post was last modified: 21-06-2018, 08:37 AM by josemendez.)
(19-06-2018, 10:50 AM)M. Hanssen Wrote: Thanks for the quick reply. I understand that I mixed up the constraint indices with the particle indices. However even after adjusting the code to remove the right index, the access violation error still triggers at random.
The weird thing is that when I try to trigger the access violation by feeding the pinconstraints non existing indices, the access violation crash is not triggered!
The current code is:
Code:public override T Spawn<T>(WorldObject worldObject)
{
T genericSpawnedObject = base.Spawn<T>(worldObject);
HitablePulleyWorldObject spawnedObject = genericSpawnedObject as HitablePulleyWorldObject;
if (spawnedObject != null)
{
ObiPinConstraintBatch batch = PinConstraints.GetFirstBatch();
if (batch != null)
{
PinConstraints.RemoveFromSolver(null);
int index = GetClosestParticleIndex();
if (index >= 0)
{
batch.AddConstraint(index, spawnedObject.GetComponentInChildren<ObiCollider>(), Vector3.zero, 1);
spawnedObject.ConstraintIndex = batch.ConstraintCount - 1;
spawnedObject.PinConstraints = PinConstraints;
}
PinConstraints.AddToSolver(null);
}
}
return genericSpawnedObject;
}
private int GetClosestParticleIndex()
{
int result = -1;
float closestDistance = float.MaxValue;
for (int i = 0; i < Rope.invMasses.Length; i++)
{
if (Rope.invMasses[i] > 0)
{
float distance = Vector3.Distance(Rope.GetParticlePosition(i), transform.position);
if (distance < closestDistance)
{
closestDistance = distance;
result = i;
}
}
}
return result;
}
Code:private void DetachFromRope()
{
ObiPinConstraintBatch batch = PinConstraints.GetFirstBatch();
if (batch != null && ConstraintIndex < batch.ConstraintCount)
{
PinConstraints.RemoveFromSolver(null);
batch.RemoveConstraint(ConstraintIndex);
PinConstraints.AddToSolver(null);
}
Object.Destroy(GetComponent<ObiCollider>());
}
Is there still something wrong with this?
And above all, why is it that the whole application crashes by inserting a wrong index according to you?
Cant this be fixed by throwing a safe exception? An access violation crash seems a bit harsh for an out of bounds exception!
I hope you can help me resolve this issue.
The new dump file of the crash that is still occurring: https://we.tl/SoBARZU6P0
Hi,
I'll take a look at your code asap and get back to you.
Obi uses raw, unmanaged memory buffers for performance reasons. Including range check for all indices passed trough its API (which are accessed very frame multiple times, for potentially thousands of particles) would defeat the purpose of using unmanaged memory which is precisely to avoid the checks performed by managed code and scrape performance out of it. Many other low-level buffer based c APIs (OpenGL, Flex) behave the same way: they assume you pass well-formed data to them. If the input is inconsistent, behaviour is undefined in most cases: it might work, it might not, or it might crash. Performing a range check yourself just once, before calling the API is both faster and easier, as it allows you to bail out before even entering unmanaged territory.