812 views
in System Integration by

Hello, we are experiencing a crash on one of our screens

An example in semi pseudo code, we have defined a native method in chora called passString()

native void passString(uint32 aStringPointer){

 char *myString = (char*) aStringPointer;

 StringWrapper stringWrapper = EwNewObject( StringWrapper , 0 );

 stringWrapper->theString = EwNewStringAnsi(myString);       

 CoreSystemEvent_Trigger( &_this->SystemNewString, ((XObject)stringWrapper), 0 );
}

This method is called from the C side, and passes a pointer to a string, we create a wrapper class for the string and assign the varible theString via EwNewStringAnsi(myString); 

We then fire off a system event which is picked up by our UI, the string appears, and then we get a crash...

The method we are using to display the string is as follows: On our screen, we receive the event and process it as follows:

slot SystemNewStringHandler{

var StringWrapper stringWrapper = (StringWrapper)SystemNewStringHandler.Context;

StatusLabel.String = stringWrapper.theString; 
}

How do we avoid the unmanaged string panic in this circumstance? Must we keep a reference to my passed stringWrapper on my screen? Otherwise it gets deleted when the method returns?

Thank you very much!

1 Answer

0 votes
by

Hi,

well, you are trying an advance technique to access object members directly from C code. This is ok. But it requires a deep understand about the system. In our documentation we limit to address techniques, which are safer and more simple.

Anyway, in your C code you create a string as copy of an ANSI string. This is good. Embedded Wizard will fail when you feed it with foreign strings. The error with this implementation is, you assign the string directly to a data member (variable) of the previously created String::Wrapper instance.

When you implement such operation in Chora and then you take a look at the generated code you would see, that the simple assignment is represented by an invocation of a Runtime Environment function EwRetainString(). For example:

  EwRetainString( &_this->theString, someString );

The function EwRetainString() manages the garbage collection of strings. Since the usage of this function is advance, we don't document it and we don't recommend it. If you are interesting in this function, please see the header file ewrte.h. In all cases, when you have doubts regarding your C code, just implement the corresponding operation in Chora and analyze the generated code.

Hope it helps you

Best regards

Paul Banach

Embedded Wizard Website | Privacy Policy | Imprint

...