Go: Fix memory leak and simplify in type mapping of std::vector.
// Map std::vector<tinyspline::real> to Go slice (do not free $1).
%typemap(out) std::vector<tinyspline::real> * {
- $result = {&$1->front(), $1->size(), $1->size()};
+ size_t size = $1->size() * sizeof(tinyspline::real);
+ tinyspline::real *data = (tinyspline::real *)malloc(size);
+ if (!data) throw std::bad_alloc();
+ memcpy(data, &$1->front(), size);
+ $result = {data, $1->size(), $1->size()};
+}
+%typemap(newfree) std::vector<tinyspline::real> * {
+ delete $1;
}
// Map Go slice to std::vector<tinyspline::real>.
%typemap(in) std::vector<tinyspline::real> * {
tinyspline::real *data = (tinyspline::real *)$input.array;
- intgo size = $input.len;
- $1 = new std::vector<tinyspline::real>();
- $1->reserve(size);
- for (int i = 0; i < size; i++) {
- $1->push_back((tinyspline::real)data[i]);
- }
+ $1 = new std::vector<tinyspline::real>(data, data + $input.len);
}
%typemap(freearg) std::vector<tinyspline::real> * {
delete $1;
