From 4b8a7626bff72b4c3c087649933910e6adf55f37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Mon, 18 Jul 2022 02:22:07 +0200 Subject: Avoid leak in Prolog interface. PL_new_atom(_wchars) creates an atom with a reference count of one, which is never decreased, and the atom is thus never garbage collected. --- c/pl.h | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) (limited to 'c/pl.h') diff --git a/c/pl.h b/c/pl.h index 06efe13..276f79a 100644 --- a/c/pl.h +++ b/c/pl.h @@ -23,10 +23,10 @@ inline int PlPut(const term_t t, const int x) { return PL_put_integer(t, x); } inline int PlPut(const term_t t, const long x) { return PL_put_integer(t, x); } inline int PlPut(const term_t t, const long long x) { return PL_put_integer(t, x); } inline int PlPut(const term_t t, const atom_t x) { return PL_put_atom(t, x); } -inline int PlPut(const term_t t, char* const x) { return PL_put_atom(t, PL_new_atom(x)); } -inline int PlPut(const term_t t, const char* const x) { return PL_put_atom(t, PL_new_atom(x)); } -inline int PlPut(const term_t t, wchar_t* const x) { return PL_put_atom(t, PL_new_atom_wchars(-1, x)); } -inline int PlPut(const term_t t, const wchar_t* const x) { return PL_put_atom(t, PL_new_atom_wchars(-1, x)); } +inline int PlPut(const term_t t, char* const x) { return PL_put_atom_chars(t, x); } +inline int PlPut(const term_t t, const char* const x) { return PL_put_atom_chars(t, x); } +inline int PlPut(const term_t t, wchar_t* const x) { return PL_unify_wchars(t, PL_ATOM, -1, x); } +inline int PlPut(const term_t t, const wchar_t* const x) { return PL_unify_wchars(t, PL_ATOM, -1, x); } inline int PlPut(const term_t, int* const) { return -1; } inline int PlPut(const term_t, long* const) { return -1; } inline int PlPut(const term_t, long long* const) { return -1; } @@ -45,14 +45,7 @@ inline int PlGet(const term_t t, long* const x) { return PL_get_long(t, x); } inline int PlGet(const term_t t, long long* const x) { return PL_get_int64(t, x); } inline int PlGet(const term_t t, atom_t* const x) { return PL_get_atom(t, x); } inline int PlGet(const term_t t, char** const x) { return PL_get_atom_chars(t, x); } -inline int PlGet(const term_t t, wchar_t** const x) -{ - atom_t a; - size_t len; - if (!PL_get_atom(t, &a)) return 0; - *x = (wchar_t*)PL_atom_wchars(a, &len); - return *x != NULL; -} +inline int PlGet(const term_t t, wchar_t** const x) { size_t len; return PL_get_wchars(t, &len, x, CVT_ATOM); } /* Helper templates for Plx, Pl. */ template -- cgit v1.2.3