bullet3
bullet3 copied to clipboard
Multi Threading Example Memory error
I am experimenting with the multithreading capability of bullet in the following simple example I get unknown address error. The btCollisionConfiguration is initialized correctly and I would appreciate some insight into the problem. here is the example:
#include <btBulletDynamicsCommon.h>
#include "BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolverMt.h"
#include "BulletCollision/CollisionDispatch/btCollisionDispatcherMt.h"
#include "BulletCollision/BroadphaseCollision/btDbvtBroadphase.h"
#include "BulletCollision/CollisionShapes/btBoxShape.h"
#include "BulletDynamics/Dynamics/btDiscreteDynamicsWorld.h"
int main() {
auto collisionConfig = std::make_unique<btDefaultCollisionConfiguration>();
auto dispatcher = std::make_unique<btCollisionDispatcherMt>(collisionConfig.get());
auto broadphase = std::make_unique<btDbvtBroadphase>();
auto solver = std::make_unique<btSequentialImpulseConstraintSolverMt>();
auto dynamicsWorld = std::make_unique<btDiscreteDynamicsWorld>(dispatcher.get(), broadphase.get(), solver.get(), collisionConfig.get());
dynamicsWorld->setGravity(btVector3(0, -10, 0));
auto boxShape = std::make_unique<btBoxShape>(btVector3(1, 1, 1));
btScalar mass = 1.0;
btVector3 localInertia(0, 0, 0);
boxShape->calculateLocalInertia(mass, localInertia);
btTransform startTransform;
startTransform.setIdentity();
startTransform.setOrigin(btVector3(0, 50, 0));
auto motionState = std::make_unique<btDefaultMotionState>(startTransform);
btRigidBody::btRigidBodyConstructionInfo rbInfo(mass, motionState.get(), boxShape.get(), localInertia);
auto body = std::make_unique<btRigidBody>(rbInfo);
dynamicsWorld->addRigidBody(body.get());
for (int i = 0; i < 100; ++i) {
dynamicsWorld->stepSimulation(1.f / 60.f, 10);
}
return 0;
}
here is my Cmake file:
cmake_minimum_required(VERSION 3.10)
project(PhysicsSimulation)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Debug")
endif()
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
add_compile_options(-fsanitize=address)
add_link_options(-fsanitize=address)
endif()
find_package(Bullet REQUIRED)
add_executable(PhysicsSimulation PerformanceTest.cpp)
target_include_directories(PhysicsSimulation PUBLIC ${BULLET_INCLUDE_DIRS})
target_link_libraries(PhysicsSimulation PUBLIC ${BULLET_LIBRARIES})
target_compile_definitions(PhysicsSimulation PUBLIC BT_USE_DOUBLE_PRECISION)
set_target_properties(PhysicsSimulation PROPERTIES
DEBUG_POSTFIX "_Debug"
MINSIZEREL_POSTFIX "_MinsizeRel"
RELWITHDEBINFO_POSTFIX "_RelWithDebugInfo"
)
Here is the error:
AddressSanitizer:DEADLYSIGNAL
=================================================================
==45038==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x000105458a54 bp 0x00016b02a970 sp 0x00016b02a920 T0)
==45038==The signal is caused by a READ memory access.
==45038==Hint: address points to the zero page.
#0 0x105458a54 in btCollisionDispatcherMt::btCollisionDispatcherMt(btCollisionConfiguration*, int)+0x6c (libBulletCollision.3.26.dylib:arm64+0x18a54)
#1 0x104dda3c4 in std::__1::__unique_if<btCollisionDispatcherMt>::__unique_single std::__1::make_unique[abi:ue170006]<btCollisionDispatcherMt, btDefaultCollisionConfiguration*>(btDefaultCollisionConfiguration*&&) unique_ptr.h:689
#2 0x104dd9158 in main PerformanceTest.cpp:11
#3 0x19236a0dc (<unknown module>)
==45038==Register values:
x[0] = 0x0000000000000000 x[1] = 0x0000000000000023 x[2] = 0x0000000000000023 x[3] = 0x0000000105000010
x[4] = 0x0000000105000280 x[5] = 0x0000000000000000 x[6] = 0x000000016a830000 x[7] = 0x0000000000000001
x[8] = 0x0000000105292000 x[9] = 0x0000000000005170 x[10] = 0x0000000000000005 x[11] = 0x00000000ffffffff
x[12] = 0x0000000000000000 x[13] = 0xa11de761ea9f2253 x[14] = 0x0000000000007e01 x[15] = 0x0000000000000006
x[16] = 0x0000000105286f0c x[17] = 0x0000000105a440b8 x[18] = 0x0000000000000000 x[19] = 0x0000000105000200
x[20] = 0x0000000105005350 x[21] = 0x0000000105005370 x[22] = 0x0000000000000028 x[23] = 0x0000000105005354
x[24] = 0x000000016b02b450 x[25] = 0x00000001923de2db x[26] = 0x0000000000000000 x[27] = 0x0000000000000000
x[28] = 0x0000000000000000 fp = 0x000000016b02a970 lr = 0x0000000105458a54 sp = 0x000000016b02a920
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (libBulletCollision.3.26.dylib:arm64+0x18a54) in btCollisionDispatcherMt::btCollisionDispatcherMt(btCollisionConfiguration*, int)+0x6c
==45038==ABORTING
zsh: abort ./PhysicsSimulation_Debug