Skip to content

question about memory management #1779

Description

@guan-das-jounior

Hello,

I'm a little confused about memory management in Janet's C API. I'm trying to evaluate Janet expressions in a loop and print the results, but I'm running into issues with the string returned by janet_to_string().

Issue:

  • If I call janet_free(buff), the program crashes after the first iteration
  • If I don't call janet_free(buff), the program leaks memory on every iteration
#include "janet.h"
#include <stdio.h>

int main(int argc, const char *argv[]) {
    janet_init();

    JanetTable *env = janet_core_env(NULL);
    Janet out;

    while (1)
    {
        int status = janet_dostring(env, "(+ 1 1)", "main", &out);
        char *buff = janet_to_string(out);
        printf("result is %s\n", buff);
        janet_free(buff); // <--- CRASHES HERE
    }

    janet_deinit();
    return 0;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions