Advertisement

Passing a null pointer

Started by October 05, 2011 06:39 AM
3 comments, last by BrianEmpson 13 years, 1 month ago
Hello!

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


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


I registered it as:


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);


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


@conWin = GUIEnv.addWindow(windowPos,false,consoleTitle,null,ID_GUI_CONSOLE_WINDOW);


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


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


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


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);
}


And changed the registration call to:


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);


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?
I don't know much about AngelCode, but I would just have two functions: addWindowWithParent and addWindow.

Engineering Manager at Deloitte Australia

Advertisement
It seems there are deeper problems somewhere in the way I've registered or recieved these values, I updated the wrapper function:


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);
}


And I got:


True
False
<gibberish>
31264416


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.
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@[/font] (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.
Thank you, behc. Simply removing the &in on the values passed by value worked wonderfully. I was under the impression that you needed the &in all the time. Cool, thanks!

This topic is closed to new replies.

Advertisement