linuxdev
28th November 2009, 07:38
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
QHash<int, QVector<TTrack> *> hash_for_track;
class TTrack
{
public:
TTrack();
~TTrack()
{
}
int operator==(TTrack other);
unsigned short track_number:12;
unsigned short reserved:4;
unsigned char track_status;
unsigned char track_status_ext;
signed short x_comp;
signed short y_comp;
signed short height;
unsigned short calculated_ground_speed;
unsigned short calculated_heading;
UINT16 flight_level:14;
UINT16 flt_lvl_garbel:1;
UINT16 flt_lvl_validity:1;
unsigned short rho;
unsigned short theta;
UINT16 Mode1Code:5;
UINT16 Mode1CodeMatch:1;
UINT16 Mode1Garbel:1;
UINT16 Mode1Validity:1;
UINT16 Mode2Code:12;
UINT16 Mode2Confidencel:1;
UINT16 Mode2CodeMatch:1;
UINT16 Mode2Garbel:1;
UINT16 Mode2Validity:1;
UINT16 Mode3ACode:12;
UINT16 Mode3AConfidencel:1;
UINT16 Mode3ACodeMatch:1;
UINT16 Mode3AGarbel:1;
UINT16 Mode3AValidity:1;
//Code match
/*
This is to hold the Code match
Info for the New Track
*/
bool mode1_code_match;
bool mode2_code_match;
bool mode3_code_match;
bool code_match;
};
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:
void CircleWidget::track_dbase(TTrack local_track)
{
QHash<int, QVector<TTrack> *>::const_iterator iter_for_track_hash;
UINT16 key = 0;
key = local_track.track_number;
/*
* 16-Oct-2009
* Track number is only 12 bits
* so bit wise AND with this value
*/
key = key & 0x0FFF;
if (hash_for_track.isEmpty() ==true)
{
/* no key exists in the hash so diretly insert the key and pointer to the track data
into the HASH
*/
m_vtrack_ptr = new QVector<TTrack>;
if (m_vtrack_ptr ==NULL)
{
}
else
{
m_vtrack_ptr->append(local_track);
hash_for_track.insert(key, m_vtrack_ptr);
}
}
else
{
if (trial_enabled == 0)
{
/*here we dont have store the trial details for all the tracks
only one element needs to be there in the vector of each
trackid
*/
number_of_points_in_vector = 1;
} //end of if trial enabled
else
{
/* check to see if the key exists in the hash_for_track table
*/
number_of_points_in_vector = ELEMENTS_IN_TRACK_DBASE;
}
if (hash_for_track.contains(key) == false)
{
/*
update the vector wtth the current track data
insert the (key,value) into the hash
*/
m_vtrack_ptr = new QVector<TTrack>;
if (m_vtrack_ptr ==NULL)
{
}
else
{
m_vtrack_ptr->append(local_track);
hash_for_track.insert(key, m_vtrack_ptr);
}
} //end of if key is not present
else
{
/*hash table already has the key specified retreive the correcsponding value and then
update the vector
*/
/*
here come and append into the vector
if the size is less than 50
if the size is equal to 50 then remove the element at index 0 and then apend
because at any instant i need only 50 elements in the vector.
*/
iter_for_track_hash = hash_for_track.find(key);
QVector<TTrack>* m_vtrack_ptr_alreadyinhash =
iter_for_track_hash.value();
if (m_vtrack_ptr_alreadyinhash == NULL)
{
}
else
{
if (m_vtrack_ptr_alreadyinhash->size()
< number_of_points_in_vector)
{
m_vtrack_ptr_alreadyinhash->append(local_track);
}
else
{
m_vtrack_ptr_alreadyinhash->remove(0);
m_vtrack_ptr_alreadyinhash->append(local_track);
}
}
}
}
}
void QCSurveillance::On_TrackDeleteSignal(const S_TRACK_DELETE_MSG &resp)
{
int number_trackdel = -1;
if (hm->m_gen.m_cwid->hash_for_track.isEmpty() == false)
{
//Find the key in the Hash Table if found then delete
if (hm->m_gen.m_cwid->hash_for_track.contains(resp.track_id) == true)
{
number_trackdel
= hm->m_gen.m_cwid->hash_for_track.remove(resp.track_id);
if (number_trackdel==0)
if (RDSDEBUG)
qWarning("Delete Failed %d", resp.track_id);
else
;
}
else
{
;
}
}
else
{
qDebug("Track Delete warning: No track in the database");
}
}
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
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
QHash<int, QVector<TTrack> *> hash_for_track;
class TTrack
{
public:
TTrack();
~TTrack()
{
}
int operator==(TTrack other);
unsigned short track_number:12;
unsigned short reserved:4;
unsigned char track_status;
unsigned char track_status_ext;
signed short x_comp;
signed short y_comp;
signed short height;
unsigned short calculated_ground_speed;
unsigned short calculated_heading;
UINT16 flight_level:14;
UINT16 flt_lvl_garbel:1;
UINT16 flt_lvl_validity:1;
unsigned short rho;
unsigned short theta;
UINT16 Mode1Code:5;
UINT16 Mode1CodeMatch:1;
UINT16 Mode1Garbel:1;
UINT16 Mode1Validity:1;
UINT16 Mode2Code:12;
UINT16 Mode2Confidencel:1;
UINT16 Mode2CodeMatch:1;
UINT16 Mode2Garbel:1;
UINT16 Mode2Validity:1;
UINT16 Mode3ACode:12;
UINT16 Mode3AConfidencel:1;
UINT16 Mode3ACodeMatch:1;
UINT16 Mode3AGarbel:1;
UINT16 Mode3AValidity:1;
//Code match
/*
This is to hold the Code match
Info for the New Track
*/
bool mode1_code_match;
bool mode2_code_match;
bool mode3_code_match;
bool code_match;
};
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:
void CircleWidget::track_dbase(TTrack local_track)
{
QHash<int, QVector<TTrack> *>::const_iterator iter_for_track_hash;
UINT16 key = 0;
key = local_track.track_number;
/*
* 16-Oct-2009
* Track number is only 12 bits
* so bit wise AND with this value
*/
key = key & 0x0FFF;
if (hash_for_track.isEmpty() ==true)
{
/* no key exists in the hash so diretly insert the key and pointer to the track data
into the HASH
*/
m_vtrack_ptr = new QVector<TTrack>;
if (m_vtrack_ptr ==NULL)
{
}
else
{
m_vtrack_ptr->append(local_track);
hash_for_track.insert(key, m_vtrack_ptr);
}
}
else
{
if (trial_enabled == 0)
{
/*here we dont have store the trial details for all the tracks
only one element needs to be there in the vector of each
trackid
*/
number_of_points_in_vector = 1;
} //end of if trial enabled
else
{
/* check to see if the key exists in the hash_for_track table
*/
number_of_points_in_vector = ELEMENTS_IN_TRACK_DBASE;
}
if (hash_for_track.contains(key) == false)
{
/*
update the vector wtth the current track data
insert the (key,value) into the hash
*/
m_vtrack_ptr = new QVector<TTrack>;
if (m_vtrack_ptr ==NULL)
{
}
else
{
m_vtrack_ptr->append(local_track);
hash_for_track.insert(key, m_vtrack_ptr);
}
} //end of if key is not present
else
{
/*hash table already has the key specified retreive the correcsponding value and then
update the vector
*/
/*
here come and append into the vector
if the size is less than 50
if the size is equal to 50 then remove the element at index 0 and then apend
because at any instant i need only 50 elements in the vector.
*/
iter_for_track_hash = hash_for_track.find(key);
QVector<TTrack>* m_vtrack_ptr_alreadyinhash =
iter_for_track_hash.value();
if (m_vtrack_ptr_alreadyinhash == NULL)
{
}
else
{
if (m_vtrack_ptr_alreadyinhash->size()
< number_of_points_in_vector)
{
m_vtrack_ptr_alreadyinhash->append(local_track);
}
else
{
m_vtrack_ptr_alreadyinhash->remove(0);
m_vtrack_ptr_alreadyinhash->append(local_track);
}
}
}
}
}
void QCSurveillance::On_TrackDeleteSignal(const S_TRACK_DELETE_MSG &resp)
{
int number_trackdel = -1;
if (hm->m_gen.m_cwid->hash_for_track.isEmpty() == false)
{
//Find the key in the Hash Table if found then delete
if (hm->m_gen.m_cwid->hash_for_track.contains(resp.track_id) == true)
{
number_trackdel
= hm->m_gen.m_cwid->hash_for_track.remove(resp.track_id);
if (number_trackdel==0)
if (RDSDEBUG)
qWarning("Delete Failed %d", resp.track_id);
else
;
}
else
{
;
}
}
else
{
qDebug("Track Delete warning: No track in the database");
}
}
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