Elad Weiss
Elad Weiss

Reputation: 4002

SWIG - memory leak when wrapping std::pair of strings

I am trying to wrap a std::map using SWIG to python, and it works nicely, except that it leaks memory (my code below).

Apparently SWIG automatically frees the returned object's (the Tuple) memory , but does not free the String allocated inside it. I read that I can use explicit deallocation using %typemap(newfree), but no idea how to implement.

%typemap(out) std::pair<std::string, double> {
    $result = PyTuple_Pack(2, PyUnicode_FromString($1.first.c_str()), 
                              PyFloat_FromDouble($1.second));
};

%typemap(newfree) std::pair<std::string, double> {
     // What to do here?
     // delete[] $1.first.c_str() clearly not the way to go...
}

Upvotes: 1

Views: 440

Answers (1)

Mark Tolonen
Mark Tolonen

Reputation: 177674

SWIG has pre-defined typemaps for pair and string, so you won't need to write them yourself:

test.i

%module test

// Add appropriate includes to wrapper
%{
#include <utility>
#include <string>
%}

// Use SWIG's pre-defined templates for pair and string
%include <std_pair.i>
%include <std_string.i>

// Instantiate code for your specific template.
%template(sdPair) std::pair<std::string,double>;

// Declare and wrap a function for demonstration.
%inline %{
    std::pair<std::string,double> get()
    {
        return std::pair<std::string,double>("abcdefg",1.5);
    }
%}

Demo:

>>> import test
>>> p = test.sdPair('abc',3.5)
>>> p.first
'abc'
>>> p.second
3.5
>>> test.get()
('abcdefg', 1.5)

Upvotes: 2

Related Questions