Hi,

I have the following code:

Qt Code:
  1. BinaryEncodedDictionary* BinaryMetaDataDecoder::parse(QIODevice *sourceDevice) {
  2. qDebug() << "Received a QIODevice to parse from!";
  3.  
  4. if(sourceDevice->peek(1) != "d") {
  5. qDebug() << "Input stream contains an invalid input file";
  6. return 0;
  7. }
  8. else {
  9. qDebug() << "Found a dictionary, valid file";
  10. return parseDictionary(sourceDevice);
  11. }
  12. }
  13.  
  14. BinaryEncodedDictionary* BinaryMetaDataDecoder::parseDictionary(QIODevice *stream) {
  15. //... Implementation will follow here
  16. return new BinaryEncodedDictionary;
  17. }
  18.  
  19. BinaryEncodedInteger* BinaryMetaDataDecoder::parseInteger(QIODevice *stream) {
  20. //... Implementation will follow here
  21. return new BinaryEncodedInteger;
  22. }
  23.  
  24. BinaryEncodedString* BinaryMetaDataDecoder::parseString(QIODevice *stream) {
  25. //... Implementation will follow here
  26. return new BinaryEncodedString;
  27. }
  28.  
  29. BinaryEncodedList* BinaryMetaDataDecoder::parseList(QIODevice *stream) {
  30. //... Implementation will follow here
  31. return new BinaryEncodedList;
  32. }
To copy to clipboard, switch view to plain text mode 

All of the classes above are plain old C++ classes (not inheriting from QObject) and part of
the following class hierarchy:

Pure virtual (super) class: BinaryEncodedAbstractType
Child (inheriting classes): BinaryEncodedDictionary, BinaryEncodedInteger, BinaryEncodedString, BinaryEncodedList

BinaryEncodedDictionary is implemented like this:

Qt Code:
  1. //binaryencodeddictionary.h
  2. class BinaryEncodedDictionary : public BinaryEncodedAbstractType {
  3. public:
  4. BinaryEncodedDictionary();
  5. //TODO: Delete pointers contained by hashMap
  6. ~BinaryEncodedDictionary();
  7. private:
  8. QHash<BinaryEncodedString*, BinaryEncodedAbstractType*> hashMap;
  9. };
  10.  
  11. //binaryencodeddictionary.cpp
  12. BinaryEncodedDictionary::BinaryEncodedDictionary() {
  13. hashMap = new QHash<BinaryEncodedString*, BinaryEncodedAbstractType*>();
  14. }
  15.  
  16. BinaryEncodedDictionary::~BinaryEncodedDictionary() {
  17. //TODO: We need to remove dynamically allocated values (needed for polymorphism)
  18. }
To copy to clipboard, switch view to plain text mode 

The hashMap member above contains the heap-allocated return values from calls to parse*() functions as keys
and values. Without going further into implementation details (if not needed), my main concern is the
deletion of objects allocated by the parse*() functions. The returned BinaryEncodedDictionary from parse()
function contains the pointers to these objects through the hashMap member variable.

I am trying to make sure that the objects are de-allocated when BinaryEncodedDictionary goes out of scope eventually,
and at the same time, I would like to make use of QScopedPointer and/or QSharedPointer in order not to have
to deal with manually freeing the memory with delete.

How could I use above in my code? I am planing to return QScopedPointers instead of raw pointers as the return
values from the functions above, but will it be enough or do I have to do anything more? Any other suggestions?

I am very thankfull to any kind of desing improvement suggestions as well.

Thank you in advance.

Kind Regards,
Veroslav