Question: I'd like to use some existing C++ code, [NvTriStrip](http://developer.nvidia.com/object/nvtristrip_library.html), in a Python tool.

SWIG easily handles the functions with simple parameters, but the main function, `GenerateStrips`, is much more complicated.

What do I need to put in the SWIG interface file to indicate that `primGroups` is really an output parameter and that it must be cleaned up with `delete[]`?

```
///////////////////////////////////////////////////////////////////////////
// GenerateStrips()
//
// in_indices: input index list, the indices you would use to render
// in_numIndices: number of entries in in_indices
// primGroups: array of optimized/stripified PrimitiveGroups
// numGroups: number of groups returned
//
// Be sure to call delete[] on the returned primGroups to avoid leaking mem
//
bool GenerateStrips( const unsigned short* in_indices,
                     const unsigned int    in_numIndices,
                     PrimitiveGroup**      primGroups,
                     unsigned short*       numGroups,
                     bool                  validateEnabled = false );

```

FYI, here is the `PrimitiveGroup` declaration:

```
enum PrimType
{
    PT_LIST,
    PT_STRIP,
    PT_FAN
};

struct PrimitiveGroup
{
    PrimType type;
    unsigned int numIndices;
    unsigned short* indices;

    PrimitiveGroup() : type(PT_STRIP), numIndices(0), indices(NULL) {}
    ~PrimitiveGroup()
    {
        if(indices)
            delete[] indices;
        indices = NULL;
    }
};

```

Answer: 
It's actually so easy to make python bindings for things directly that I don't know why people bother with confusing wrapper stuff like SWIG.

Just use [Py\_BuildValue](http://docs.python.org/c-api/arg.html) once per element of the outer array, producing one tuple per row. Store those tuples in a C array. Then Call [PyList\_New](http://docs.python.org/c-api/list.html) and [PyList\_SetSlice](http://docs.python.org/c-api/list.html) to generate a list of tuples, and return the list pointer from your C function.
I don't know how to do it with SWIG, but you might want to consider moving to a more modern binding system like [Pyrex](http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/) or [Cython](http://cython.org/).

For example, Pyrex gives you access to C++ delete for cases like this. Here's an excerpt from the documentation:

> 
> **Disposal**
> 
> 
> The del statement can be applied to a pointer to a C++ struct
>  to deallocate it. This is equivalent to delete in C++.
> 
> 
> 
> ```
> cdef Shrubbery *big_sh
> big_sh = new Shrubbery(42.0)
> display_in_garden_show(big_sh)
> del big_sh
> 
> ```
> 
> 

<http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/version/Doc/Manual/using_with_c++.html>