3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-04-14 21:08:46 +00:00

Bugfix for concurrent garbage collection in Java API.

Relates to #205 and #245
This commit is contained in:
Christoph M. Wintersteiger 2015-10-14 12:42:27 +01:00
parent 2d2ec38541
commit 6263252bf5
2 changed files with 34 additions and 19 deletions

View file

@ -18,6 +18,7 @@ Notes:
package com.microsoft.z3; package com.microsoft.z3;
import java.util.Map; import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import com.microsoft.z3.enumerations.Z3_ast_print_mode; import com.microsoft.z3.enumerations.Z3_ast_print_mode;
@ -3761,29 +3762,42 @@ public class Context extends IDisposable
return m_Optimize_DRQ; return m_Optimize_DRQ;
} }
protected long m_refCount = 0; protected AtomicInteger m_refCount = new AtomicInteger(0);
/** /**
* Finalizer. * Finalizer.
* @throws Throwable
**/ **/
protected void finalize() protected void finalize() throws Throwable
{ {
dispose(); try {
dispose();
if (m_refCount == 0)
{ if (m_refCount.get() == 0)
try
{ {
Native.delContext(m_ctx); try
} catch (Z3Exception e) {
Native.delContext(m_ctx);
} catch (Z3Exception e)
{
// OK.
}
m_ctx = 0;
System.out.println("> disposed OK.");
}
else if (m_refCount.get() < 0)
System.out.println("XXX negative ref count");
else
{ {
// OK. System.out.println("XXX context not disposed");
} }
m_ctx = 0; }
} catch (Throwable t) {
/* throw t;
else }
CMW: re-queue the finalizer? */ finally {
super.finalize();
}
} }
/** /**
@ -3791,6 +3805,7 @@ public class Context extends IDisposable
**/ **/
public void dispose() public void dispose()
{ {
System.out.println("> context disposing.");
m_AST_DRQ.clear(this); m_AST_DRQ.clear(this);
m_ASTMap_DRQ.clear(this); m_ASTMap_DRQ.clear(this);
m_ASTVector_DRQ.clear(this); m_ASTVector_DRQ.clear(this);

View file

@ -43,8 +43,8 @@ public class Z3Object extends IDisposable
} }
if (m_ctx != null) if (m_ctx != null)
{ {
m_ctx.m_refCount--; m_ctx.m_refCount.decrementAndGet();
m_ctx = null; m_ctx = null;
} }
} }
@ -54,13 +54,13 @@ public class Z3Object extends IDisposable
Z3Object(Context ctx) Z3Object(Context ctx)
{ {
ctx.m_refCount++; ctx.m_refCount.incrementAndGet();
m_ctx = ctx; m_ctx = ctx;
} }
Z3Object(Context ctx, long obj) Z3Object(Context ctx, long obj)
{ {
ctx.m_refCount++; ctx.m_refCount.incrementAndGet();
m_ctx = ctx; m_ctx = ctx;
incRef(obj); incRef(obj);
m_n_obj = obj; m_n_obj = obj;