3.15 P003: returning shared_ptr from functions

Discussions about product bugs & problems!
Note: This is no replacement for the Official ETM Support!
2 posts • Page 1 of 1
iain
Posts:33
Joined: Fri May 19, 2017 7:08 pm

3.15 P003: returning shared_ptr from functions

Post by iain »

Hi all. I've been looking at the new shared_ptr datatype in P003 scripting. The one thing I'm getting tied up by is the ability to return a shared_ptr from a function. The code below doesn't give a syntax warning on save, but it generates an exception and crashes the UI.

None of the examples in the docs show returning a shared_ptr, and most deal with simple types, not shared_ptr, so I'm not sure if what I'm doing is legal. I'd think there would be a way to do this for shared_ptrs to be useful in general, but maybe it isn't by returning them directly.

dummy() in the code below originally created the shared_ptr that is returned, but I was concerned that the problem may have been in the object going out of scope, so I changed it to use a passed in shared_ptr. Apologies that the test case is more complex than it could be.

This is tied to a button, and when I click the button in Gedi Quicktest it causes Gedi to generate a "ui Executable has stopped working".

Code: Select all

class First
{
  public string sType() {return "First";}
};

class Second : First
{
  public string sType() {return "Second";}
};

shared_ptr dummy(shared_ptr P)
{
  shared_ptr T = P;
  return T;
}

main(mapping event)
{
  shared_ptr X = new Second();
  DebugN(X.sType());
  shared_ptr V = dummy(X);  
  DebugN(V.sType());
}
Results from the log file: the first DebugN() works, but the exception from the call to dummy() appears earlier in the log.

Code: Select all

    Module: _QuickTest_
    Panel: C:\\WinCC_OA_Proj\\testing\\panels\\test.pnl []
    Object: 1 named: "PUSH_BUTTON2" of type: PUSH_BUTTON
    Script: Clicked
    Line: 14, Given datatype does not match with needed type 'First'
WCCOAui1:["Second"]
Final note: I just tried changing the data type of X to First (shared_ptr X = new First()) and get identical results, so it isn't simply failing to identify inheritance.

Code: Select all

    Module: _QuickTest_
    Panel: C:\\WinCC_OA_Proj\\testing\\panels\\test.pnl []
    Object: 1 named: "PUSH_BUTTON2" of type: PUSH_BUTTON
    Script: Clicked
    Line: 14, Given datatype does not match with needed type 'First'
WCCOAui1:["First"]
Edit: "final note" wasn't so final, because I experimented right after posting. If I change the return type of dummy() to an anytype this appears to work. I'm not sure if that's entirely legitimate or if I'll lose type information, but it may be okay at least for now.

mkoller
Posts:741
Joined: Fri Sep 17, 2010 9:03 am

Re: 3.15 P003: returning shard_ptr from functions

Post by mkoller »

Sadly there was a bug in returning a shared_ptr of a user-defined type (enum, class) which led to the crash.
This was already fixed and will be included in the next patch.

2 posts • Page 1 of 1