Skip to content

Commit 7e88851

Browse files
authored
Z3: dec ref on close (#104)
1 parent e8a65e1 commit 7e88851

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

ksmt-z3/src/main/kotlin/org/ksmt/solver/z3/KZ3Context.kt

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.microsoft.z3.decRefUnsafe
66
import com.microsoft.z3.incRefUnsafe
77
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap
88
import it.unimi.dsi.fastutil.longs.LongOpenHashSet
9+
import it.unimi.dsi.fastutil.longs.LongSet
910
import it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap
1011
import org.ksmt.KContext
1112
import org.ksmt.decl.KDecl
@@ -259,24 +260,29 @@ class KZ3Context(
259260
return cached
260261
}
261262

262-
/**
263-
* Note: we don't invoke decRef for each remaining expression/sort/...
264-
* because native context releases all memory on close.
265-
* */
266263
override fun close() {
267264
isClosed = true
268265

266+
z3Expressions.keys.decRefAll()
269267
expressions.clear()
270268
z3Expressions.clear()
271269

270+
tmpNativeObjects.decRefAll()
272271
tmpNativeObjects.clear()
273272

274-
sorts.clear()
275-
z3Sorts.clear()
276-
273+
z3Decls.keys.decRefAll()
277274
decls.clear()
278275
z3Decls.clear()
279276

277+
z3Sorts.keys.decRefAll()
278+
sorts.clear()
279+
z3Sorts.clear()
280+
280281
ctx.close()
281282
}
283+
284+
private fun LongSet.decRefAll() =
285+
longIterator().forEachRemaining {
286+
decRefUnsafe(nCtx, it)
287+
}
282288
}

0 commit comments

Comments
 (0)