You are not logged in.

Dear visitor, welcome to If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.


Friday, June 14th 2013, 12:08pm

Memory Leak Issue while create List View using QAbstactListModel and QList

I an trying to create List view in Qt using QAbstractListModel and Qlist. If i Create a list view first time then it will allocated memory which is required but once i clear by remove the rows of that model than allocated memory should be freed but it wasn't and again when i append new entry to that model than again new memory will be allocated for that entry. here i want to know when i clear the model using clear member function of Qlist then how to free the allocated memory, it will freed by itself after destructor called ?
and if i want to free that memory forcefully than how can i?

Here is code Sinnepet

typedef struct{
char name[CONTACT_NAME_LEN_MAX + 1];
char number[CONTACT_NUM_LEN_MAX + 1];
quint16 index;
UiIM_Status_e status;
char timeStamp[IM_TIMESTAMP_LEN_MAX];
char textMessage[IM_TEXT_MSG_LEN_MX];
BOOL unReadMsgFlag;

class IMConversationList : public QAbstractListModel
static const int SendReceiveFlagRole;
static const int NameRole;
static const int NumberRole;
static const int IndexRole;
static const int StatusRole;
static const int TimeStampRole;
static const int TextMessageRole;
static const int UnReadMsgFlagRole;
QList<IMConversationList_t *> mIMConversationModelList;
explicit IMConversationList(QObject *parent = 0);
int rowCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;

void getIMConversation(QString number);
void removeAllEntryFromIMCoversationList();
void appendNewEntry(UINT16 imIndex);
void updateConversationByIndex(quint16 index, UiIM_Status_e status);
void refreshView();
bool indexOfContact(quint16 index, quint16 *listIndex);

void sigDraftedMessage(QString draftMessage);

public slots:

void IMConversationList::getIMConversation(QString number)

IMConversationList_t imConvList[SUPPORTED_IM_MAX];
UINT16 index, msgCnt = 0;
Contact contactInfo;
InstantMessage imInfo;
QString name;
PresencePublishStatus_e preStatus = PRESENCE_PUBLISH_STATUS_MAX;
char statusNote[PRESENCE_NOTE_LEN_MAX + 1];

qDebug()<< " *********************** In getconversation number " << number;

memset(imConvList, 0, (sizeof(IMConversationList_t) * SUPPORTED_IM_MAX));

// using this function is get data in imConvList Structure array
if(FALSE == getIMConvfromDb(number.toLatin1().data(), imConvList, &msgCnt))
qWarning() << objectName() << "Failed to read Instant Message Conversation from Database";

// begin reset mode
// Clear IM COnversation Model
beginRemoveRows(QModelIndex(), 0, msgCnt - 1);
for(int row = 0; row < msgCnt ;row++)


qDebug() << "mIMConversationModelList.isEmpty() = " << mIMConversationModelList.isEmpty();
for(index = 0; index < msgCnt; index++)
if((strlen(imConvList[index].number) == 0))

strcpy(imConvList[index].name, name.toLatin1());

if(imConvList[index].status == UI_IM_STATUS_DRAFTED)
emit sigDraftedMessage((QString)imConvList[index].textMessage);
//Add the received data in to the list

any Ideas Wellcome


Wednesday, June 19th 2013, 1:58pm

Hello Friends, Please give suggestion if u have any idea about this problem.......



  • "pitonyak" is male

Posts: 58

Location: Columbus, Ohio, USA

  • Send private message


Friday, June 20th 2014, 8:00pm

You mention that data should be deleted / set free in a destructor, but I don't see any destructors defined in your code, nor do I see a new or delete.

Where is the memory allocated that you think should be removed? I found the code difficult to follow. For example, you define a local variable

Source code

IMConversationList_t imConvList[SUPPORTED_IM_MAX];

Next, you clear the memory used by the array

Source code

memset(imConvList, 0, (sizeof(IMConversationList_t) * SUPPORTED_IM_MAX));

So far, nothing has been created that can be deleted.

You show some local variables that are referencing the address of (pointers into) an array that is defined locally (and not allocated using new). I think that I am missing something.

Similar threads