Skip to content

Commit a428d47

Browse files
committed
T6717: memory management of alloced return values
The ctypes binding makes a copy of the string return value; the originating C memory allocation needs to be explicitly managed.
1 parent 39ec15b commit a428d47

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

src/vy_adapter.cpp

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include <iostream>
2222
#include <cstdint>
2323
#include <cassert>
24+
#include <algorithm>
25+
#include <list>
2426
#include <fcntl.h>
2527
#include <unistd.h>
2628

@@ -89,10 +91,22 @@ class stdout_redirect {
8991
bool redirecting;
9092
};
9193

92-
const char *out_data_copy(std::string msg)
94+
struct cstore_obj {
95+
Cstore *cstore;
96+
std::list<char *> output;
97+
};
98+
99+
static struct cstore_obj* create_handle() {
100+
struct cstore_obj *h = new cstore_obj;
101+
h->cstore = Cstore::createCstore(false);
102+
return h;
103+
};
104+
105+
const char *out_data_copy(std::string msg, cstore_obj *o)
93106
{
94107
size_t len = msg.length();
95108
char *out_data = (char *) malloc(len + 1);
109+
o->output.push_back(out_data);
96110
msg.copy(out_data, len);
97111
out_data[len] = '\0';
98112
return out_data;
@@ -109,36 +123,41 @@ static void *voidptr_of_uint(uint64_t v)
109123
return (void *)(uintptr_t)(v & ~1);
110124
}
111125

112-
static Cstore *cstore_of_handle(uint64_t handle)
126+
static cstore_obj *cstore_obj_of_handle(uint64_t handle)
113127
{
114-
return (Cstore *) voidptr_of_uint(handle);
128+
return (cstore_obj *) voidptr_of_uint(handle);
115129
}
116130

117131
uint64_t
118132
vy_cstore_init(void)
119133
{
120-
Cstore *handle = Cstore::createCstore(false);
134+
cstore_obj *handle = create_handle();
121135
return uint_of_voidptr(handle);
122136
}
123137

124138
void
125139
vy_cstore_free(uint64_t handle)
126140
{
127-
Cstore *h = cstore_of_handle(handle);
141+
cstore_obj *h = cstore_obj_of_handle(handle);
142+
for (char * x: h->output) {
143+
free(x);
144+
}
145+
delete h->cstore;
128146
delete h;
129147
}
130148

131149
int
132150
vy_in_session(uint64_t handle)
133151
{
134-
Cstore *h = cstore_of_handle(handle);
152+
Cstore *h = cstore_obj_of_handle(handle)->cstore;
135153
return h->inSession() ? 1 : 0;
136154
}
137155

138156
const char *
139157
vy_validate_path(uint64_t handle, const void** path_ptr, size_t len)
140158
{
141-
Cstore *cstore = cstore_of_handle(handle);
159+
cstore_obj *obj = cstore_obj_of_handle(handle);
160+
Cstore *cstore = obj->cstore;
142161
const char **path = (const char **) path_ptr;
143162
Cpath path_comps = Cpath(path, len);
144163
const char *out_data;
@@ -154,15 +173,16 @@ vy_validate_path(uint64_t handle, const void** path_ptr, size_t len)
154173
out_str.append(redirect.get_redirected_output());
155174
}
156175

157-
out_data = out_data_copy(out_str);
176+
out_data = out_data_copy(out_str, obj);
158177
out_stream = NULL;
159178
return out_data;
160179
}
161180

162181
const char *
163182
vy_set_path(uint64_t handle, const void** path_ptr, size_t len)
164183
{
165-
Cstore *cstore = cstore_of_handle(handle);
184+
cstore_obj *obj = cstore_obj_of_handle(handle);
185+
Cstore *cstore = obj->cstore;
166186
const char **path = (const char **) path_ptr;
167187
Cpath path_comps = Cpath(path, len);
168188
const char *out_data;
@@ -178,15 +198,16 @@ vy_set_path(uint64_t handle, const void** path_ptr, size_t len)
178198
out_str.append(redirect.get_redirected_output());
179199
}
180200

181-
out_data = out_data_copy(out_str);
201+
out_data = out_data_copy(out_str, obj);
182202
out_stream = NULL;
183203
return out_data;
184204
}
185205

186206
const char *
187207
vy_legacy_set_path(uint64_t handle, const void** path_ptr, size_t len)
188208
{
189-
Cstore *cstore = cstore_of_handle(handle);
209+
cstore_obj *obj = cstore_obj_of_handle(handle);
210+
Cstore *cstore = obj->cstore;
190211
const char **path = (const char **) path_ptr;
191212
Cpath path_comps = Cpath(path, len);
192213
const char *out_data;
@@ -210,15 +231,16 @@ vy_legacy_set_path(uint64_t handle, const void** path_ptr, size_t len)
210231
}
211232

212233
out:
213-
out_data = out_data_copy(out_str);
234+
out_data = out_data_copy(out_str, obj);
214235
out_stream = NULL;
215236
return out_data;
216237
}
217238

218239
const char *
219240
vy_delete_path(uint64_t handle, const void** path_ptr, size_t len)
220241
{
221-
Cstore *cstore = cstore_of_handle(handle);
242+
cstore_obj *obj = cstore_obj_of_handle(handle);
243+
Cstore *cstore = obj->cstore;
222244
const char **path = (const char **) path_ptr;
223245
Cpath path_comps = Cpath(path, len);
224246
const char *out_data;
@@ -234,7 +256,7 @@ vy_delete_path(uint64_t handle, const void** path_ptr, size_t len)
234256
out_str.append(redirect.get_redirected_output());
235257
}
236258

237-
out_data = out_data_copy(out_str);
259+
out_data = out_data_copy(out_str, obj);
238260
out_stream = NULL;
239261
return out_data;
240262
}

0 commit comments

Comments
 (0)