Community Forum
ANVEL Licensing Options

[Solved] ACCESS_VIOLATION exception thrown by XmlUtils::GetStringAttribute



  • If you call XmlUtils::GetStringAttribute with an attribute name that doesn't exist in your XML file, this will generate an ACCESS_VIOLATION exception. Example:

    StaticAssetParamsPtr MySensorFactory::ParseXmlParams(const tinyxml2::XMLElement* pXmlParams) {
    ...
    XmlUtils::GetStringAttribute(pXmlParams, "my_attribute", nullptr)
    ...
    }

    If my_attribute is not defined within the <sensor> tag, you'll see this problem. This causes ANVEL to crash on start-up.



  • Thank you for contacting ANVEL.

    Is the result of XmlUtils::GetStringAttribute being assigned to an Anvel::String? If so, then you'll need to change parameter three from nullptr to an empty string to avoid this behavior.

    This method returns a const char* (not an Anvel::String) that will be populated with the default value specified in the third parameter (defaultStr) if the attribute is not found. If the value is being stored in a std::string or Anvel::String, nullptr is not a valid default as it will cause the error shown above. If the value is being stored in a const char*, it should work, and also allow you to check for a nullptr value in subsequent usage.

    The two following examples should work:
    const char* pStrResult = XmlUtils::GetStringAttribute(pXmlParams, "my_attribute", nullptr);
    if (pStrResult == nullptr)
    ...

    or
    String result = XmlUtils::GetStringAttribute(pXmlParams, "my_attribute", "");
    if (result.empty())
    ...

    If that is not the issue, please let us know!



  • Thanks, that fixed the problem.