Sign in to follow this  
BrianEmpson

Passing a null pointer

Recommended Posts

Hello!

I have a function I am trying to pass a null (0) pointer to:

[code]
virtual IGUIWindow * addWindow (const core::rect< s32 > &rectangle, bool modal=false, const wchar_t *text=0, IGUIElement *parent=0, s32 id=-1)=0
[/code]

I registered it as:

[code]
r = engine->RegisterObjectMethod("GEnvironment", "GWindow@ addWindow(const recti &in,bool &in,const stringw &in,GElement@ &in,s32 &in)", asMETHOD(irr::gui::IGUIEnvironment, addWindow), asCALL_THISCALL); assert(r >= 0);
[/code]

But I get a segfault when I try to create it in the script like this:

[code]
@conWin = GUIEnv.addWindow(windowPos,false,consoleTitle,null,ID_GUI_CONSOLE_WINDOW);
[/code]

Normally, in C++, the equivalent call I am trying to make would be:

[code]
recti windowPos = recti(50,50,400,450);
stringw consoleTitle = L"Console"
IGUIWindow* win = gui_env->addWindow(windowPos,false,consoleTitle,0,ID_GUI_CONSOLE_WINDOW);[/code]

The only difference is the 4th field, null and 0 respectively. To troubleshoot, I made a wrapper function:

[code]
static irr::gui::IGUIWindow* addWindowWrapper(const irr::core::recti &a,bool b,const wchar_t* c,irr::gui::IGUIElement* d,s32 e,irr::gui::IGUIEnvironment* env)
{
d = 0;
env->addWindow(a,b,c,d,e);
}
[/code]

And changed the registration call to:

[code]
r = engine->RegisterObjectMethod("GEnvironment", "GWindow@ addWindow(const recti &in,bool &in,const stringw &in,GElement@ &in,s32 &in)", asFUNCTION(addWindowWrapper), asCALL_CDECL_OBJLAST); assert(r >= 0);
[/code]

In the wrapper, without the "d = 0" line to set the IGUIElement* pointer to 0, a value of 0xfffffff4 gets passed to the addWindow function, crashing it.

So I have a work around in place, my question is, how do you pass a pointer to 0 from the script? null seems to equate to 0xfffffff4 as seen above. I tried inputting an int, but it messed up the declaration because I have other functions that expect a pointer to an IGUIElement and not null, so I need a handle in the script call somewhere.

Any suggestions?

Share this post


Link to post
Share on other sites
It seems there are deeper problems somewhere in the way I've registered or recieved these values, I updated the wrapper function:

[code]
static irr::gui::IGUIWindow* addWindowWrapper(const irr::core::recti &a,bool b,const wchar_t* c,irr::gui::IGUIElement* d,s32 e,irr::gui::IGUIEnvironment* env)
{

d = 0;
if (b == true)
{
std::cout<<"True\n";
}
if (b == false)
{
std::cout<<"False\n";
}
irr::core::stringw tempc(c);
irr::core::stringc temp = tempc.c_str();
std::cout<<temp.c_str()<<"\n";
std::cout<<e<<"\n";
env->addWindow(a,b,c,d,e);
}
[/code]

And I got:

[code]
True
False
<gibberish>
31264416
[/code]

The gibberish is probably because angelscript passed a stringw but the function is expecting a wchar_t* array. The rest I have no clue as to why it's getting mangled.

Interestingly, apparently the only values getting passed correctly is the "recti" and "stringw" types. The rest appear to be garbage.

Share this post


Link to post
Share on other sites
hi

You are passing parameters by value in c++ but registered function to expect them by reference (&in part), that is why only recti gets correct values, it's reference on both sides.
Same goes for handle, it should be registered as [color=#008800][font=CourierNew, monospace][size=2]GElement@[/size][/font][/color] (without &in).
I don't know how stringw type is registered, but it's very likely that passing raw pointer will crash app at some point.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this