Commit 9353e464 authored by Peter Eastman's avatar Peter Eastman
Browse files

Fixed errors in which destructors were executed in the wrong order

parent 0a850783
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* Biological Structures at Stanford, funded under the NIH Roadmap for * * Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. * * Medical Research, grant U54 GM072970. See https://simtk.org. *
* * * *
* Portions copyright (c) 2008 Stanford University and the Authors. * * Portions copyright (c) 2008-2012 Stanford University and the Authors. *
* Authors: Peter Eastman * * Authors: Peter Eastman *
* Contributors: * * Contributors: *
* * * *
...@@ -41,7 +41,8 @@ Kernel::Kernel(KernelImpl* impl) : impl(impl) { ...@@ -41,7 +41,8 @@ Kernel::Kernel(KernelImpl* impl) : impl(impl) {
} }
Kernel::Kernel(const Kernel& copy) : impl(copy.impl) { Kernel::Kernel(const Kernel& copy) : impl(copy.impl) {
impl->referenceCount++; if (impl)
impl->referenceCount++;
} }
Kernel::~Kernel() { Kernel::~Kernel() {
...@@ -53,8 +54,14 @@ Kernel::~Kernel() { ...@@ -53,8 +54,14 @@ Kernel::~Kernel() {
} }
Kernel& Kernel::operator=(const Kernel& copy) { Kernel& Kernel::operator=(const Kernel& copy) {
if (impl) {
impl->referenceCount--;
if (impl->referenceCount == 0)
delete impl;
}
impl = copy.impl; impl = copy.impl;
impl->referenceCount++; if (impl)
impl->referenceCount++;
return *this; return *this;
} }
......
...@@ -118,6 +118,14 @@ ContextImpl::ContextImpl(Context& owner, System& system, Integrator& integrator, ...@@ -118,6 +118,14 @@ ContextImpl::ContextImpl(Context& owner, System& system, Integrator& integrator,
ContextImpl::~ContextImpl() { ContextImpl::~ContextImpl() {
for (int i = 0; i < (int) forceImpls.size(); ++i) for (int i = 0; i < (int) forceImpls.size(); ++i)
delete forceImpls[i]; delete forceImpls[i];
// Make sure all kernels get properly deleted before contextDestroyed() is called.
initializeForcesKernel = Kernel();
kineticEnergyKernel = Kernel();
updateStateDataKernel = Kernel();
applyConstraintsKernel = Kernel();
virtualSitesKernel = Kernel();
platform->contextDestroyed(*this); platform->contextDestroyed(*this);
} }
......
...@@ -229,7 +229,6 @@ CudaContext::~CudaContext() { ...@@ -229,7 +229,6 @@ CudaContext::~CudaContext() {
string errorMessage = "Error deleting Context"; string errorMessage = "Error deleting Context";
if (contextIsValid) if (contextIsValid)
CHECK_RESULT(cuCtxDestroy(context)); CHECK_RESULT(cuCtxDestroy(context));
contextIsValid = false;
} }
void CudaContext::initialize() { void CudaContext::initialize() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment