Skip to content

Commit 07d1b30

Browse files
Prerak SinghPrerak Singh
authored andcommitted
bug fix
1 parent d99f263 commit 07d1b30

File tree

1 file changed

+5
-18
lines changed

1 file changed

+5
-18
lines changed

pydatastructs/linear_data_structures/_backend/cpp/arrays/OneDimensionalArray.hpp

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,7 @@ typedef struct {
1616
} OneDimensionalArray;
1717

1818
static void OneDimensionalArray_dealloc(OneDimensionalArray *self) {
19-
if (self->_data) {
20-
for (size_t i = 0; i < self->_size; i++) {
21-
Py_XDECREF(self->_data[i]);
22-
}
23-
std::free(self->_data);
24-
self->_data = nullptr;
25-
}
26-
Py_XDECREF(self->_dtype);
19+
std::free(self->_data);
2720
Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self));
2821
}
2922

@@ -112,7 +105,7 @@ static PyObject* OneDimensionalArray___new__(PyTypeObject* type, PyObject *args,
112105
return NULL;
113106
}
114107
self->_size = size;
115-
self->_data = reinterpret_cast<PyObject**>(std::calloc(size, sizeof(PyObject*)));
108+
self->_data = reinterpret_cast<PyObject**>(std::malloc(size * sizeof(PyObject*)));
116109
if (!self->_data) {
117110
Py_DECREF(args0);
118111
Py_DECREF(args1);
@@ -171,32 +164,26 @@ static PyObject* OneDimensionalArray___new__(PyTypeObject* type, PyObject *args,
171164
if (!init) {
172165
PyErr_Clear();
173166
init = Py_None;
174-
Py_INCREF(init);
175167
}
176168
}
177169
}
178170
if (!init) {
179171
init = Py_None;
180-
Py_INCREF(init);
181172
}
182173
if (init != Py_None && raise_exception_if_dtype_mismatch(init, self->_dtype)) {
183-
Py_DECREF(init);
184174
Py_DECREF(args0);
185175
return NULL;
186176
}
187-
self->_data = reinterpret_cast<PyObject**>(std::calloc(self->_size, sizeof(PyObject*)));
177+
self->_data = reinterpret_cast<PyObject**>(std::malloc(self->_size * sizeof(PyObject*)));
188178
if (!self->_data) {
189-
Py_DECREF(init);
190179
Py_DECREF(args0);
191180
PyErr_NoMemory();
192181
return NULL;
193182
}
194183

195184
for (size_t i = 0; i < self->_size; i++) {
196-
Py_INCREF(init);
197185
self->_data[i] = init;
198186
}
199-
Py_DECREF(init);
200187

201188
} else if (PyList_Check(args0) || PyTuple_Check(args0)) {
202189
Py_ssize_t size_ssize = PyObject_Length(args0);
@@ -205,7 +192,7 @@ static PyObject* OneDimensionalArray___new__(PyTypeObject* type, PyObject *args,
205192
return NULL;
206193
}
207194
self->_size = (size_t)size_ssize;
208-
self->_data = reinterpret_cast<PyObject**>(std::calloc(self->_size, sizeof(PyObject*)));
195+
self->_data = reinterpret_cast<PyObject**>(std::malloc(self->_size * sizeof(PyObject*)));
209196
if (!self->_data) {
210197
Py_DECREF(args0);
211198
PyErr_NoMemory();
@@ -320,7 +307,7 @@ static PyObject* OneDimensionalArray_fill(OneDimensionalArray *self, PyObject *a
320307
return NULL;
321308
}
322309

323-
PyObject* value = PyTuple_GetItem(args, 0); // Borrowed reference
310+
PyObject* value = PyTuple_GetItem(args, 0);
324311
if (!value) return NULL;
325312

326313
if (value != Py_None && raise_exception_if_dtype_mismatch(value, self->_dtype)) {

0 commit comments

Comments
 (0)