diff --git a/go-src/publiccode-parser-wrapper.go b/go-src/publiccode-parser-wrapper.go index 9c12997..949269f 100644 --- a/go-src/publiccode-parser-wrapper.go +++ b/go-src/publiccode-parser-wrapper.go @@ -172,6 +172,14 @@ func FreeResult(result *C.struct_ParseResult) { result.WarningCount = 0 } + C.free(unsafe.Pointer(result)) + +} + +//export FreeParser +func FreeParser(handle C.ParserHandle) { + h := cgo.Handle(handle) + h.Delete() } func toGoParser(handle C.ParserHandle) (*publiccode.Parser, error) { diff --git a/lib/libpubliccode-parser.h b/lib/libpubliccode-parser.h index fc499f5..d648b04 100644 --- a/lib/libpubliccode-parser.h +++ b/lib/libpubliccode-parser.h @@ -101,6 +101,7 @@ extern "C" { extern ParserHandle NewParser(_Bool disableNetwork, char* branch, char* baseURL); extern struct ParseResult* ParseString(ParserHandle handle, char* content); extern void FreeResult(struct ParseResult* result); +extern void FreeParser(ParserHandle handle); #ifdef __cplusplus } diff --git a/lib/libpubliccode-parser.so b/lib/libpubliccode-parser.so index 7d854e5..b7a6337 100644 Binary files a/lib/libpubliccode-parser.so and b/lib/libpubliccode-parser.so differ diff --git a/src/Parser.php b/src/Parser.php index 56c5883..31b3636 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -31,6 +31,15 @@ public function __construct(?ParserConfig $config = null) } } + public function __destruct() + { + if ($this->handle != 0) { + /** @phpstan-ignore-next-line */ + $this->ffi->FreeParser($this->handle); + $this->handle = 0; + } + } + /** * Parse publiccode.yml content * @@ -127,6 +136,7 @@ private function getFFI(): FFI ParserHandle NewParser(bool disableNetwork, const char* branch, const char* baseURL); ParseResult* ParseString(ParserHandle handle, const char* content); void FreeResult(ParseResult* result); + void FreeParser(ParserHandle handle); CDEF; try { @@ -151,11 +161,11 @@ private function findLibrary(): string { $libraryName = 'libpubliccode-parser.so'; $possiblePaths = [ - __DIR__ . '/', - __DIR__ . '/../lib/', - __DIR__ . '/../vendor/lib/', - '/usr/local/lib/', - '/usr/lib/', + __DIR__ . '/', + __DIR__ . '/../lib/', + __DIR__ . '/../vendor/lib/', + '/usr/local/lib/', + '/usr/lib/', ]; foreach ($possiblePaths as $path) {