PDA

View Full Version : QList of pointers



Farcaller
23rd January 2006, 20:08
I have such a code:

static QList<display_list*> displayLists;
items are added this way:

list = new display_list();
list->new_list(GL_COMPILE);
glBegin(GL_QUADS);
...
glEnd();
list->end_list();
displayLists.append(list);
The problem is if I need to clear list and generate otehr display lists than displayLists.clear() is a bad choice - ~display_list() is not called for elements, in fact lists still reside in OpenGL memory. Currently I have such a header in function, that generates this list:

void prepareLists()
{
if(!displayLists.isEmpty())
foreach(display_list* ptr, displayLists) delete ptr;
displayLists.clear();
...
but I don't like it :confused:
Any ideas how to make it nice&clean?

PS: display_list is not a QObject

Farcaller
23rd January 2006, 20:13
hmm... will

class MyQList: public QList
{
void QCollection::deleteItem ( Item d ) {
delete d;
::deleteItem(d);
}
}
do the job?

Codepoet
23rd January 2006, 20:17
Do you know boost? You could use boost::shared_ptr. Please don't try this with std::auto_ptr because std:auto_ptr becomes invalid after reconstruction in / by QList.

wysota
23rd January 2006, 20:28
void prepareLists()
{
qDeleteAll(displayLists);
displayLists.clear();
//...

Farcaller
24th January 2006, 17:48
I've moved to boost::shared_ptr, Codepoet thanks for the idea (I'm boost user for several weeks, don't remember all its features ;) )