diff --git a/amplpy/ampl.pyx b/amplpy/ampl.pyx index d390d21..405da34 100644 --- a/amplpy/ampl.pyx +++ b/amplpy/ampl.pyx @@ -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) diff --git a/amplpy/dataframe.pxi b/amplpy/dataframe.pxi index 18060d7..c2a0cc1 100644 --- a/amplpy/dataframe.pxi +++ b/amplpy/dataframe.pxi @@ -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): """ @@ -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): @@ -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): """ diff --git a/amplpy/entity.pxi b/amplpy/entity.pxi index 1dcd6f5..4f8eadc 100644 --- a/amplpy/entity.pxi +++ b/amplpy/entity.pxi @@ -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 @@ -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): diff --git a/amplpy/environment.pxi b/amplpy/environment.pxi index 830f6d0..09f5384 100644 --- a/amplpy/environment.pxi +++ b/amplpy/environment.pxi @@ -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): @@ -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): @@ -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(&(bin_name_c)) return bin_name # Aliases diff --git a/amplpy/util.pxi b/amplpy/util.pxi index 89e6e7d..3ef3f85 100644 --- a/amplpy/util.pxi +++ b/amplpy/util.pxi @@ -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): @@ -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)): @@ -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