Advertisement

Memory errors when accessing property

Started by December 30, 2024 10:01 PM
1 comment, last by allpurposemat 5 days, 15 hours ago

Hi! I'm adding AngelScript to my simple engine, which has a class like so:


class Transform3D
{
  glm::vec3 get_pos() const { clear_dirty(); return pos; }
private:
  glm::vec3 pos;
}

class Node3D : Node
{
private:
  Transform3D transform;

public:
  Transform3D& get_transform();
}

auto transform_to_str(const Transform3D* trans) -> std::string
{
  // just show the pos for now
  return glm::to_string(trans->get_pos());
}

I registered it like so:

int r = pEngine->RegisterObjectType("Transform3D", 0, asOBJ_REF | asOBJ_NOCOUNT);
assert(r >= 0);
r = pEngine->RegisterObjectMethod("Transform3D", "vec3 get_pos() const property", asMETHOD(Transform3D, get_pos), asCALL_THISCALL);
assert(r >= 0);

r = pEngine->RegisterObjectMethod("Transform3D", "string opImplConv() const", asFUNCTION(transform_to_str), asCALL_CDECL_OBJLAST);
assert(r >= 0);

// now register Node3D
r = pEngine->RegisterObjectType("Node3D", 0, asOBJ_REF | asOBJ_NOCOUNT);
assert(r >= 0);
r = pEngine->RegisterObjectMethod("Node3D", "Transform3D@ get_xform() property", asMETHOD(Node3D, transform), asCALL_THISCALL);
assert(r >= 0);

In my script, I have this:

auto @xform = @self.xform;
log("xform: " + xform);
log("xform.pos: " + xform.pos);

And it prints:

[SCRIPT] xform: vec3(0.000000, 0.000000, 5.000000)
[SCRIPT] xform.pos: vec3(0.250000, -22.000000, 18.000000)

When I check with a debugger, the call to Transform3D::get_pos that happens when accessing it via .pos is made on an invalid/garbage this pointer, which ends up printing garbage values.
What am I doing wrong? Why is it different?

Aha, I figured out after much testing that the issue was caused by my registration of the glm::vec3 type.

I noticed that accessing the X and Y property of any vector from AngelScript yielded the value of X, and accessing Z yielded a strange number that seems to change when X changes. I reviewed the documentation on registering value types, and found that I missed the part about registering all the type traits. I changed my code to this:

r = pEngine->RegisterObjectType(pName, sizeof(VecType), asOBJ_VALUE | asGetTypeTraits<VecType>() | asOBJ_APP_CLASS_ALLFLOATS | asOBJ_APP_CLASS_UNION);
assert(r >= 0);

It works great now!

Advertisement