question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

recursive use of an object detected which would lead to unsafe aliasing in rust

See original GitHub issue

Using rapier2d-compat version 0.7.6

// World contains some static and dynamic body
world.forEachRigidBody(body =>{
  const collidersLength = body.numColliders();

  for (let i = 0; i < collidersLength; i++) {
    const collider = world.getCollider(body.collider(i));
    drawBody(ctx, collider, body);
  }

  if (body.bodyType() === RAPIER.RigidBodyType.Static) {
    return
  }

  const bodyPosition = body.translation();

  const bodyMass = body.mass();

  world.forEachRigidBody(otherBody => {
    if (body.handle === otherBody.handle) {
      return
    }

    const otherBodyMass = otherBody.mass();

    const otherBodyPosition = otherBody.translation();

    const distance = getDistance(otherBodyPosition, bodyPosition);

    const forceDirection = getDirection(
      otherBodyPosition,
      bodyPosition
    );
    const forceMagnitude = getGravitationalForce(
      otherBodyMass,
      bodyMass,
      distance
    );
    try {
      body.applyForce(
        new RAPIER.Vector2(
          (Math.sin(forceDirection) * forceMagnitude) /
            Math.sqrt(bodyMass),
          (Math.cos(forceDirection) * forceMagnitude) /
            Math.sqrt(bodyMass)
        ),
        true
      );
    } catch (error) {
      console.error(error);
    }
  })
})

This code always throws as soon as I have 2 bodies (1 static & 1 dynamic, 2 statics, 2 dynamics, whatever) with the error recursive use of an object detected which would lead to unsafe aliasing in rust.

image

The error also happens with forEachRigidBodyHandle

The same code using forEachCollider works (but I wanna use forEachRigidBody because some bodies have multiple colliders)

world.forEachCollider((collider) => {
  const body = world.getRigidBody(collider.parent());

  drawBody(ctx, collider, body);

  if (body.bodyType() === RAPIER.RigidBodyType.Static) {
    return;
  }

  const bodyPosition = body.translation();

  const bodyMass = body.mass();

  world.forEachCollider((otherCollider) => {
    const otherBody = world.getRigidBody(otherCollider.parent());

    if (body.handle === otherBody.handle) {
      return;
    }

    const otherBodyMass = otherBody.mass();

    const otherBodyPosition = otherBody.translation();

    const distance = getDistance(otherBodyPosition, bodyPosition);

    const forceDirection = getDirection(
      otherBodyPosition,
      bodyPosition
    );
    const forceMagnitude = getGravitationalForce(
      otherBodyMass,
      bodyMass,
      distance
    );
    try {
      body.applyForce(
        new RAPIER.Vector2(
          (Math.sin(forceDirection) * forceMagnitude) /
            Math.sqrt(bodyMass),
          (Math.cos(forceDirection) * forceMagnitude) /
            Math.sqrt(bodyMass)
        ),
        true
      );
    } catch (error) {
      console.error(error);
    }
  });
});

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
LeXXikcommented, Nov 18, 2022

Ah, of course, you are right on that one. Completely forgot about the event callbacks 😅 I am scheduling all event updates to the next simulation step, thats why I don’t have it. I wonder why Ammo doesn’t crash, though 🤔 I guess there is some safety mechanism in place to prevent it.

1reaction
racer161commented, Mar 22, 2022

@jcyuan I found out I was starting a new runtime loop before I finished the last due to async/await. So this was indeed the problem. thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

recursive use of an object detected which would lead ... - GitHub
Summary Hi, I am porting the "gl-matrix" to webassembly with wasm-bindgen. In "gl-matrix", we can find lots of those methods: // module vec3 ......
Read more >
Recursive use of an object detected which would lead to ...
Recursive use of an object detected which would lead to unsafe aliasing in rust.
Read more >
lib.rs.html -- source - Docs.rs
{ super::throw("recursive use of an object detected which would lead to \ unsafe aliasing in rust"); } #[no_mangle] pub extern fn __wbindgen_malloc(size: ...
Read more >
rust - Passing mutable struct to JavaScript callback and keep ...
I'm trying to pass a struct from Rust to a JavaScript callback with ... recursive use of an object detected which would lead...
Read more >
Rust: throw: closure invoked recursively or destroyed already?
SOLVED: The error message isn't clear about the rust variable being ... No idea what wasm-function[190]:27 is, would be nice to get a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found