3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2026-08-09 23:42:21 +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
AlgebraicNum.cs
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
ArrayExpr.cs
ArraySort.cs
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
BitVecNum.cs
BitVecSort.cs
BoolExpr.cs
BoolSort.cs
CharSort.cs
cmake_install_gac.cmake.in
cmake_uninstall_gac.cmake.in
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
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
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
FiniteDomainNum.cs
FiniteDomainSort.cs
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
FPNum.cs fix dotnet build errors 2026-02-18 20:56:02 -08:00
FPRMExpr.cs
FPRMNum.cs
FPRMSort.cs
FPSort.cs
FuncDecl.cs
FuncInterp.cs remove DecRefQueue, use Z3_enable_concurrent_dec_ref (#6332) 2022-09-11 18:59:00 -07:00
Global.cs
Goal.cs remove DecRefQueue, use Z3_enable_concurrent_dec_ref (#6332) 2022-09-11 18:59:00 -07:00
IntExpr.cs
IntNum.cs
IntSort.cs
IntSymbol.cs
Lambda.cs
ListSort.cs
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
Microsoft.Z3.snk
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
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
Probe.cs remove DecRefQueue, use Z3_enable_concurrent_dec_ref (#6332) 2022-09-11 18:59:00 -07:00
Quantifier.cs
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
RealSort.cs
ReExpr.cs
RelationSort.cs
ReSort.cs
SeqExpr.cs
SeqSort.cs
SetSort.cs
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
Statistics.cs remove DecRefQueue, use Z3_enable_concurrent_dec_ref (#6332) 2022-09-11 18:59:00 -07:00
Status.cs
StringSymbol.cs
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
UninterpretedSort.cs
UserPropagator.cs add on_binding callbacks across APIs 2025-08-07 12:55:50 -07:00
Version.cs
Z3Exception.cs
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