Update Historized Items leading 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
er.raunakgupta
Hero Member
Hero Member
Posts: 37
Joined: 11 May 2017, 09:26

Update Historized Items leading to Segmentation Fault

Post by er.raunakgupta »

Hi all,

I have implemented a function to update Historized items during the start of my server. For that I am providing my code below.

Code: Select all

UaStatus AggrServerHistoryManager::updateHistorizedItems()
{
    UaStatus status;
    // Create internal monitored items for historizing
    if (m_pSession->isActivated())
    {
        DataMonitoredItemSpecArray dataMonitoredItems;
        OpcUa_UInt32 i = 0;
        std::map<UaNodeId,HistorizedVariable*>::iterator it;
        dataMonitoredItems.create((OpcUa_UInt32)m_mapVariables.size());
        // Collect information for the monitored items to create
        for (it = m_mapVariables.begin(); it != m_mapVariables.end(); it++)
        {
            HistorizedVariable* pVariable = it->second;
            pVariable->m_pVariable->nodeId().copyTo(&dataMonitoredItems[i].m_itemToMonitor.NodeId);
            dataMonitoredItems[i].m_itemToMonitor.AttributeId = OpcUa_Attributes_Value;
            dataMonitoredItems[i].m_requestedSamplingInterval = ASHISTORYMANAGER_SAMPLING_INTERVAL;
            dataMonitoredItems[i].m_pDataCallback = pVariable;
            i++;
        }

       // Create the monitored items
        status = m_pServerManager->createDataMonitoredItems(m_pSession, dataMonitoredItems);

        if (status.isGood())
        {
            i = 0;
            // Store the create results
            for (it = m_mapVariables.begin(); it != m_mapVariables.end(); it++)
            {
                HistorizedVariable* pVariable = it->second;
                if (dataMonitoredItems[i].m_createResult.isGood())
                {
                    pVariable->m_isValid = OpcUa_True;
                    pVariable->m_monitoredItemId = dataMonitoredItems[i].m_monitoredItemId;
                    pVariable->setHistoryManager(this);
                    if (dataMonitoredItems[i].m_isInitialValueProvided != OpcUa_False)
                    {
                        pVariable->dataChange(dataMonitoredItems[i].m_initialValue);
                    }
                }
                i++;
            }
        }
    }
    return status;
}
The map variable m_mapVariables is a list having all the namespace variables to be historised. Unfortunately sometimes this function runs and sometimes it gives a segmentation fault. The fault comes when the below line gets executed for creating data monitored items (SDK).

Code: Select all

status = m_pServerManager->createDataMonitoredItems(m_pSession, dataMonitoredItems)
Could someone please have a look to my function and suggest where exactly could be the problem?

Thanks
Raunak

er.raunakgupta
Hero Member
Hero Member
Posts: 37
Joined: 11 May 2017, 09:26

Re: Update Historized Items leading to Segmentation Fault

Post by er.raunakgupta »

Hi,

Any update on this?

Thanks
rGupta

er.raunakgupta
Hero Member
Hero Member
Posts: 37
Joined: 11 May 2017, 09:26

Re: Update Historized Items leading to Segmentation Fault

Post by er.raunakgupta »

Hi,

On further debugging the code it seems like loading more devic is causing some timing problem (mutex) in the below line:

Code: Select all

// Create the monitored items
status = m_pServerManager->createDataMonitoredItems(m_pSession, dataMonitoredItems);
Please assist.

Thanks
rGupta

er.raunakgupta
Hero Member
Hero Member
Posts: 37
Joined: 11 May 2017, 09:26

Re: Update Historized Items leading to Segmentation Fault

Post by er.raunakgupta »

Hi Support team,

This issue has been opened since long and there is no response to it.
Please let me know whether any feedback could be given to it or not. I would be happy to provide you any further details required.

Thanks
rG

er.raunakgupta
Hero Member
Hero Member
Posts: 37
Joined: 11 May 2017, 09:26

Re: Update Historized Items leading to Segmentation Fault

Post by er.raunakgupta »

This issue was solved. The

Code: Select all

pVariable->setHistoryManager(this);
was not on its correct place. The correct code is below:

Code: Select all

UaStatus AggrServerHistoryManager::updateHistorizedItems()
{
    UaStatus status;
    // Create internal monitored items for historizing
    if (m_pSession->isActivated())
    {
        DataMonitoredItemSpecArray dataMonitoredItems;
        OpcUa_UInt32 i = 0;
        std::map<UaNodeId,HistorizedVariable*>::iterator it;
        dataMonitoredItems.create((OpcUa_UInt32)m_mapVariables.size());
        // Collect information for the monitored items to create
        for (it = m_mapVariables.begin(); it != m_mapVariables.end(); it++)
        {
            HistorizedVariable* pVariable = it->second;
            pVariable->setHistoryManager(this);
            pVariable->m_pVariable->nodeId().copyTo(&dataMonitoredItems[i].m_itemToMonitor.NodeId);
            dataMonitoredItems[i].m_itemToMonitor.AttributeId = OpcUa_Attributes_Value;
            dataMonitoredItems[i].m_requestedSamplingInterval = ASHISTORYMANAGER_SAMPLING_INTERVAL;
            dataMonitoredItems[i].m_pDataCallback = pVariable;
            i++;
        }

       // Create the monitored items
        status = m_pServerManager->createDataMonitoredItems(m_pSession, dataMonitoredItems);

        if (status.isGood())
        {
            i = 0;
            // Store the create results
            for (it = m_mapVariables.begin(); it != m_mapVariables.end(); it++)
            {
                HistorizedVariable* pVariable = it->second;
                if (dataMonitoredItems[i].m_createResult.isGood())
                {
                    pVariable->m_isValid = OpcUa_True;
                    pVariable->m_monitoredItemId = dataMonitoredItems[i].m_monitoredItemId;
                   
                    if (dataMonitoredItems[i].m_isInitialValueProvided != OpcUa_False)
                    {
                        pVariable->dataChange(dataMonitoredItems[i].m_initialValue);
                    }
                }
                i++;
            }
        }
    }
    return status;

Post Reply