diff options
author | John Ankarström <john@ankarstrom.se> | 2022-07-19 12:16:15 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2022-07-19 12:16:15 +0200 |
commit | 34c32802122ff80e79d850d44cbc84624d9a5840 (patch) | |
tree | b03f55581dda8ad8560ca8e0fd2f2eb341392345 /c/pl.h | |
parent | 2a7f4bcd4c04d0e48e580c4b11a94174bc5b09ae (diff) | |
download | EpisodeBrowser-34c32802122ff80e79d850d44cbc84624d9a5840.tar.gz |
Fix Prolog memory leaks.
Apparently foreign frames ARE needed when calling Prolog from C. The
official documentation is very terse and could make this clearer.
To summarize, whenever a term is created (e.g., PL_new_term_refs), its
reference count is increased by one. It is garbage-collected when its
reference count hits zero. But the reference count is never decreased
unless (a) control returns to Prolog after executing a foreign
predicate -- which does not happen in my application -- or (b) the
foreign frame in which the term was created is closed.
In other words, terms must be created within a foreign frame. This is
achieved by initializing a Frame object before creating the term and
destroying it once the term has served its purpose.
The destructor for Frame does not DISCARD the frame, only CLOSE it.
The former would also invalidate all data bound by the terms, which is
usually undesirable.
Diffstat (limited to 'c/pl.h')
-rw-r--r-- | c/pl.h | 12 |
1 files changed, 12 insertions, 0 deletions
@@ -7,6 +7,17 @@ int PL_get_tchars(term_t t, TCHAR** pTsz, int iFlags); int Plx(const char* szMod, const char* szPred); +struct Frame +{ + Frame(); + ~Frame(); + void Close(); + void Discard(); + void Rewind(); +private: + fid_t m_f; +}; + struct Query { Query(module_t ctx, predicate_t p, term_t t0); @@ -65,6 +76,7 @@ int Countv(const int i, T, R... rest) { return Countv(i+1, rest...); } template <typename ...T> int Plx(const char* const szMod, const char* const szPred, T... args) { + Frame f; const int iArity = Countv(0, args...); const term_t t = PL_new_term_refs(iArity); if (!PlPutv(t, args...)) return 0; |