SWIG Go interface question. How to pass null pointers

192 views
Skip to first unread message

Jeroen Dirks

unread,
Mar 25, 2011, 3:07:53 PM3/25/11
to golang-nuts
First of all I want to thank Ian Lance Taylor for all the work on SWIG
and Go.

I am currently experimenting with using SWIG to create a Go wrapper
for wxWidgets library (similar to wxPython).

There is one thing that is not quite working for me (but maybe I am
missing something.)

Say I have a C++ class:

class wxFrame : public wxWindow
{
public:
wxFrame( wxWindow *parent );
};

In this case I may want to call this with a null pointer for the
parent.
The swig wrapper however is:

func NewWxFrame(arg1 WxWindow) WxFrame {
return _swig_wrap_new_wxFrame(arg1.Swigcptr())
}

If from Go I call
frame := NewWxFrame(nil)
I get a nil pointer derefence error.

Is there a way to create a 'null value' WxWindow to pass in or could
the wrapper

have generated code like this:
func NewWxFrame(arg1 WxWindow) WxFrame {
if arg1 == nil {
return _swig_wrap_new_wxFrame(0)
} else {
return _swig_wrap_new_wxFrame(arg1.Swigcptr())
}
}

Jeroen Dirks

unread,
Mar 25, 2011, 3:16:16 PM3/25/11
to golang-nuts
Hmm, I found this works:

frame := NewWxFrame( SwigcptrWxWindow(0) )

But it does not look very pretty.

Ian Lance Taylor

unread,
Mar 25, 2011, 4:05:42 PM3/25/11
to Jeroen Dirks, golang-nuts
Jeroen Dirks <jeroen...@oracle.com> writes:

> have generated code like this:
> func NewWxFrame(arg1 WxWindow) WxFrame {
> if arg1 == nil {
> return _swig_wrap_new_wxFrame(0)
> } else {
> return _swig_wrap_new_wxFrame(arg1.Swigcptr())
> }
> }

Yeah, that might not be a bad idea. My general thinking was that in
general you need to get valid values from the C++ side. But nil/NULL is
sort of a special case.

Ian

Reply all
Reply to author
Forward
0 new messages