Method call leads to segmentation fault!

Questions regarding the use of the C++ SDK for Server or Client development or integration into customer products ...

Moderator: uasdkcpp

Post Reply
tommys
Hero Member
Hero Member
Posts: 33
Joined: 03 Oct 2023, 16:42

Method call leads to segmentation fault!

Post by tommys »

Hi,

I have a method in my OPCUA server (based on C++SDKv1.8.3) that takes two input arguments and everything works fine when I call the method from an OPCUA client, UaExpert for example, with valid input. However, if the input is invalid, the OPCUA server crashes because of a segmentation fault! Reading documentation and looking in the examples does not clearly explain if I do something wrong... Doc says: "pCallback: Callback interface used for the transaction. The MethodManager must use this interface to finish the action for each passed node in the transaction.". So, I interpret this as I must always call callback->finishCall, ok? However, this leads to a segfault.

Here's the situation explained in more detail:
  • My MethodManager subclass overrides the virtual beginCall method.
  • In my MethodManager subclass beginCall method I check the inputArguments and if they are not valid I create an inputArgumentResult array of size 2 with elements set to OpcUa_BadInvalidArgument and I also set the overall statusCode to OpcUa_BadInvalidArgument. Furthermore I create a one-element uavariant-int32 outputArgument array with value set to -1.
  • In my MethodManager subclass beginCall method I then finish the call transaction by calling finishCall-method on the input pCallback pointer (of type UaCallContext) with the arguments just explained. The last thing that happens in UaCallContext::finishCall is that it deletes itself: delete this; (in uatransactionmanager.cpp).
  • After this, my MethodManager subclass beginCall-method immediatly returns to UaTransactionManager::executeMethodCall with the OpcUa_BadInvalidArgument return value.
  • UaTransactionManager::executeMethodCall checks if the return value is good, which it isn't, so it then tries to call pUaCallContext->finishCall. Problem is that pUaCallContext object has been deleted (by itself) already, which leads to undefined behaviour and results in a segfault crash.

So, please can you explain how this shall be handled? IMO, the documentation does not clearly say how to handle this. One guess from my side is that my MethodManagerSubClass:beginCall-method should always call callback->finishCall (as doc says?) and unintuitively always return OpcUa_Good (as doc does not say?) from my MethodManagerSubClass:beginCall-method. Another guess is that my MethodManagerSubClass:beginCall-method should only call callback->finishCall if the method call could be handled fully in my server and return OpcUa_Good, otherwise I should return a OpcUa_Bad and, counter to what the documentation says, not call callback->finishCall. I don't know which is best, and maybe there is yet another way that I have overlooked?

Regards,
/Tommy

User avatar
Support Team
Hero Member
Hero Member
Posts: 3171
Joined: 18 Mar 2011, 15:09

Re: Method call leads to segmentation fault!

Post by Support Team »

Hi,

your assumption to allways call the finish is wrong. In your implementation of beginCall you can

EITHER:
  • call finishCall and return Good
OR:
  • return a bad status (and not call finishCall)
In the sample code we always call finishCall and pass a bad status as an argument to finishCall, if an error occured. In this case you always return Good.
Best regards
Unified Automation Support Team

tommys
Hero Member
Hero Member
Posts: 33
Joined: 03 Oct 2023, 16:42

Re: Method call leads to segmentation fault!

Post by tommys »

Thanks for spelling that out! I prefer reading documentation instead of looking at examples, because they are... just examples. Therefore, it would be great if documentation could spell that out too, instead of me (and others I guess) having to dive deep into the UA implementation.

Regards,
/Tommy

User avatar
Support Team
Hero Member
Hero Member
Posts: 3171
Joined: 18 Mar 2011, 15:09

Re: Method call leads to segmentation fault!

Post by Support Team »

Hi,

yes correct, we also prefer reading documentation, however some details are simply shown in single line of code rather than explained in 1000 words.
But I totally agree that specially this issue/question was asked few times, hence the documentation definately needs improvement and more clarity.
Best regards
Unified Automation Support Team

Post Reply