Fix some more mem errors in 2024.09.1 (#7899)

* fixes

* do not leak MolCatalogParams

* do not leak points on align failures

* give python ownership of pointers returned in getFingerprintsHelper

* clean up ScaffoldNetwork ptr if createNetworkHelper fails

* manage FF ptrs during construction

* wire in ownsBondInvGenerator in getMorganGenerator

* manage weights in rdMolAlign CalcRMS

* fix ownership of matches list/tuple in generateRmsdTransMatchPyTuple

* manage stream in createForwardSupplier during construction

* drop redundant Point3D allocations in GetUSRDistributionsFromPoints

* fix signed comparison mismatch
This commit is contained in:
Ricardo Rodriguez
2024-10-10 10:08:50 -04:00
committed by GitHub
parent 315939f7ae
commit 0164ac7fae
16 changed files with 103 additions and 82 deletions

View File

@@ -23,6 +23,24 @@ namespace python = boost::python;
namespace RDNumeric {
namespace Alignments {
class PointVectManager {
public:
PointVectManager() = default;
PointVectManager(const PointVectManager &rhs) = delete;
PointVectManager &operator=(const PointVectManager &rhs) = delete;
~PointVectManager() {
for (auto &i : m_ptr) {
delete i;
}
}
RDGeom::Point3DConstPtrVect &getVect() { return m_ptr; }
private:
RDGeom::Point3DConstPtrVect m_ptr;
};
void GetPointsFromPythonSequence(python::object &points,
RDGeom::Point3DConstPtrVect &pts) {
PyObject *pyObj = points.ptr();
@@ -89,19 +107,18 @@ PyObject *AlignPointPairs(python::object refPoints, python::object probePoints,
// 2. A list of doubles of size N
// first deal with situation where we have Numerics arrays
RDGeom::Point3DConstPtrVect refPts, probePts;
PointVectManager refPts, probePts;
GetPointsFromPythonSequence(refPoints, refPts);
GetPointsFromPythonSequence(probePoints, probePts);
GetPointsFromPythonSequence(refPoints, refPts.getVect());
GetPointsFromPythonSequence(probePoints, probePts.getVect());
unsigned int npt = refPts.size();
if (npt != probePts.size()) {
unsigned int npt = refPts.getVect().size();
if (npt != probePts.getVect().size()) {
throw_value_error("Mis-match in number of points");
}
PyObject *weightsObj = weights.ptr();
RDNumeric::DoubleVector *wtsVec;
wtsVec = nullptr;
std::unique_ptr<RDNumeric::DoubleVector> wtsVec;
double *data;
if (PyArray_Check(weightsObj)) {
auto *wtsMat = reinterpret_cast<PyArrayObject *>(weightsObj);
@@ -110,7 +127,7 @@ PyObject *AlignPointPairs(python::object refPoints, python::object probePoints,
throw_value_error(
"Number of weights supplied do not match the number of points");
}
wtsVec = new RDNumeric::DoubleVector(nwts);
wtsVec.reset(new RDNumeric::DoubleVector(nwts));
data = reinterpret_cast<double *>(PyArray_DATA(wtsMat));
for (unsigned int i = 0; i < nwts; i++) {
wtsVec->setVal(i, data[i]);
@@ -124,7 +141,7 @@ PyObject *AlignPointPairs(python::object refPoints, python::object probePoints,
throw_value_error(
"Number of weights supplied do not match the number of points");
}
wtsVec = new RDNumeric::DoubleVector(nwts);
wtsVec.reset(new RDNumeric::DoubleVector(nwts));
for (unsigned int i = 0; i < npt; i++) {
wtsVec->setVal(i, wts[i]);
}
@@ -132,8 +149,8 @@ PyObject *AlignPointPairs(python::object refPoints, python::object probePoints,
}
RDGeom::Transform3D trans;
double ssd =
AlignPoints(refPts, probePts, trans, wtsVec, reflect, maxIterations);
double ssd = AlignPoints(refPts.getVect(), probePts.getVect(), trans,
wtsVec.get(), reflect, maxIterations);
npy_intp dims[2];
dims[0] = 4;
@@ -148,12 +165,6 @@ PyObject *AlignPointPairs(python::object refPoints, python::object probePoints,
}
}
delete wtsVec;
for (unsigned int i = 0; i < npt; ++i) {
delete probePts[i];
delete refPts[i];
}
PyObject *resTup = PyTuple_New(2);
PyObject *ssdItem = PyFloat_FromDouble(ssd);
PyTuple_SetItem(resTup, 0, ssdItem);