Ref counting
[iotcloud.git] / version2 / src / C / Transaction.cc
index 258c22b6d61c43f113aa4f3f6363024c4b11e7d1..b0d3c5bd50d4f0ccdcb69c320c4809907cd4ac42 100644 (file)
@@ -30,6 +30,9 @@ Transaction::~Transaction() {
        if (missingParts)
                delete missingParts;
        {
+               uint Size = parts->size();
+               for(uint i=0; i<Size; i++)
+                       parts->get(i)->releaseRef();
                delete parts;
        }
        {
@@ -54,11 +57,14 @@ Transaction::~Transaction() {
 }
 
 void Transaction::addPartEncode(TransactionPart *newPart) {
+       newPart->acquireRef();
+       printf("Add part %d\n", newPart->getPartNumber());
        TransactionPart *old = parts->setExpand(newPart->getPartNumber(), newPart);
        if (old == NULL) {
                partCount++;
-       } else
-               delete old;
+       } else {
+               old->releaseRef();
+       }
        partsPendingSend->add(newPart->getPartNumber());
 
        sequenceNumber = newPart->getSequenceNumber();
@@ -75,7 +81,7 @@ void Transaction::addPartDecode(TransactionPart *newPart) {
                newPart->setDead();
                return;
        }
-
+       newPart->acquireRef();
        sequenceNumber = newPart->getSequenceNumber();
        arbitratorId = newPart->getArbitratorId();
        transactionId = newPart->getTransactionId();
@@ -88,6 +94,7 @@ void Transaction::addPartDecode(TransactionPart *newPart) {
 
        if (previouslySeenPart != NULL) {
                // Set dead the old one since the new one is a rescued version of this part
+               previouslySeenPart->releaseRef();
                previouslySeenPart->setDead();
        } else if (newPart->isLastPart()) {
                missingParts = new Hashset<int32_t>();
@@ -253,10 +260,6 @@ void Transaction::setDead() {
        }
 }
 
-TransactionPart *Transaction::getPart(int index) {
-       return parts->get(index);
-}
-
 void Transaction::decodeTransactionData() {
        // Calculate the size of the data section
        int dataSize = 0;