Hi,

I have the following requirement,

Data shall be received at runtime from network, this data has an unique ID/Key
associated with it, say 100 or 103

Many such data can come over a period of time and it can come for the
for the same Key or different Keys. This data has to be displayed in
a GUI and capacity to store a maximum of 50 such data for a given Key
has to be provided.

Request to delete all the data associated with a Key, comes asynchronously
at runtime, which should clear all the data held for that Key.

To implement this i have used QHash

Qt Code:
  1. QHash<int, QVector<TTrack> *> hash_for_track;
  2. class TTrack
  3. {
  4. public:
  5. TTrack();
  6.  
  7. ~TTrack()
  8. {
  9.  
  10. }
  11. int operator==(TTrack other);
  12.  
  13. unsigned short track_number:12;
  14. unsigned short reserved:4;
  15.  
  16. unsigned char track_status;
  17. unsigned char track_status_ext;
  18. signed short x_comp;
  19. signed short y_comp;
  20. signed short height;
  21.  
  22. unsigned short calculated_ground_speed;
  23. unsigned short calculated_heading;
  24. UINT16 flight_level:14;
  25. UINT16 flt_lvl_garbel:1;
  26. UINT16 flt_lvl_validity:1;
  27. unsigned short rho;
  28. unsigned short theta;
  29. UINT16 Mode1Code:5;
  30. UINT16 Mode1CodeMatch:1;
  31. UINT16 Mode1Garbel:1;
  32. UINT16 Mode1Validity:1;
  33. UINT16 Mode2Code:12;
  34. UINT16 Mode2Confidencel:1;
  35. UINT16 Mode2CodeMatch:1;
  36. UINT16 Mode2Garbel:1;
  37. UINT16 Mode2Validity:1;
  38. UINT16 Mode3ACode:12;
  39. UINT16 Mode3AConfidencel:1;
  40. UINT16 Mode3ACodeMatch:1;
  41. UINT16 Mode3AGarbel:1;
  42. UINT16 Mode3AValidity:1;
  43. //Code match
  44. /*
  45.   This is to hold the Code match
  46.   Info for the New Track
  47. */
  48. bool mode1_code_match;
  49. bool mode2_code_match;
  50. bool mode3_code_match;
  51. bool code_match;
  52. };
To copy to clipboard, switch view to plain text mode 


And the functions are
a. "track_dbase(TTrack local_track)"
One to store all the data related to New Key/ Old Key
b. "On_TrackDeleteSignal(const S_TRACK_DELETE_MSG &resp) "
Delete all the data associated with a Key


Function Implementation:
Qt Code:
  1. void CircleWidget::track_dbase(TTrack local_track)
  2. {
  3. QHash<int, QVector<TTrack> *>::const_iterator iter_for_track_hash;
  4.  
  5. UINT16 key = 0;
  6. key = local_track.track_number;
  7.  
  8. /*
  9. * 16-Oct-2009
  10. * Track number is only 12 bits
  11. * so bit wise AND with this value
  12. */
  13. key = key & 0x0FFF;
  14.  
  15. if (hash_for_track.isEmpty() ==true)
  16. {
  17. /* no key exists in the hash so diretly insert the key and pointer to the track data
  18. into the HASH
  19. */
  20.  
  21. m_vtrack_ptr = new QVector<TTrack>;
  22. if (m_vtrack_ptr ==NULL)
  23. {
  24. }
  25. else
  26. {
  27. m_vtrack_ptr->append(local_track);
  28. hash_for_track.insert(key, m_vtrack_ptr);
  29. }
  30. }
  31. else
  32. {
  33.  
  34. if (trial_enabled == 0)
  35. {
  36. /*here we dont have store the trial details for all the tracks
  37. only one element needs to be there in the vector of each
  38. trackid
  39. */
  40. number_of_points_in_vector = 1;
  41.  
  42. } //end of if trial enabled
  43. else
  44. {
  45. /* check to see if the key exists in the hash_for_track table
  46. */
  47. number_of_points_in_vector = ELEMENTS_IN_TRACK_DBASE;
  48. }
  49.  
  50. if (hash_for_track.contains(key) == false)
  51. {
  52. /*
  53. update the vector wtth the current track data
  54. insert the (key,value) into the hash
  55. */
  56.  
  57. m_vtrack_ptr = new QVector<TTrack>;
  58. if (m_vtrack_ptr ==NULL)
  59. {
  60. }
  61. else
  62. {
  63. m_vtrack_ptr->append(local_track);
  64. hash_for_track.insert(key, m_vtrack_ptr);
  65. }
  66.  
  67. } //end of if key is not present
  68. else
  69. {
  70.  
  71. /*hash table already has the key specified retreive the correcsponding value and then
  72. update the vector
  73. */
  74.  
  75. /*
  76. here come and append into the vector
  77. if the size is less than 50
  78.  
  79. if the size is equal to 50 then remove the element at index 0 and then apend
  80. because at any instant i need only 50 elements in the vector.
  81. */
  82. iter_for_track_hash = hash_for_track.find(key);
  83. QVector<TTrack>* m_vtrack_ptr_alreadyinhash =
  84. iter_for_track_hash.value();
  85. if (m_vtrack_ptr_alreadyinhash == NULL)
  86. {
  87. }
  88. else
  89. {
  90. if (m_vtrack_ptr_alreadyinhash->size()
  91. < number_of_points_in_vector)
  92. {
  93. m_vtrack_ptr_alreadyinhash->append(local_track);
  94. }
  95. else
  96. {
  97. m_vtrack_ptr_alreadyinhash->remove(0);
  98. m_vtrack_ptr_alreadyinhash->append(local_track);
  99. }
  100. }
  101. }
  102. }
  103. }
  104.  
  105. void QCSurveillance::On_TrackDeleteSignal(const S_TRACK_DELETE_MSG &resp)
  106. {
  107. int number_trackdel = -1;
  108.  
  109. if (hm->m_gen.m_cwid->hash_for_track.isEmpty() == false)
  110. {
  111. //Find the key in the Hash Table if found then delete
  112. if (hm->m_gen.m_cwid->hash_for_track.contains(resp.track_id) == true)
  113. {
  114.  
  115. number_trackdel
  116. = hm->m_gen.m_cwid->hash_for_track.remove(resp.track_id);
  117. if (number_trackdel==0)
  118. if (RDSDEBUG)
  119. qWarning("Delete Failed %d", resp.track_id);
  120. else
  121. ;
  122.  
  123. }
  124. else
  125. {
  126. ;
  127. }
  128. }
  129. else
  130. {
  131. qDebug("Track Delete warning: No track in the database");
  132. }
  133. }
To copy to clipboard, switch view to plain text mode 

Problem:
Application consumes memory continuosly, to debug the same i used Valgrind, which reported memory leak errors in the "new" operator used in the function "track_dbase".

I have removed the element in the hash using "remove" function for the given key in function "On_TrackDeleteSignal". Still i get memory leak error.

Query:
1. Is there any problem in the way i have used "new" operator in "track_dbase" function?
2.Does "remove" for a key in Hash remove all the memory explicitly used by value in the function "On_TrackDeleteSignal" ?
3. I have not defined the function "int operator==(TTrack other);" is it necessary to do it, and can this be a source of error?

I have limited time to solve this issue ... Kindly looking forward response from the community