Skip to content

Commit

Permalink
fix more leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
jurgen-lentz committed Oct 24, 2024
1 parent 9d0cb06 commit 5f9c367
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
1 change: 1 addition & 0 deletions amplpy/ampl.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ cdef class AMPL:

PY_AMPL_CALL(campl.AMPL_GetOption(self._c_ampl, name.encode('utf-8'), &exists, &value_c))
value = value_c.decode('utf-8')
campl.AMPL_StringFree(&value_c)
if exists:
try:
return int(value)
Expand Down
12 changes: 9 additions & 3 deletions amplpy/dataframe.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,9 @@ cdef class DataFrame(object):
if len(value) == 1 and isinstance(value[0], (tuple, list)):
value = value[0]
assert len(value) == self._get_num_cols()
cdef campl.AMPL_TUPLE* py_tuple = to_c_tuple(tuple(value))
campl.AMPL_DataFrameAddRow(self._c_df, py_tuple)
cdef campl.AMPL_TUPLE* tuple_c = to_c_tuple(tuple(value))
campl.AMPL_DataFrameAddRow(self._c_df, tuple_c)
campl.AMPL_TupleFree(&tuple_c)

def _add_column(self, header, values=None):
"""
Expand Down Expand Up @@ -341,6 +342,7 @@ cdef class DataFrame(object):
cdef size_t index
cdef campl.AMPL_TUPLE* tuple = to_c_tuple(key)
campl.AMPL_DataFrameGetRowIndex(self._c_df, tuple, &index)
campl.AMPL_TupleFree(&tuple)
return Row.create(self._c_df, index)

def _get_row_by_index(self, index):
Expand All @@ -365,7 +367,11 @@ cdef class DataFrame(object):
cdef size_t size
cdef char** headers
campl.AMPL_DataFrameGetHeaders(self._c_df, &size, &headers)
return tuple(headers[i].decode('utf-8') for i in range(size))
headers_py = tuple(str(headers[i].decode('utf-8')) for i in range(size))
for i in range(size):
campl.AMPL_StringFree(&headers[i])
free(headers)
return headers_py

def _set_values(self, values):
"""
Expand Down
9 changes: 7 additions & 2 deletions amplpy/entity.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ cdef class Entity(object):
return entity

#def __dealloc__(self):
# if self._index is not NULL:
# campl.AMPL_TupleFree(&self._index)
# if self._name is not NULL:
# campl.AMPL_StringFree(&self._name)
#if self._index is not NULL:
# campl.AMPL_TupleFree(&self._index)

def to_string(self):
cdef char* output_c
Expand Down Expand Up @@ -111,9 +113,12 @@ cdef class Entity(object):
campl.AMPL_EntityGetTuples(self._c_ampl, self._name, &indices_c, &size)
for i in range(size):
if campl.AMPL_TupleCompare(index_c, indices_c[i]) == 0:
for j in range(size):
campl.AMPL_TupleFree(&indices_c[j])
free(indices_c)
return create_entity(self.wrap_function, self._c_ampl, self._name, index_c)
free(indices_c)
campl.AMPL_TupleFree(&index_c)
return None

def instances(self):
Expand Down
3 changes: 0 additions & 3 deletions amplpy/environment.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ cdef class Environment(object):
cdef const char* to_string_c
campl.AMPL_EnvironmentToString(self._c_env, &to_string_c)
to_string = str(to_string_c.decode('utf-8'))
#campl.AMPL_StringFree(&to_string_c)
return to_string

def set_bin_dir(self, binary_directory):
Expand All @@ -92,7 +91,6 @@ cdef class Environment(object):
cdef const char* bin_dir_c
campl.AMPL_EnvironmentGetBinaryDirectory(self._c_env, &bin_dir_c)
bin_dir = str(bin_dir_c.decode('utf-8'))
#campl.AMPL_StringFree(&bin_dir_c)
return bin_dir

def set_bin_name(self, binary_name):
Expand All @@ -111,7 +109,6 @@ cdef class Environment(object):
cdef const char* bin_name_c
campl.AMPL_EnvironmentGetBinaryName(self._c_env, &bin_name_c)
bin_name = str(bin_name_c.decode('utf-8'))
#campl.AMPL_StringFree(&(<char*>bin_name_c))
return bin_name

# Aliases
Expand Down
6 changes: 5 additions & 1 deletion amplpy/util.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ cdef void setValues(campl.AMPL* ampl, char* name, campl.AMPL_TUPLE* index, value
campl.AMPL_SetInstanceSetValuesTuples(ampl, name, index, values_c, size)

for i in range(size):
free(values_c[i])
campl.AMPL_TupleFree(&values_c[i])
free(values_c)

cdef to_py_variant(campl.AMPL_VARIANT* variant):
Expand Down Expand Up @@ -103,6 +103,7 @@ cdef to_py_tuple(campl.AMPL_TUPLE* tuple_c):
return tuple(pylist)

cdef campl.AMPL_TUPLE* to_c_tuple(py_tuple):
cdef size_t i
cdef campl.AMPL_TUPLE* tuple_c

if not isinstance(py_tuple, (tuple, list)):
Expand All @@ -116,6 +117,9 @@ cdef campl.AMPL_TUPLE* to_c_tuple(py_tuple):

campl.AMPL_TupleCreate(&tuple_c, size, variants)

for i in range(size):
campl.AMPL_VariantFree(&variants[i])

free(variants)
return tuple_c

Expand Down

0 comments on commit 5f9c367

Please sign in to comment.