241 views
in System Integration by

Hello,

I want to create a struct in the device driver when Init-Method of the EW class is called. This struct has to be deleted, when the class is deleted (for example with garbage collection).

But I am getting following error, when trying to call the native function in the Done-method. 

Unsafe usage of the variable 'reference' within the 'Done()' destructor. Since the variable is declared as '^handle' doing this can result in an access to a potentially disposed object. Please note: during the garbage collection the order of object disposal is not predetermined. The relationships between the objects may become invalid.

Currently I am storing the reference to the struct in an EW variable of type ^handle. When the variable reference is not accessable, how can I delete the struct in the device driver?

Did I miss something? 

// Init: Create new struct
var ^handle hand = reference;

$if !$prototyper && ( $platform != *.WebGL.* )
native( hand)
{
  extern XRef STR_Create();
  hand = STR_Create();
}
$endif
  
reference = hand;

 

// Done: Delete struct
if ( reference == null )
  return;
  
var ^handle hand = reference;

$if !$prototyper && ( $platform != *.WebGL.* )
native( hand )
{
  extern void STR_Delete( XRef hand );
  STR_Delete( hand );
}
$endif

Kind regards Jonas

 

1 Answer

+1 vote
by
 
Best answer

Hello Jonas,

what does STR_Create() do? From your implementation it looks as if it creates a reference to a property. But there is no property specified. Property references can interconnect several objects. During Garbage Collection the order in which objects are disposed (and the methods Done() are invoked) is not predicable. A property reference can thus refer to a property belonging to a previously released object. Trying to access the referenced property may crash the system. Therefore Embedded Wizard reports the error message.

Should STR_Create() just allocate some data structure and return the pointer to this structure, use handle data type to store the pointer in EW application. Don't use ^handle which in fact represents a reference to a property able to store a handle. Doing this is even problematic, because during Garbage Collection the pointer to the data structure (stored in ^handle) is evaluated as a reference to a property. When the Garbage Collection follows this reference, it misinterprets it  causing again a crash.

Following ideas:

Step 1: Change the declaration of the reference variable form ^handle to handle.

Step 2: Change the declaration of the local variables hand form ^handle to handle.

See also the section Data types: handle.

I hope it helps you further.

Best regards

Paul Banach

by
That helped. Thank you very much.

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

...