64 Bit UE how much bytes using memory alignment?


I added thirdparty library as a plugin in unreal (Bullet Physics. Not a replacement of physx just for some functions). But when i use my working code in unreal it gives “unable to read memory” errors.

When i debug it i see unable to read memory errors on variables, actors …

So i am thinking maybe Bullets memory management collides with unreals. Bullet uses 16byte alignment. Is this can be source of problem ? Maybe if i tell bullet use same alignment as unreal this can be solution to this problem ?

// Fill out your copyright notice in the Description page of Project Settings.

#include "SharpSurgeon.h"
#include "TestActor.h"

#include "btBulletDynamicsCommon.h"
#include "BulletSoftBody/btSoftRigidDynamicsWorld.h"
#include "BulletSoftBody/btDefaultSoftBodySolver.h"
#include "BulletSoftBody/btSoftBodyHelpers.h"
#include "BulletSoftBody/btSoftBodyRigidBodyCollisionConfiguration.h"

struct bulletObject {
	int id;
	float r, g, b;
	bool hit;
	btRigidBody* body;
	bulletObject(btRigidBody* b, int i, float r0, float g0, float b0) : body(b), id(i), r(r0), g(g0), b(b0), hit(false) {}

btSoftRigidDynamicsWorld* world;
btDispatcher* dispatcher;
btCollisionConfiguration* collisionConfig;
btBroadphaseInterface* broadphase;
btConstraintSolver* solver;
btSoftBodySolver* softbodySolver;
TArray<bulletObject*> bodies;
btSoftBody* softBody;
btTransform t;
btMotionState* motion;
btStaticPlaneShape* plane;
btRigidBody* body;
btRigidBody::btRigidBodyConstructionInfo info(0.0, motion, plane);

// Sets default values
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

// Called when the game starts or when spawned
void ATestActor::BeginPlay()
	collisionConfig = new btSoftBodyRigidBodyCollisionConfiguration();
	dispatcher = new btCollisionDispatcher(collisionConfig);
	broadphase = new btDbvtBroadphase();
	solver = new btSequentialImpulseConstraintSolver();
	softbodySolver = new btDefaultSoftBodySolver();
	world = new btSoftRigidDynamicsWorld(dispatcher, broadphase, solver, collisionConfig, softbodySolver);
	world->setGravity(btVector3(0, -10, 0));

	t.setOrigin(btVector3(0, 0, 0));
	plane = new btStaticPlaneShape(btVector3(0, 1, 0), 0);
	motion = new btDefaultMotionState(t);

	body = new btRigidBody(info);
	bodies.Add(new bulletObject(body, 4, 0.8, 0.8, 0.8));
	body->setUserPointer(bodies[bodies.Num() - 1]);

	float s = 4;
	float h = 20;

	softBody = btSoftBodyHelpers::CreateEllipsoid(world->getWorldInfo(),
		btVector3(10, 10, 10), btVector3(2, 2, 2), 1000);
	softBody->m_cfg.viterations = 50;
	softBody->m_cfg.piterations = 50;
	softBody->m_cfg.kPR = 1000;
	softBody->setMass(0, 0);


// Called every frame
void ATestActor::Tick( float DeltaTime )
	Super::Tick( DeltaTime );


I have the exact same problem as you, also using the Bullet library. Mine works sometimes, sometimes crashes. I figured maybe changing the DEFAULT_ALIGNMENT or MIN_ALIGNMENT to 16 inside MemoryBase.h to match Bullet’s 16 bit alignment might do the trick, but nope still getting crashes.

I do know that whenever including bullet headers, you need to wrap them with:


So for example:


#include <BulletCollision/CollisionShapes/btBoxShape.h>


But even doing that it hasn’t fixed my issue, but maybe will help you (or someone else who stumbles upon this)

But in any case, according to this comment in WindowsPlatformCompilerPreSetup.h line 137:

// UE4 uses a struct packing of 4 for Win32 and 8 for Win64, 
// and the default packing is 8 for Win32 and 16 for Win64

So if you are on Win64 the macro above should force 16 bit alignment

So I ended up fixing my issue, and it was related to using new and delete. I’m not sure if it is a memory alignment issue or a garbage collection issue, but Unreal doesn’t like you allocating things on the heap it seems, even when its done outside of a UCLASS or UObject. Moving my arrays to pre allocated fixed sizes on the stack fixed my issues.