Browse Source

Fix memory leak in recursive serialization

Alexey Edelev 6 years ago
parent
commit
a084cda9c5
2 changed files with 2 additions and 2 deletions
  1. 2 1
      src/lib/protobufobject.h
  2. 0 1
      tests/serializationtest.cpp

+ 2 - 1
src/lib/protobufobject.h

@@ -32,6 +32,7 @@
 #include <QBitArray>
 #include <QBitArray>
 
 
 #include <unordered_map>
 #include <unordered_map>
+#include <memory>
 #include <type_traits>
 #include <type_traits>
 
 
 #define ASSERT_FIELD_NUMBER(X) Q_ASSERT_X(X < 128 && X > 0, T::staticMetaObject.className(), "fieldIndex is out of range")
 #define ASSERT_FIELD_NUMBER(X) Q_ASSERT_X(X < 128 && X > 0, T::staticMetaObject.className(), "fieldIndex is out of range")
@@ -94,7 +95,7 @@ public:
                 const void *src = propertyValue.constData();
                 const void *src = propertyValue.constData();
                 //TODO: each time huge objects will make own copies
                 //TODO: each time huge objects will make own copies
                 //Probably generate fields reflection is better solution
                 //Probably generate fields reflection is better solution
-                ProtobufObjectPrivate *value = reinterpret_cast<ProtobufObjectPrivate *>(QMetaType::create(userType, src));
+                auto value = std::unique_ptr<ProtobufObjectPrivate>(reinterpret_cast<ProtobufObjectPrivate *>(QMetaType::create(userType, src)));
                 result.append(serializeLengthDelimited(value->serializePrivate(),
                 result.append(serializeLengthDelimited(value->serializePrivate(),
                                   fieldIndex));
                                   fieldIndex));
             }
             }

+ 0 - 1
tests/serializationtest.cpp

@@ -329,6 +329,5 @@ TEST_F(SerializationTest, ComplexTypeSerializeTest)
     test.setTestComplexField(stringMsg);
     test.setTestComplexField(stringMsg);
 
 
     result = test.serialize();
     result = test.serialize();
-    qDebug() << "result: " << result.toHex();
     ASSERT_TRUE(result == QByteArray::fromHex("120832067177657274790859"));
     ASSERT_TRUE(result == QByteArray::fromHex("120832067177657274790859"));
 }
 }