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: 
Have you looked at the documentation of SWIG regarding their "cpointer.i" and "carray.i" libraries? They're found [here](http://www.swig.org/Doc1.3/Library.html#Library_nn4). That's how you have to manipulate things unless you want to create your own utility libraries to accompany the wrapped code. Here's the link to the Python handling of pointers with [SWIG](http://www.swig.org/Doc1.3/SWIG.html#SWIG_nn15).

Onto your question on getting it to recognize input versus output. They've got another section in the documentation [here](http://www.swig.org/Doc1.3/Arguments.html#Arguments_nn5), that describes exactly that. You lable things `OUTPUT` in the \*.i file. So in your case you'd write:

```
%inline{
extern bool GenerateStrips( const unsigned short* in_dices,
                            const unsigned short* in_numIndices,
                            PrimitiveGroup** OUTPUT,
                            unsigned short* numGroups,
                            bool validated );
%}

```

which gives you a function that returns both the bool and the `PrimitiveGroup*` array as a tuple.

Does that help?
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>