Skip to content

Commit 3f74da9

Browse files
committed
update memory management
1 parent 9fc7ee3 commit 3f74da9

1 file changed

Lines changed: 49 additions & 57 deletions

File tree

src/gstype_python.i

Lines changed: 49 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -574,10 +574,9 @@ static bool convertObjectToBlob(PyObject* value, size_t* size, void** data) {
574574
}
575575
//Ignore null character
576576
*size = *size - 1;
577-
if (blobData) {
578-
*data = (void*) strdup(blobData);
579-
cleanString(blobData, alloc);
580-
}
577+
*data = malloc(*size);
578+
memcpy(*data, blobData, *size);
579+
cleanString(blobData, alloc);
581580
return true;
582581
}
583582
return false;
@@ -681,7 +680,6 @@ static bool convertToRowKeyFieldWithType(griddb::Field &field, PyObject* value,
681680
if (!SWIG_IsOK(res)) {
682681
return false;
683682
}
684-
685683
if (v) {
686684
field.value.asString = strdup(v);
687685
cleanString(v, alloc);
@@ -1171,10 +1169,10 @@ static bool convertToFieldWithType(GSRow *row, int column, PyObject* value, GSTy
11711169
$1 = PyList_Check($input) ? 1 : 0;
11721170
}
11731171

1174-
%typemap(freearg, fragment = "cleanString") (const GSColumnInfo* props, int propsCount) (int i) {
1172+
%typemap(freearg) (const GSColumnInfo* props, int propsCount) (int i) {
11751173
if ($1) {
11761174
for (int i = 0; i < $2; i++) {
1177-
cleanString($1[i].name, alloc$argnum[i]);
1175+
free((void*) $1[i].name);
11781176
}
11791177
free((void *) $1);
11801178
}
@@ -1186,9 +1184,9 @@ static bool convertToFieldWithType(GSRow *row, int column, PyObject* value, GSTy
11861184

11871185
%typemap(doc, name = "column_info_list") (const GSColumnInfo* props, int propsCount) "list[list[string, Type, TypeOption]]";
11881186
/**
1189-
* Typemaps for get_store() function
1187+
* Typemaps for StoreFactory::set_properties() function
11901188
*/
1191-
%typemap(in, fragment = "SWIG_AsCharPtrAndSize") (const GSPropertyEntry* props, int propsCount)
1189+
%typemap(in, fragment = "SWIG_AsCharPtrAndSize", fragment = "cleanString") (const GSPropertyEntry* props, int propsCount)
11921190
(int i, int j, Py_ssize_t si, PyObject* key, PyObject* val, size_t size = 0, int* alloc = 0, int res, char* v = 0) {
11931191
if (!PyDict_Check($input)) {
11941192
PyErr_SetString(PyExc_ValueError, "Expected a Dict");
@@ -1214,24 +1212,30 @@ static bool convertToFieldWithType(GSRow *row, int column, PyObject* value, GSTy
12141212
%variable_fail(res, "String", "name");
12151213
}
12161214

1217-
$1[i].name = v;
1215+
$1[i].name = strdup(v);
1216+
cleanString(v, alloc[j]);
12181217
res = SWIG_AsCharPtrAndSize(val, &v, &size, &alloc[j + 1]);
12191218
if (!SWIG_IsOK(res)) {
12201219
%variable_fail(res, "String", "value");
12211220
}
1222-
$1[i].value = v;
1221+
$1[i].value = strdup(v);
1222+
cleanString(v, alloc[j + 1]);
12231223
i++;
12241224
j+=2;
12251225
}
12261226
}
12271227
}
12281228

1229-
%typemap(freearg, fragment = "cleanString") (const GSPropertyEntry* props, int propsCount)
1229+
%typemap(freearg) (const GSPropertyEntry* props, int propsCount)
12301230
(int i = 0, int j = 0) {
12311231
if ($1) {
12321232
for (int i = 0; i < $2; i++) {
1233-
cleanString($1[i].name, alloc$argnum[j]);
1234-
cleanString($1[i].value, alloc$argnum[j + 1]);
1233+
if ($1[i].name) {
1234+
free((void*) $1[i].name);
1235+
}
1236+
if ($1[i].value) {
1237+
free((void*) $1[i].value);
1238+
}
12351239
j += 2;
12361240
}
12371241
free((void *) $1);
@@ -1350,16 +1354,16 @@ static bool convertToFieldWithType(GSRow *row, int column, PyObject* value, GSTy
13501354
GSRowKeyPredicateEntry *predicateEntry = &pList[i];
13511355
res = SWIG_AsCharPtrAndSize(key, &v, &size, &alloc[i]);
13521356
if (!SWIG_IsOK(res)) {
1353-
%variable_fail(res, "String", "containerName");
13541357
free((void*) pList);
1358+
%variable_fail(res, "String", "containerName");
13551359
}
13561360
predicateEntry->containerName = v;
13571361
//Get GSRowKeyPredicate pointer from RowKeyPredicate pyObject
13581362
int newmem = 0;
13591363
res = SWIG_ConvertPtrAndOwn(val, (void **) &vpredicate, $descriptor(std::shared_ptr<griddb::RowKeyPredicate>*), %convertptr_flags, &newmem);
13601364
if (!SWIG_IsOK(res)) {
1361-
%argument_fail(res, "$type", $symname, $argnum);
13621365
free((void*) pList);
1366+
%argument_fail(res, "$type", $symname, $argnum);
13631367
}
13641368
if (vpredicate) {
13651369
pPredicate = *%reinterpret_cast(vpredicate, std::shared_ptr<griddb::RowKeyPredicate>*);
@@ -1885,13 +1889,15 @@ static bool getRowFields(GSRow* row, int columnCount, GSType* typeList, bool tim
18851889
*/
18861890
%typemap(in, fragment = "convertToFieldWithType", fragment = "convertObjToStr")
18871891
(GSRow*** listRow, const int *listRowContainerCount, const char ** listContainerName, size_t containerCount) () {
1892+
$1 = NULL;
1893+
$2 = NULL;
1894+
$3 = NULL;
1895+
$4 = 0;
18881896
if (!PyDict_Check($input)) {
18891897
PyErr_SetString(PyExc_ValueError, "Expected a Dict");
18901898
SWIG_fail;
18911899
}
1892-
$1 = NULL;
1893-
$2 = NULL;
1894-
$3 = NULL;
1900+
18951901
$4 = (size_t)PyInt_AsLong(PyLong_FromSsize_t(PyDict_Size($input)));
18961902
griddb::Container* tmpContainer;
18971903
if ($4 > 0) {
@@ -1998,14 +2004,14 @@ static bool getRowFields(GSRow* row, int columnCount, GSType* typeList, bool tim
19982004

19992005
%typemap(freearg) (GSRow*** listRow, const int *listRowContainerCount, const char ** listContainerName, size_t containerCount) {
20002006
for (int i = 0; i < $4; i++) {
2001-
if ($1[i]) {
2007+
if ($1[i] != NULL) {
20022008
for (int j = 0; j < $2[i]; j++) {
20032009
gsCloseRow(&$1[i][j]);
20042010
}
2005-
delete $1[i];
2011+
delete [] $1[i];
20062012
}
20072013
}
2008-
if ($1) delete $1;
2014+
if ($1) delete [] $1;
20092015
if ($2) delete $2;
20102016
if ($3) delete $3;
20112017
}
@@ -2059,10 +2065,10 @@ static bool getRowFields(GSRow* row, int columnCount, GSType* typeList, bool tim
20592065
free ((void*) (*$4)[j]);
20602066
}
20612067
}
2062-
delete (*$4);
2068+
delete [] (*$4);
20632069
}
20642070
if (*$3) {
2065-
delete (*$3);
2071+
delete [] (*$3);
20662072
}
20672073
$result = dict;
20682074
}
@@ -2071,11 +2077,8 @@ static bool getRowFields(GSRow* row, int columnCount, GSType* typeList, bool tim
20712077
* Typemap for QueryAnalysisEntry.get()
20722078
*/
20732079
%typemap(in, numinputs = 0) (GSQueryAnalysisEntry* queryAnalysis) (GSQueryAnalysisEntry queryAnalysis1) {
2074-
$1 = (GSQueryAnalysisEntry*) malloc(sizeof(GSQueryAnalysisEntry));
2075-
if ($1 == NULL) {
2076-
PyErr_SetString(PyExc_ValueError, "Memory allocation error");
2077-
SWIG_fail;
2078-
}
2080+
queryAnalysis1 = GS_QUERY_ANALYSIS_ENTRY_INITIALIZER;
2081+
$1 = &queryAnalysis1;
20792082
}
20802083

20812084
%typemap(argout, fragment = "convertStrToObj") (GSQueryAnalysisEntry* queryAnalysis) () {
@@ -2103,7 +2106,6 @@ static bool getRowFields(GSRow* row, int columnCount, GSType* typeList, bool tim
21032106
if ($1->valueType) {
21042107
free((void*) $1->valueType);
21052108
}
2106-
free((void*) $1);
21072109
}
21082110
}
21092111

@@ -2135,21 +2137,14 @@ static bool getRowFields(GSRow* row, int columnCount, GSType* typeList, bool tim
21352137

21362138
%typemap(argout, numinputs = 0, fragment = "convertStrToObj") (char*** listName, int* num) {
21372139
$result = PyList_New(*$2);
2138-
if (*$2){
2140+
if (*$2 && *$1){
21392141
for (int i = 0; i < *$2; i++) {
21402142
PyList_SetItem($result, i, convertStrToObj((*$1)[i]));
2141-
}
2142-
}
2143-
return $result;
2144-
}
2145-
2146-
%typemap(freearg) (char*** listName, int* num) {
2147-
if (*$2){
2148-
for (int i = 0; i < *$2; i++) {
21492143
free((*$1)[i]);
21502144
}
21512145
free((void *) *$1);
21522146
}
2147+
return $result;
21532148
}
21542149

21552150
//Read only attribute Container::type
@@ -2242,18 +2237,18 @@ static bool getRowFields(GSRow* row, int columnCount, GSType* typeList, bool tim
22422237
}
22432238

22442239
%typemap(freearg) (GSRow** listRowdata, int rowCount) {
2245-
if ($1) {
2240+
if ($1 != NULL) {
22462241
for (int rowNum = 0; rowNum < $2; rowNum++) {
22472242
gsCloseRow(&$1[rowNum]);
22482243
}
2249-
delete $1;
2244+
delete [] $1;
22502245
}
22512246
}
22522247

22532248
%typemap(doc, name = "row_list") (GSRow** listRowdata, int rowCount) "list[list[object]]";
22542249

22552250
//attribute ContainerInfo::column_info_list
2256-
%typemap(in, fragment = "SWIG_AsCharPtrAndSize", fragment = "cleanString") (ColumnInfoList columnInfoList) (int* alloc){
2251+
%typemap(in, fragment = "SWIG_AsCharPtrAndSize", fragment = "cleanString") (ColumnInfoList columnInfoList) (int* alloc = NULL){
22572252

22582253
if (!PyList_Check($input)) {
22592254
PyErr_SetString(PyExc_ValueError, "Expected a List");
@@ -2281,7 +2276,7 @@ static bool getRowFields(GSRow* row, int columnCount, GSType* typeList, bool tim
22812276
PyErr_SetString(PyExc_ValueError, "Memory allocation for set column_info_list is error");
22822277
SWIG_fail;
22832278
}
2284-
memset($1.columnInfo, 0x0, size * sizeof(GSColumnInfo));
2279+
*($1.columnInfo) = GS_COLUMN_INFO_INITIALIZER;
22852280

22862281
PyObject* columInfoList;
22872282
int option;
@@ -2306,10 +2301,7 @@ static bool getRowFields(GSRow* row, int columnCount, GSType* typeList, bool tim
23062301
PyErr_SetString(PyExc_ValueError, "Can't convert field to string");
23072302
SWIG_fail;
23082303
}
2309-
if (v) {
2310-
$1.columnInfo[i].name = strdup(v);
2311-
cleanString(v, alloc[i]);
2312-
}
2304+
$1.columnInfo[i].name = v;
23132305
$1.columnInfo[i].type = PyLong_AsLong(PyList_GetItem(columInfoList, 1));
23142306
%#if GS_COMPATIBILITY_SUPPORT_3_5
23152307
if (sizeColumn == 3) {
@@ -2328,19 +2320,19 @@ static bool getRowFields(GSRow* row, int columnCount, GSType* typeList, bool tim
23282320
}
23292321

23302322
%typemap(freearg, fragment = "cleanString") (ColumnInfoList columnInfoList) {
2331-
size_t size = $1.size;
2332-
2333-
if (alloc$argnum) {
2334-
for (int i = 0; i < size; i++) {
2335-
cleanString($1.columnInfo[i].name, alloc$argnum[i]);
2336-
}
2337-
free(alloc$argnum);
2323+
size_t size = 0;
2324+
if ($1.size) {
2325+
size = $1.size;
23382326
}
2339-
2340-
if ($1.columnInfo) {
2327+
if ($1.columnInfo != NULL) {
2328+
if (alloc$argnum) {
2329+
for (int i = 0; i < size; i++) {
2330+
cleanString($1.columnInfo[i].name, alloc$argnum[i]);
2331+
}
2332+
free(alloc$argnum);
2333+
}
23412334
free((void *) $1.columnInfo);
23422335
}
2343-
23442336
}
23452337

23462338
%typemap(out, fragment = "convertStrToObj") (ColumnInfoList) {

0 commit comments

Comments
 (0)