Jump to content

[SOLVED] What is the correct way to return a string from a plugin function?


photo

Recommended Posts

Posted

I have a class function which will return a const char*, but I've tried with multiple method to return the pointer from the function, all these will cause the engine crash, seems it's the problem of memory management of script engine.

 

1

 

const char *Device::getInfo() {
    Unigine::String *p = new Unigine::String(device.toString().c_str());
    return p->get();
}

 

2

 

const char *Device::getInfo() {
    return device.toString().c_str();
}

 

3

 

// create a char info[64] as a member variable of Device class then copy the device.toString().c_str(); to the info array in device's constructor
const char *Device::getInfo() {
    return info;
}

 

all this will not work if I use the function as these in script

 

log.message("got device: %s\n", device.getInfo());

 

And I also tried to change the class function to

 

Unigine::String Device::getInfo() const ;

 

it still won't work. So how can I return  a string from a plugin function to use in script?

 

Posted

I know method 2 definitly won't work, because the device.toString() return's a temp std::string variable and will be deleted after the function returns.

 

but I can not understand why the others won't work .

 

all crash info are same:

 

Program: ...evelop\UnigineSDK_source_2013-08-16_windows\bin\main_x64d.exe
File: f:\dd\vctools\crt_bld\self_64_amd64\crt\src\dbgheap.c
Line: 1424

Expression: _pFirstBlock == pHead

For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.

(Press Retry to debug the application)
main_x64d.exe has triggered a breakpoint.
The program '[2652] main_x64d.exe' has exited with code 0 (0x0).
Posted

All methods should work. What compiler version do you using?

Posted

i'm using visual studio 2012 with update 3 and debug version of engine.

Posted

OK, this is not a problem, the device sdk are built with vs2010, and I'm using vs2012, this only happens when I use /RTC1 to compile the plugin,

 

when switching to the release version and don't use run time check, everything works fine, except the method 2 won't work.

  • 2 weeks later...
Posted

Check the Interface plugin source code (source/plugins/Interface/Interface/).

We are using all "string to script" variants without any problem.

×
×
  • Create New...