Skip to content

Commit

Permalink
fix memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
jurgen-lentz committed Oct 18, 2024
1 parent 7f525f3 commit f4480c6
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
4 changes: 3 additions & 1 deletion amplpy/ampl.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,9 @@ cdef class AMPL:
"""
cdef campl.AMPL_VARIANT* v
PY_AMPL_CALL(campl.AMPL_GetValue(self._c_ampl, scalar_expression.encode('utf-8'), &v))
return to_py_variant(v)
py_variant = to_py_variant(v)
campl.AMPL_VariantFree(&v)
return py_variant

def set_data(self, data, set_name=None):
"""
Expand Down
1 change: 0 additions & 1 deletion amplpy/dataframe.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ cdef class Row(object):

def __getitem__(self, key):
cdef campl.AMPL_VARIANT* v
#campl.AMPL_DataFrameGetColumnIndex(self._df, key.encode('utf-8'), &column_index)
campl.AMPL_DataFrameElement(self._df, self._index, key, &v)
return to_py_variant(v)

Expand Down
11 changes: 11 additions & 0 deletions amplpy/entity.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ cdef class Entity(object):
entity.wrap_function = campl.AMPL_UNDEFINED
return entity

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

def to_string(self):
cdef char* output_c
campl.AMPL_EntityGetDeclaration(self._c_ampl, self._name.encode('utf-8'), &output_c)
Expand Down Expand Up @@ -179,6 +183,7 @@ cdef class Entity(object):
The string representation of the indexing sets for this entity or
an empty array if the entity is scalar.
"""
cdef size_t i
cdef size_t size
cdef char** sets
cdef list pylist = []
Expand All @@ -188,6 +193,9 @@ cdef class Entity(object):
pylist.append(sets[i].decode('utf-8'))
else:
pylist.append(None)
campl.AMPL_StringFree(&sets[i])
free(sets)

return pylist

def xref(self):
Expand Down Expand Up @@ -297,15 +305,18 @@ cdef class Entity(object):
df = data
df_c = df.get_ptr()
campl.AMPL_EntitySetValues(self._c_ampl, _name_c, df_c)
campl.AMPL_StringFree(&_name_c)
elif isinstance(data, dict):
df = DataFrame.from_dict(data)
df_c = df.get_ptr()
campl.AMPL_EntitySetValues(self._c_ampl, _name_c, df_c)
campl.AMPL_StringFree(&_name_c)
else:
if pd is not None and isinstance(data, (pd.DataFrame, pd.Series)):
df = DataFrame.from_pandas(data, indexarity=self.indexarity())
df_c = df.get_ptr()
campl.AMPL_EntitySetValues(self._c_ampl, _name_c, df_c)
campl.AMPL_StringFree(&_name_c)
return
raise TypeError(f"Unexpected data type: {type(data)}.")

Expand Down
9 changes: 7 additions & 2 deletions amplpy/parameter.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,20 @@ cdef class Parameter(Entity):
cdef campl.AMPL_VARIANT* v
campl.AMPL_InstanceGetName(self._c_ampl, self._name.encode('utf-8'), tuple_c, &expression)
campl.AMPL_GetValue(self._c_ampl, expression, &v)
return to_py_variant(v)
campl.AMPL_StringFree(&expression)
py_variant = to_py_variant(v)
campl.AMPL_VariantFree(&v)
return py_variant

def value(self):
"""
Get the value of this parameter. Valid only for non-indexed parameters.
"""
cdef campl.AMPL_VARIANT* v
campl.AMPL_GetValue(self._c_ampl, self._name.encode('utf-8'), &v)
return to_py_variant(v)
py_variant = to_py_variant(v)
campl.AMPL_VariantFree(&v)
return py_variant

def set(self, *args):
"""
Expand Down
4 changes: 3 additions & 1 deletion amplpy/util.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,13 @@ cdef void setValuesPyDict(campl.AMPL* ampl, str name, dicts):
for i, (key, value) in enumerate(dicts.items()):
key_c = to_c_tuple(key)
campl.AMPL_ParameterInstanceSetStringValue(ampl, name.encode('utf-8'), key_c, value.encode('utf-8'))
campl.AMPL_TupleFree(&key_c)
elif has_numbers and not has_strings:
for i, (key, value) in enumerate(dicts.items()):
if isinstance(value, int) or isinstance(value, float):
key_c = to_c_tuple(key)
campl.AMPL_ParameterInstanceSetNumericValue(ampl, name.encode('utf-8'), key_c, value)
campl.AMPL_ParameterInstanceSetNumericValue(ampl, name.encode('utf-8'), key_c, value)
campl.AMPL_TupleFree(&key_c)
else:
raise ValueError("Unexpected value type")
else:
Expand Down

0 comments on commit f4480c6

Please sign in to comment.