Skip to content

Commit

Permalink
fix gc'ed callbacks in .NET propagator api (#6118)
Browse files Browse the repository at this point in the history
Co-authored-by: Maxwell Levatich <t-mlevatich@microsoft.com>
  • Loading branch information
mlevatich and Maxwell Levatich authored Jun 29, 2022
1 parent 7977876 commit 12e7b4c
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions src/api/dotnet/UserPropagator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class UserPropagator
/// <param name="phase">Set phase to -1 (false) or 1 (true) to override solver's phase</param>
/// </summary>
public delegate void DecideEh(ref Expr term, ref uint idx, ref int phase);

// access managed objects through a static array.
// thread safety is ignored for now.
static List<UserPropagator> propagators = new List<UserPropagator>();
Expand All @@ -76,6 +76,17 @@ public class UserPropagator
CreatedEh created_eh;
DecideEh decide_eh;

Native.Z3_push_eh push_eh;
Native.Z3_pop_eh pop_eh;
Native.Z3_fresh_eh fresh_eh;

Native.Z3_fixed_eh fixed_wrapper;
Native.Z3_final_eh final_wrapper;
Native.Z3_eq_eh eq_wrapper;
Native.Z3_eq_eh diseq_wrapper;
Native.Z3_decide_eh decide_wrapper;
Native.Z3_created_eh created_wrapper;

void Callback(Action fn, Z3_solver_callback cb)
{
this.callback = cb;
Expand All @@ -85,7 +96,7 @@ void Callback(Action fn, Z3_solver_callback cb)
}
catch
{
// TBD: add debug log or exception handler
// TBD: add debug log or exception handler
}
finally
{
Expand Down Expand Up @@ -172,7 +183,10 @@ public UserPropagator(Solver s)
propagators.Add(this);
solver = s;
ctx = solver.Context;
Native.Z3_solver_propagate_init(ctx.nCtx, solver.NativeObject, new IntPtr(id), _push, _pop, _fresh);
push_eh = _push;
pop_eh = _pop;
fresh_eh = _fresh;
Native.Z3_solver_propagate_init(ctx.nCtx, solver.NativeObject, new IntPtr(id), push_eh, pop_eh, fresh_eh);
}

/// <summary>
Expand Down Expand Up @@ -244,9 +258,10 @@ public FixedEh Fixed
{
set
{
this.fixed_wrapper = _fixed;
this.fixed_eh = value;
if (solver != null)
Native.Z3_solver_propagate_fixed(ctx.nCtx, solver.NativeObject, _fixed);
Native.Z3_solver_propagate_fixed(ctx.nCtx, solver.NativeObject, fixed_wrapper);
}
}

Expand All @@ -257,9 +272,10 @@ public Action Final
{
set
{
this.final_wrapper = _final;
this.final_eh = value;
if (solver != null)
Native.Z3_solver_propagate_final(ctx.nCtx, solver.NativeObject, _final);
Native.Z3_solver_propagate_final(ctx.nCtx, solver.NativeObject, final_wrapper);
}
}

Expand All @@ -270,9 +286,10 @@ public EqEh Eq
{
set
{
this.eq_wrapper = _eq;
this.eq_eh = value;
if (solver != null)
Native.Z3_solver_propagate_eq(ctx.nCtx, solver.NativeObject, _eq);
Native.Z3_solver_propagate_eq(ctx.nCtx, solver.NativeObject, eq_wrapper);
}
}

Expand All @@ -283,9 +300,10 @@ public EqEh Diseq
{
set
{
this.diseq_wrapper = _diseq;
this.diseq_eh = value;
if (solver != null)
Native.Z3_solver_propagate_diseq(ctx.nCtx, solver.NativeObject, _diseq);
Native.Z3_solver_propagate_diseq(ctx.nCtx, solver.NativeObject, diseq_wrapper);
}
}

Expand All @@ -296,9 +314,10 @@ public CreatedEh Created
{
set
{
this.created_wrapper = _created;
this.created_eh = value;
if (solver != null)
Native.Z3_solver_propagate_created(ctx.nCtx, solver.NativeObject, _created);
Native.Z3_solver_propagate_created(ctx.nCtx, solver.NativeObject, created_wrapper);
}
}

Expand All @@ -309,9 +328,10 @@ public DecideEh Decide
{
set
{
this.decide_wrapper = _decide;
this.decide_eh = value;
if (solver != null)
Native.Z3_solver_propagate_decide(ctx.nCtx, solver.NativeObject, _decide);
Native.Z3_solver_propagate_decide(ctx.nCtx, solver.NativeObject, decide_wrapper);
}
}

Expand Down

0 comments on commit 12e7b4c

Please sign in to comment.