fixed bug in instance discovery DCI deletion
authorVictor Kirhenshtein <victor@netxms.org>
Sat, 2 Jul 2016 20:14:16 +0000 (23:14 +0300)
committerVictor Kirhenshtein <victor@netxms.org>
Sat, 2 Jul 2016 20:14:16 +0000 (23:14 +0300)
src/server/core/dctarget.cpp
src/server/core/template.cpp

index e162b4e..adacdf3 100644 (file)
@@ -351,27 +351,25 @@ void DataCollectionTarget::cleanDeletedTemplateItems(UINT32 dwTemplateId, UINT32
  */
 void DataCollectionTarget::unbindFromTemplate(UINT32 dwTemplateId, bool removeDCI)
 {
  */
 void DataCollectionTarget::unbindFromTemplate(UINT32 dwTemplateId, bool removeDCI)
 {
-   UINT32 i;
-
    if (removeDCI)
    {
       lockDciAccess(true);  // write lock
 
    if (removeDCI)
    {
       lockDciAccess(true);  // write lock
 
-               UINT32 *pdwDeleteList = (UINT32 *)malloc(sizeof(UINT32) * m_dcObjects->size());
-               UINT32 dwNumDeleted = 0;
+               UINT32 *deleteList = (UINT32 *)malloc(sizeof(UINT32) * m_dcObjects->size());
+               int numDeleted = 0;
 
 
-      for(i = 0; i < (UINT32)m_dcObjects->size(); i++)
+               int i;
+      for(i = 0; i < m_dcObjects->size(); i++)
          if (m_dcObjects->get(i)->getTemplateId() == dwTemplateId)
          {
          if (m_dcObjects->get(i)->getTemplateId() == dwTemplateId)
          {
-            pdwDeleteList[dwNumDeleted++] = m_dcObjects->get(i)->getId();
+            deleteList[numDeleted++] = m_dcObjects->get(i)->getId();
          }
 
          }
 
-               for(i = 0; i < dwNumDeleted; i++)
-                       deleteDCObject(pdwDeleteList[i], false);
+               for(i = 0; i < numDeleted; i++)
+                       deleteDCObject(deleteList[i], false);
 
       unlockDciAccess();
 
       unlockDciAccess();
-
-               safe_free(pdwDeleteList);
+               free(deleteList);
    }
    else
    {
    }
    else
    {
index 439ae89..016e4be 100644 (file)
@@ -465,6 +465,10 @@ bool Template::deleteDCObject(UINT32 dcObjectId, bool needLock)
          if ((object->getType() == DCO_TYPE_ITEM) && (((DCItem *)object)->getInstanceDiscoveryMethod() != IDM_NONE))
          {
             deleteChildDCIs(dcObjectId);
          if ((object->getType() == DCO_TYPE_ITEM) && (((DCItem *)object)->getInstanceDiscoveryMethod() != IDM_NONE))
          {
             deleteChildDCIs(dcObjectId);
+
+            // Index may be incorrect at this point
+            if (m_dcObjects->get(i) != object)
+               i = m_dcObjects->indexOf(object);
          }
          // Destroy item
                        DbgPrintf(7, _T("Template::DeleteDCObject: deleting DCObject %d from object %d"), (int)dcObjectId, (int)m_id);
          }
          // Destroy item
                        DbgPrintf(7, _T("Template::DeleteDCObject: deleting DCObject %d from object %d"), (int)dcObjectId, (int)m_id);