ccrma / chuck

ChucK Music Programming Language
http://chuck.stanford.edu/
GNU General Public License v2.0
809 stars 129 forks source link

API->object->create(...) not calling default constructor #447

Open AndrewAday opened 3 months ago

AndrewAday commented 3 months ago

For chugins, the API->object->create() method does not invoke the constructor of the type it creates. E.g.

// Foo object that holds arbitrary data
struct Foo { char buff[256]; }

// default ck constructor of the object we're creating in a function
CK_DLL_CTOR(foo_ctor)
{
    // allocates memory to hold struct data
    OBJ_MEMBER_UINT(SELF, foo_data_ptr_offset) = (t_CKUINT) new Foo;   
}

// function that creates and returns a Foo
CK_DLL_SFUN(create_foo)
{
    Chuck_Object* foo_obj = API->object->create(SHRED, fooType, false);
    Foo* foo = (Foo*)OBJ_MEMBER_UINT(foo_obj, foo_data_ptr_offset);
    // ideally, `foo_ctor` has been called and memory has been allocated
    // but from my logging, foo_ctor is actually never called
    strcpy(foo->buff, "this will segfault");
    RETURN->v_obj = foo_obj;
}
AndrewAday commented 3 months ago

In some cases the current behavior is beneficial actually.