wrap WolfSSLContext finalizer free to catch IllegalStateException if already freed

pull/37/head
Chris Conlon 2020-03-18 11:08:40 -06:00
parent 427c8782da
commit ccd0456a08
1 changed files with 11 additions and 4 deletions

View File

@ -40,7 +40,7 @@ import com.wolfssl.WolfSSLJNIException;
public class WolfSSLContext { public class WolfSSLContext {
/* internal native WOLFSSL_CTX pointer */ /* internal native WOLFSSL_CTX pointer */
private long sslCtxPtr; private long sslCtxPtr = 0;
/* user-registerd I/O callbacks, called by internal WolfSSLContext /* user-registerd I/O callbacks, called by internal WolfSSLContext
* I/O callback. This is done in order to pass references to * I/O callback. This is done in order to pass references to
@ -100,6 +100,9 @@ public class WolfSSLContext {
long getContextPtr() long getContextPtr()
{ {
if (this.active == false) {
return 0;
}
return sslCtxPtr; return sslCtxPtr;
} }
@ -566,10 +569,11 @@ public class WolfSSLContext {
throw new IllegalStateException("Object has been freed"); throw new IllegalStateException("Object has been freed");
/* free native resources */ /* free native resources */
freeContext(getContextPtr()); freeContext(this.sslCtxPtr);
/* free Java resources */ /* free Java resources */
this.active = false; this.active = false;
this.sslCtxPtr = 0;
} }
/** /**
@ -1695,8 +1699,11 @@ public class WolfSSLContext {
protected void finalize() throws Throwable protected void finalize() throws Throwable
{ {
if (this.active == true) { if (this.active == true) {
/* free resources, set state */ try {
this.free(); this.free();
} catch (IllegalStateException e) {
/* already freed */
}
this.active = false; this.active = false;
} }
super.finalize(); super.finalize();