3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2026-07-18 13:05:46 +00:00
z3/src/api/dotnet
Copilot ba7b12c18c
Fix .NET API memory leak: NativeContext finalizer, delegate lifetime, GC memory pressure (#10090)
Creating and disposing `Context` instances causes unbounded native
memory growth (~12 GB for 100k contexts) because `NativeContext` had no
finalizer — if `Dispose()` was never called, the native Z3 context
leaked permanently. Additionally, both `Context` and `NativeContext` had
delegate lifetime and thread-safety issues in their disposal paths.

## `NativeContext.cs`

- **Add missing finalizer** `~NativeContext() { Dispose(); }` — the root
cause of permanent leaks when callers don't explicitly dispose
- **Atomic disposal** via `Interlocked.Exchange(ref m_ctx, IntPtr.Zero)`
— prevents double-free when `Dispose()` is called concurrently (e.g.
user code + finalizer race)
- **Delegate lifetime** — capture `errHandler` locally +
`GC.KeepAlive(errHandler)` after `Z3_del_context`; the GC could
otherwise collect the error handler callback before the native
destructor finishes
- **Remove dead code** — `GC.SuppressFinalize` in `InitContext()` and
`GC.ReRegisterForFinalize` in `Dispose()` were both no-ops (no finalizer
existed); the latter would have caused infinite finalization with the
new finalizer
- **GC memory pressure** — `GC.AddMemoryPressure(8MB)` on init /
`GC.RemoveMemoryPressure(8MB)` on dispose, guarded by
`m_memPressureAdded` flag, so the GC schedules finalizers promptly when
contexts accumulate

## `Context.cs`

- **Thread-safe disposal** — capture `ctx` and `errHandler` inside the
existing `lock(this)` block; previously both were read outside the lock,
allowing two concurrent callers to both capture the same non-zero `ctx`
and double-free it
- **Delegate lifetime** — same `errHandler` + `GC.KeepAlive` pattern as
`NativeContext`
- **`GC.SuppressFinalize` placement** — moved inside the `if (m_ctx !=
IntPtr.Zero)` block, before cleanup, per .NET best practice
- **GC memory pressure** — same add/remove pattern, conditioned on
`!is_external` via `m_memPressureAdded` flag

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
2026-07-11 21:15:36 -07:00
..
Properties build errors/warnings 2019-03-16 16:52:18 -07:00
AlgebraicNum.cs remove dependencies on contracts 2018-10-20 10:24:36 -07:00
ApplyResult.cs remove DecRefQueue, use Z3_enable_concurrent_dec_ref (#6332) 2022-09-11 18:59:00 -07:00
ArithExpr.cs Dispose of intermediate Z3Objects created in dotnet api. (#5901) 2022-03-17 08:08:05 -07:00
ArithSort.cs remove dependencies on contracts 2018-10-20 10:24:36 -07:00
ArrayExpr.cs add multi-argument select for C# 2019-03-17 11:36:29 -07:00
ArraySort.cs remove dependencies on contracts 2018-10-20 10:24:36 -07:00
AST.cs remove DecRefQueue, use Z3_enable_concurrent_dec_ref (#6332) 2022-09-11 18:59:00 -07:00
ASTMap.cs remove DecRefQueue, use Z3_enable_concurrent_dec_ref (#6332) 2022-09-11 18:59:00 -07:00
ASTVector.cs remove DecRefQueue, use Z3_enable_concurrent_dec_ref (#6332) 2022-09-11 18:59:00 -07:00
BitVecExpr.cs remove dependencies on contracts 2018-10-20 10:24:36 -07:00
BitVecNum.cs enable binary string access to unsigned numerals over API #4568 2020-07-07 18:58:42 -07:00
BitVecSort.cs remove dependencies on contracts 2018-10-20 10:24:36 -07:00
BoolExpr.cs remove dependencies on contracts 2018-10-20 10:24:36 -07:00
BoolSort.cs remove dependencies on contracts 2018-10-20 10:24:36 -07:00
CharSort.cs add char sort to .net 2021-07-13 19:43:12 +02:00
cmake_install_gac.cmake.in rename additional build options #2709 2019-11-18 21:32:35 -08:00
cmake_uninstall_gac.cmake.in rename additional build options #2709 2019-11-18 21:32:35 -08:00
CMakeLists.txt Fix ARM64 nightly build: dotnet NuGet packaging broken under Ninja/cmake (#9589) 2026-05-21 11:18:03 -07:00
Constructor.cs #7003 2023-11-19 09:59:44 -08:00
ConstructorList.cs remove dependencies on contracts 2018-10-20 10:24:36 -07:00
Context.cs Fix .NET API memory leak: NativeContext finalizer, delegate lifetime, GC memory pressure (#10090) 2026-07-11 21:15:36 -07:00
DatatypeExpr.cs remove dependencies on contracts 2018-10-20 10:24:36 -07:00
DatatypeSort.cs Dispose of intermediate Z3Objects created in dotnet api. (#5901) 2022-03-17 08:08:05 -07:00
EnumSort.cs Dispose of intermediate Z3Objects created in dotnet api. (#5901) 2022-03-17 08:08:05 -07:00
Expr.cs Add missing API methods across language bindings (#8150) 2026-02-18 20:57:08 -08:00
FiniteDomainExpr.cs remove dependencies on contracts 2018-10-20 10:24:36 -07:00
FiniteDomainNum.cs remove dependencies on contracts 2018-10-20 10:24:36 -07:00
FiniteDomainSort.cs remove dependencies on contracts 2018-10-20 10:24:36 -07:00
FiniteSetSort.cs Add finite set API support for C# and Java bindings (#8003) 2025-11-04 15:57:55 -08:00
Fixedpoint.cs remove DecRefQueue, use Z3_enable_concurrent_dec_ref (#6332) 2022-09-11 18:59:00 -07:00
FPExpr.cs Return significand bits correctly (dotnet API). Fixes #4584 2020-07-22 16:57:33 +01:00
FPNum.cs fix dotnet build errors 2026-02-18 20:56:02 -08:00
FPRMExpr.cs remove dependencies on contracts 2018-10-20 10:24:36 -07:00
FPRMNum.cs remove dependencies on contracts 2018-10-20 10:24:36 -07:00
FPRMSort.cs remove dependencies on contracts 2018-10-20 10:24:36 -07:00
FPSort.cs remove dependencies on contracts 2018-10-20 10:24:36 -07:00
FuncDecl.cs prepare release notes 2018-10-28 17:42:16 -05:00
FuncInterp.cs remove DecRefQueue, use Z3_enable_concurrent_dec_ref (#6332) 2022-09-11 18:59:00 -07:00
Global.cs remove dependencies on contracts 2018-10-20 10:24:36 -07:00
Goal.cs remove DecRefQueue, use Z3_enable_concurrent_dec_ref (#6332) 2022-09-11 18:59:00 -07:00
IntExpr.cs remove dependencies on contracts 2018-10-20 10:24:36 -07:00
IntNum.cs remove dependencies on contracts 2018-10-20 10:24:36 -07:00
IntSort.cs remove dependencies on contracts 2018-10-20 10:24:36 -07:00
IntSymbol.cs remove dependencies on contracts 2018-10-20 10:24:36 -07:00
Lambda.cs fix remaining incorrect uses of new BoolExpr related to #2125 2019-02-07 12:28:17 -08:00
ListSort.cs remove dependencies on contracts 2018-10-20 10:24:36 -07:00
Log.cs fix dotnet build errors 2026-02-18 20:56:02 -08:00
Microsoft.Z3.csproj.in dotnet: force PlatformTarget=AnyCPU to fix arm64 host load failure (#9868) 2026-06-16 09:52:04 -06:00
Microsoft.Z3.props Fix ARM64 nightly build: dotnet NuGet packaging broken under Ninja/cmake (#9589) 2026-05-21 11:18:03 -07:00
Microsoft.Z3.Sharp.pc.in this is still used 2019-01-20 11:25:34 -08:00
Microsoft.Z3.snk Added/improved facilities for strong name signing of the .NET assembly. 2016-07-28 18:07:34 +01:00
Microsoft.Z3.targets Fix ARM64 nightly build: dotnet NuGet packaging broken under Ninja/cmake (#9589) 2026-05-21 11:18:03 -07:00
Microsoft.Z3.targets.in Remove dependency on TargetPlatform macro 2019-03-14 15:46:03 -07:00
Model.cs remove DecRefQueue, use Z3_enable_concurrent_dec_ref (#6332) 2022-09-11 18:59:00 -07:00
NativeContext.cs Fix .NET API memory leak: NativeContext finalizer, delegate lifetime, GC memory pressure (#10090) 2026-07-11 21:15:36 -07:00
NativeFuncInterp.cs Fix null ref on access of Entry[] contents (#5947) 2022-04-06 05:37:51 +02:00
NativeModel.cs Clean up build warnings (#5884) 2022-03-07 12:55:30 -08:00
NativeSolver.cs Add missing C# API functions for solver introspection and congruence closure (#8126) 2026-02-18 20:57:06 -08:00
OnClause.cs build fixes 2023-07-18 19:14:45 -07:00
Optimize.cs Add missing API methods across language bindings (discussion #8701) 2026-02-20 04:24:22 +00:00
ParamDescrs.cs remove DecRefQueue, use Z3_enable_concurrent_dec_ref (#6332) 2022-09-11 18:59:00 -07:00
Params.cs remove DecRefQueue, use Z3_enable_concurrent_dec_ref (#6332) 2022-09-11 18:59:00 -07:00
Pattern.cs remove dependencies on contracts 2018-10-20 10:24:36 -07:00
Probe.cs remove DecRefQueue, use Z3_enable_concurrent_dec_ref (#6332) 2022-09-11 18:59:00 -07:00
Quantifier.cs fix #2460 2019-08-03 08:06:38 -07:00
RatNum.cs Dispose of intermediate Z3Objects created in dotnet api. (#5901) 2022-03-17 08:08:05 -07:00
RCFNum.cs fix build 2026-02-18 20:57:14 -08:00
README.md update path reference to readme 2023-12-05 13:47:05 -08:00
RealExpr.cs remove dependencies on contracts 2018-10-20 10:24:36 -07:00
RealSort.cs remove dependencies on contracts 2018-10-20 10:24:36 -07:00
ReExpr.cs remove dependencies on contracts 2018-10-20 10:24:36 -07:00
RelationSort.cs remove dependencies on contracts 2018-10-20 10:24:36 -07:00
ReSort.cs remove dependencies on contracts 2018-10-20 10:24:36 -07:00
SeqExpr.cs add multi-argument select for C# 2019-03-17 11:36:29 -07:00
SeqSort.cs charsort 2021-07-13 19:50:41 +02:00
SetSort.cs remove dependencies on contracts 2018-10-20 10:24:36 -07:00
Simplifiers.cs add simplifier to java API 2023-02-02 19:06:26 -08:00
Solver.cs code-simplifier: fix JavaDoc formatting in Context.java and extract ternary in Solver.cs 2026-02-25 21:49:23 +00:00
Sort.cs remove dependencies on contracts 2018-10-20 10:24:36 -07:00
Statistics.cs remove DecRefQueue, use Z3_enable_concurrent_dec_ref (#6332) 2022-09-11 18:59:00 -07:00
Status.cs remove dependencies on contracts 2018-10-20 10:24:36 -07:00
StringSymbol.cs remove dependencies on contracts 2018-10-20 10:24:36 -07:00
Symbol.cs Typo Fixes (#6803) 2023-07-09 11:56:10 -07:00
Tactic.cs remove DecRefQueue, use Z3_enable_concurrent_dec_ref (#6332) 2022-09-11 18:59:00 -07:00
TupleSort.cs remove dependencies on contracts 2018-10-20 10:24:36 -07:00
UninterpretedSort.cs remove dependencies on contracts 2018-10-20 10:24:36 -07:00
UserPropagator.cs add on_binding callbacks across APIs 2025-08-07 12:55:50 -07:00
Version.cs remove dependencies on contracts 2018-10-20 10:24:36 -07:00
Z3Exception.cs remove dependencies on contracts 2018-10-20 10:24:36 -07:00
Z3Object.cs Dotnet Api: Fix infinite finalization of Context (#6361) 2022-09-22 13:25:17 -05:00

Z3 Nuget Package

For more information see the Z3 github page