Skip to content

Garbage collection doesn't free values released during garbage collection (via native pointers) #2253

@dethredic

Description

@dethredic

The following is some sample code which I hope helps demonstrates the problem.

typedef struct {
  jerry_value_t val;
} FooBar;

static void free_ctx_cb(void *native_p) {
  FooBar *ctx = (FooBar *) native_p;
  jerry_release_value(ctx->val);
  free(ctx);
}

static const jerry_object_native_info_t native_info = {
  .free_cb = free_ctx_cb,
};

jerry_value_t my_func(const jerry_value_t function_obj_p,
                      const jerry_value_t this_val,
                      const jerry_value_t argv[],
                      const jerry_length_t argc) {
  jerry_value_t obj = jerry_create_object();
  FooBar *ctx = malloc(sizeof(FooBar));
  ctx->val = jerry_acquire_value(this_val);
  jerry_set_object_native_pointer(obj, (void *) ctx, &native_info);
  return obj;
}

To finish my program I call jerry_cleanup(). This triggers garbage collection, which causes free_ctx_cb() to be run. free_ctx_cb() decrements the ref count on the variable, but the garbage collection alg doesn't seem to be aware of this new deref, so I get the following: jerry_assert_fail(assertion="JERRY_CONTEXT (jmem_heap_allocated_size) == 0")

To work around this I have to call jerry_gc(); jerry_cleanup();, but as you can imagine that only works for 1 layer. There could hypothetically be multiple more layers.

Am I using the APIs incorrectly, or should there be another loop in the garbage collection algorithm to pick up any new objects with a ref count of 0?

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestRequested featurememory managementRelated to memory management or garbage collection

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions