Skip to content

Commit 46ba274

Browse files
authored
fix: properly free Parser and Result (#6)
Fix #5.
1 parent 6e6b3f9 commit 46ba274

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

go-src/publiccode-parser-wrapper.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,14 @@ func FreeResult(result *C.struct_ParseResult) {
172172
result.WarningCount = 0
173173
}
174174

175+
C.free(unsafe.Pointer(result))
176+
177+
}
178+
179+
//export FreeParser
180+
func FreeParser(handle C.ParserHandle) {
181+
h := cgo.Handle(handle)
182+
h.Delete()
175183
}
176184

177185
func toGoParser(handle C.ParserHandle) (*publiccode.Parser, error) {

lib/libpubliccode-parser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ extern "C" {
101101
extern ParserHandle NewParser(_Bool disableNetwork, char* branch, char* baseURL);
102102
extern struct ParseResult* ParseString(ParserHandle handle, char* content);
103103
extern void FreeResult(struct ParseResult* result);
104+
extern void FreeParser(ParserHandle handle);
104105

105106
#ifdef __cplusplus
106107
}

lib/libpubliccode-parser.so

-13.5 KB
Binary file not shown.

src/Parser.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ public function __construct(?ParserConfig $config = null)
3131
}
3232
}
3333

34+
public function __destruct()
35+
{
36+
if ($this->handle != 0) {
37+
/** @phpstan-ignore-next-line */
38+
$this->ffi->FreeParser($this->handle);
39+
$this->handle = 0;
40+
}
41+
}
42+
3443
/**
3544
* Parse publiccode.yml content
3645
*
@@ -127,6 +136,7 @@ private function getFFI(): FFI
127136
ParserHandle NewParser(bool disableNetwork, const char* branch, const char* baseURL);
128137
ParseResult* ParseString(ParserHandle handle, const char* content);
129138
void FreeResult(ParseResult* result);
139+
void FreeParser(ParserHandle handle);
130140
CDEF;
131141

132142
try {
@@ -151,11 +161,11 @@ private function findLibrary(): string
151161
{
152162
$libraryName = 'libpubliccode-parser.so';
153163
$possiblePaths = [
154-
__DIR__ . '/',
155-
__DIR__ . '/../lib/',
156-
__DIR__ . '/../vendor/lib/',
157-
'/usr/local/lib/',
158-
'/usr/lib/',
164+
__DIR__ . '/',
165+
__DIR__ . '/../lib/',
166+
__DIR__ . '/../vendor/lib/',
167+
'/usr/local/lib/',
168+
'/usr/lib/',
159169
];
160170

161171
foreach ($possiblePaths as $path) {

0 commit comments

Comments
 (0)