Skip to content

Commit

Permalink
fix shutdown order for new dtor vm/shred logic
Browse files Browse the repository at this point in the history
  • Loading branch information
gewang committed Oct 29, 2023
1 parent 5d723ae commit 95eaa79
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
7 changes: 4 additions & 3 deletions src/core/chuck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1036,13 +1036,14 @@ t_CKBOOL ChucK::shutdown()
// ensure we have a carrier
if( m_carrier != NULL )
{
// release VM (itself an Object)
CK_SAFE_RELEASE( m_carrier->vm );
// initiate VM shutdown but don't delete VM yet
m_carrier->vm->shutdown();
// delete compiler, including type system (m_carrier->env)
// do this after VM cleanup, as shreds could have objects with type refs
CK_SAFE_DELETE( m_carrier->compiler );
// verify the env pointer is NULL
assert( m_carrier->env == NULL );
// release VM (itself an Object)
CK_SAFE_RELEASE( m_carrier->vm );
}

// clear flag
Expand Down
1 change: 1 addition & 0 deletions src/core/chuck_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ typedef struct { SAMPLE re ; SAMPLE im ; } t_CKCOMPLEX_SAMPLE;
#define CK_SAFE_REF_ASSIGN(lhs,rhs) do { Chuck_VM_Object * temp = (lhs); (lhs) = (rhs); CK_SAFE_ADD_REF(lhs); CK_SAFE_RELEASE(temp); } while(0)
#define CK_SAFE_FREE(x) do { if(x){ free(x); (x) = NULL; } } while(0)
#define CK_SAFE_UNLOCK_DELETE(x) do { if(x){ (x)->unlock(); delete (x); (x) = NULL; } } while(0)
#define CK_SAFE_UNLOCK_RELEASE(x) do { if(x){ (x)->unlock(); (x)->release(); (x) = NULL; } } while(0)

// max + min
#define ck_max(x,y) ( (x) >= (y) ? (x) : (y) )
Expand Down
19 changes: 10 additions & 9 deletions src/core/chuck_oo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,15 +273,16 @@ Chuck_Object::~Chuck_Object()
// get this object's type
Chuck_Type * type = this->type_ref;

// vm ref from origin vm | 1.5.1.8
Chuck_VM * vm = origin_vm;
// no origin vm? get it from type
if( !vm ) vm = type ? type->vm() : NULL;

// shred ref from origin shred | 1.5.1.8
Chuck_VM_Shred * shred = origin_shred;
// if no origin shred, get it from vm | can only be non-NULL during VM compute cycle
if( !shred ) shred = vm ? vm->get_current_shred() : NULL;
// shred
Chuck_VM_Shred * shred = NULL;
// a way to check if we are in run state or post-run (shutdown) state | 1.5.1.8
// NOTE important to look at carrier->vm directly, and not a copy of vm pointer
Chuck_VM * vm = type && type->env() && type->env()->carrier() ? type->env()->carrier()->vm : NULL;
// if we are in normal run state
if( vm ) {
// update shred ref from origin shred | 1.5.1.8
shred = origin_shred ? origin_shred : vm->get_current_shred();
}

// call destructors, from latest descended child to oldest parent | 1.3.0.0
while( type != NULL )
Expand Down
5 changes: 3 additions & 2 deletions src/core/chuck_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -704,8 +704,9 @@ struct Chuck_Env : public Chuck_VM_Object
public:
// REFACTOR-2017: carrier and accessors
void set_carrier( Chuck_Carrier * carrier ) { m_carrier = carrier; }
Chuck_VM * vm() { return m_carrier ? m_carrier->vm : NULL; }
Chuck_Compiler * compiler() { return m_carrier ? m_carrier->compiler : NULL; }
Chuck_Carrier * carrier() const { return m_carrier; }
Chuck_VM * vm() const { return m_carrier ? m_carrier->vm : NULL; }
Chuck_Compiler * compiler() const { return m_carrier ? m_carrier->compiler : NULL; }

protected:
Chuck_Carrier * m_carrier;
Expand Down

0 comments on commit 95eaa79

Please sign in to comment.