Я создал этот 2D Qlist. Код «работает», но когда я рассматриваю немного более глубокий синтаксис, мне остается задаться вопросом, приводит ли этот код к утечке памяти.2d QList Memory Leak
Было бы здорово, если бы кто-то мог пролить свет на то, приводит ли этот код к утечке памяти или нет. Если это так, ПОЧЕМУ и как исправить?
Мои особые вопросы касаются вопросов, отмеченных как Q1, Q2, Q3, Q4 в комментариях кода.
Также, как я должен уничтожить некоторые строки из lszq2DList и в конечном итоге, как я должен уничтожить lszq2DList.
//The 2d data sructure. Each row will be a QStringList.
QList<QStringList> lszq2DList;
//The variable "emptyTempList" is a concern to me.
//Q1:: Isn't "emptyTempList" just a pointer to some allocated place in memory?
//If so, I imagine it points to some data structure allocated in memory.
//Or does this not allocate anything? I have a feeling it does not.
QStringList emptyTempList; //I use it in my scheme to make a 2D data struct.
for(int i=0; i<3; i++){
//Q2: Because I am in a loop and reusing the same pointer "emptyTempList",
//aren't i pointing to the same thing?
//If I think about this, each item in my Qlist (each row) should be
//pointing to the same place in memory. To whatever "emptyTempList"
//points to.
//Of course, this is not the case. What is really going on in line bellow?
lszq2DList += emptyTempList; //I'm no really pointing to anything am I?
QString szqTempString; //Q3: Gets trashed when out of scope?
for(int j=0; j<3; j++){
szqTempString.clear();
szqTempString = "jazz";
//QString value gets copied into datastructure?
lszq2DList[i] += szqTempString;
}
}
QStringList emptyTempList2;
emptyTempList2 += "blues";
emptyTempList2 += "blues";
emptyTempList2 += "blues";
//I'll add another row.
lszq2DList += emptyTempList2;
//Q4: lszq2DList[3] does not point to emptyTempList2, right?
//Instead it copies all the strings
//from to emptyTempList2 to lszq2DList[3], right?
Ваш размещенный код даже не имеет распределения памяти, как он может иметь какие-либо утечки? –