@@ -611,9 +611,15 @@ void close_keepers( Universe* U, lua_State* L)
611611 }
612612 // free the keeper bookkeeping structure
613613 {
614- void * allocUD ;
615- lua_Alloc allocF = lua_getallocf ( L , & allocUD );
616- allocF ( allocUD , U -> keepers , sizeof ( Keepers ) + (nbKeepers - 1 ) * sizeof ( Keeper ), 0 );
614+ // don't hijack the state allocator when running LuaJIT because it looks like LuaJIT does not expect it and might invalidate the memory unexpectedly
615+ #if LUAJIT_FLAVOR == 0
616+ {
617+ AllocatorDefinition * const allocD = & U -> protected_allocator .definition ;
618+ allocD -> allocF ( allocUD , U -> keepers , sizeof ( Keepers ) + (nbKeepers - 1 ) * sizeof ( Keeper ), 0 );
619+ }
620+ #else // LUAJIT_FLAVOR
621+ free (U -> keepers );
622+ #endif // LUAJIT_FLAVOR
617623 U -> keepers = NULL ;
618624 }
619625 }
@@ -634,8 +640,6 @@ void init_keepers( Universe* U, lua_State* L)
634640{
635641 int i ;
636642 int nb_keepers ;
637- void * allocUD ;
638- lua_Alloc allocF = lua_getallocf ( L , & allocUD );
639643
640644 STACK_CHECK ( L , 0 ); // L K
641645 lua_getfield ( L , 1 , "nb_keepers" ); // nb_keepers
@@ -649,7 +653,15 @@ void init_keepers( Universe* U, lua_State* L)
649653 // Keepers contains an array of 1 s_Keeper, adjust for the actual number of keeper states
650654 {
651655 size_t const bytes = sizeof ( Keepers ) + (nb_keepers - 1 ) * sizeof ( Keeper );
652- U -> keepers = (Keepers * ) allocF ( allocUD , NULL , 0 , bytes );
656+ // don't hijack the state allocator when running LuaJIT because it looks like LuaJIT does not expect it and might invalidate the memory unexpectedly
657+ #if LUAJIT_FLAVOR == 0
658+ {
659+ AllocatorDefinition * const allocD = & U -> protected_allocator .definition ;
660+ U -> keepers = (Keepers * ) allocD -> allocF ( allocUD , NULL , 0 , bytes );
661+ }
662+ #else // LUAJIT_FLAVOR
663+ U -> keepers = (Keepers * )malloc (bytes );
664+ #endif // LUAJIT_FLAVOR
653665 if ( U -> keepers == NULL )
654666 {
655667 (void ) luaL_error ( L , "init_keepers() failed while creating keeper array; out of memory" );
0 commit comments