Skip to content

Commit

Permalink
fix(runtime): CreateCBaseObjectArray malloc
Browse files Browse the repository at this point in the history
  • Loading branch information
Timo972 committed Sep 2, 2023
1 parent 3ca2265 commit 1c7cc58
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
5 changes: 4 additions & 1 deletion entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ func getEntityData(e *C.struct_baseObject) (typ entity.BaseObjectType, ptr unsaf
}

func newBaseObjectArray[T entity.BaseObject](arr C.struct_array) []T {
return cutil.NewArrayFunc[*C.struct_baseObject, T](unsafe.Pointer(arr.ptr), int(arr.size), func(item *C.struct_baseObject) T {
entities := cutil.NewArrayFunc[*C.struct_baseObject, T](unsafe.Pointer(arr.ptr), int(arr.size), func(item *C.struct_baseObject) T {
v, err := factory.GetBaseObject[T](getEntityData(item))
C.free(unsafe.Pointer(item))
if err != nil {
altlog.Errorln(fmt.Sprintf("[Go] newBaseObjectArray: %s", err.Error()))
}
return v
})
// TODO: not sure, but i think we are leaking memory here: arr is allocated in cpp runtime using new CBaseObject*[arr.size];
// C.free(arr.ptr) crashes and logs double free :|
return entities
}

func Players() []entity.Player {
Expand Down
3 changes: 2 additions & 1 deletion runtime/src/GoRuntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ namespace Go {
CArray arr;
arr.size = objects.size();

// TODO: test if this works
// TODO: optimize so we dont have to call malloc in for loop
auto entities = new CBaseObject*[arr.size];

for (uint64_t i = 0; i < arr.size; i++) {
entities[i] = (CBaseObject*) malloc(sizeof(CBaseObject));
GetCBaseObject(objects[i], entities[i]);
}

Expand Down

0 comments on commit 1c7cc58

Please sign in to comment.