You are not logged in.

1

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;
}IMConversationList_t;

class IMConversationList : public QAbstractListModel
{
Q_OBJECT
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;
public:
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);

signals:
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";
removeAllEntryFromIMCoversationList();
return;
}

// begin reset mode
beginResetModel();
// Clear IM COnversation Model
if(!mIMConversationModelList.isEmpty())
{
beginRemoveRows(QModelIndex(), 0, msgCnt - 1);
for(int row = 0; row < msgCnt ;row++)
mIMConversationModelList.removeAt(0);
endRemoveRows();

}

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

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

if(imConvList[index].status == UI_IM_STATUS_DRAFTED)
{
emit sigDraftedMessage((QString)imConvList[index].textMessage);
continue;
}
//Add the received data in to the list
beginInsertRows(QModelIndex(),index,index);
mIMConversationModelList.append(&imConvList[index]);
endInsertRows();
}

any Ideas Wellcome

2

Wednesday, June 19th 2013, 1:58pm

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

pitonyak

Trainee

  • "pitonyak" is male

Posts: 58

Location: Columbus, Ohio, USA

  • Send private message

3

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

1
IMConversationList_t imConvList[SUPPORTED_IM_MAX];


Next, you clear the memory used by the array

Source code

1
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