585 views
in GUI Development by

Read the doc on string changes in native code blocks. But I'm a bit confused why the following doesnt work:

native
{
 [...]
 ApplicationDeviceClass device = EwGetAutoObject( &ApplicationDevice, ApplicationDeviceClass );
 device->WIFI_Scan_SSID[c-1] = EwNewStringAnsi("TEST");
}

I get a "[FATAL ERROR in C:/NXP/IMXRT1060-EVK_CIRAS4/PlatformPackage/RTE/ewstring.c:1563] Unmanaged string 'TEST\x8150\x811B\x001A'"

I'm able to do this in a native() JavaScript block. But what is the proper way to change an AutoObject "device" string in a native() C block? 

Also, "WIFI_Scan_SSID " is an EW array string. Which could be 100 strings long. So passing native(string1,string2, etc.) into the block and then stuffing WIFI_Scan_SSID with these local strings would be quite combersome. 

 

1 Answer

0 votes
by

Hello Mike,

in your approach you want to modify a member of the device object directly from the native code by assing to it a new string. Accessing the members of objects is possible, but not the recommended way. Better is to use local variables to exchange the values. For example:

var string someValue = "";

// In native code receive some values via local variables
native ( someValue )
{
  someValue = EwNewStringAnsi("TEST");
}

// The recommended approach: access the object members only from Chora
Application::Device.WIFI_Scan_SSID[c-1] = someValue;);

When you compile the code, you will see in the resulting C code, that the assignment to the variable WIFI_Scan_SSID is accompanied by additional function call EwRetainString() or EwShareString(). This function call tracks the usage of the string so when the string is not needed anmore, the string is released. Without this function call, the string is released early and unpredictably causing the variable WIFI_Scan_SSID to refer to some invalid memory. This is then detected and the 'Unmanaged string' error is reported. 

Does it help you?

Best regards

Paul Banach

by
" Better is to use local variables to exchange the values." Right, this I knew... However, I have a large sting array to stuff. And I cant pass an array into a native() and then back out. Therefor, I'd need 100s of local variables. Way too combersome.

That said I fixed the issue by doing the following in my native()

  EwRetainString( &device->WIFI_Scan_SSID[c-1], EwNewStringAnsi((char *)WIFI_SCAN_Data->SSID[c]));

But I figured I'd check with you guys if this is a sane thing to do.
by
Hello Mike,

yes, you can do this. Officially this is not the recommended way and I can't guarant that it will continue workikg with future versions of EmWi. In any case, if you mix some Chora/native code and you are not sure how to implement something in C, do this in Chora and then analyze/copy the resulting generated C code.

Best regards

Paul Banach

Ask Embedded Wizard

Welcome to the question and answer site for Embedded Wizard users and UI developers.

Ask your question and receive answers from the Embedded Wizard support team or from other members of the community!

Embedded Wizard Website | Privacy Policy | Imprint

...