[Qt-interest] QList: Force a deep copy?

Eric Clark eclark at ara.com
Tue Jun 30 16:22:07 CEST 2009


I can understand the reason for "hiding" detach from QList's documentation due to the fact that it is not usually necessary. However, in contrast to that, there are functions in the documentation, i.e. QWidget::setParent, that are documented, but it is stated that "It is very unlikely that you will ever need this function." I see the detach function as being very similar to the QWidget::setParent function in the sense that you will probably never need to use it; however, it should be documented. There may be cases when it needs to be used.

In reply to Diego's question, I would not consider your question to be naïve, it is actually a good question. I have a bug in my application with the QList class that I cannot seem to figure out, and trying to produce a deep copy of the list was a test to try to fix my bug. However, I will point out that the deep copy did not fix my bug.

Basically, I am making a call to QItemSelectionModel::selectedIndexes as so:

void Foo()
{
  QModelIndexList list = mySelectionItem->selectedIndexes();
  ...
}

There is more code in this function, but it crashes even if it is just the one line I have given. My application crashes when it exits the function, and it crashes in QList's destructor. My application replaces the global new and delete operators with nedmalloc instead of the default malloc. We have been doing this for quite some time and have never ran into an issue with anything in Qt, until this. For some reason nedmalloc is stating that some memory is trying to be freed more than once. I do not know why this is happening, but I do know that if I change the allocator back to malloc, everything works fine. However, this is not the ideal solution. We have tested the performance of nedmalloc against malloc and nedmalloc produces much faster results. On the order of about a 200% increase in speed. That is a BIG difference! We need to be able to use nedmalloc in our application because speed is of great importance to us. The only way I can get this function to not crash is to do this:

void Foo()
{
  QModelIndexList *list = new QModelIndexList(mySelectionItem->selectedIndexes());
  ...
}

...and never delete the list. This works only because I don't delete the list. However, that is not a solution since it can be very costly having this type of "forced" memory leak. I thought that if I tried to do a deep copy instead, it might fix the problem, but it did not.

Any help with this issue would be greatly appreciated. Especially if anyone has attempted to use nedmalloc in their Qt application.

Thanks,
Eric

-----Original Message-----
From: qt-interest-bounces at trolltech.com [mailto:qt-interest-bounces at trolltech.com] On Behalf Of Diego Schulz
Sent: Monday, June 29, 2009 8:53 PM
To: qtInterest
Subject: Re: [Qt-interest] QList: Force a deep copy?

On Mon, Jun 29, 2009 at 8:10 PM, Scott Aron
Bloom<Scott.Bloom at sabgroup.com> wrote:
> But to answer Eric's original question :)  It should be.. If you are at
> QList, there really is no obvious way to know or learn about detach via
> the docs.
>
> I would file a bug in the documentation...
> Scott
>

I think there might be good reasons for not including a reference to
detach() in the QList documentation.
But admittedly, I'm not sure.

Looking at src/corelib/tools/qlist.h  (HEAD
@fbfa69a5f71a415dfa9673a6325e0fc0023a2389)

---snip---snip---
 66    ...
 67         QBasicAtomicInt ref;
 68         int alloc, begin, end;
 69         uint sharable : 1;
 70         void *array[1];
 71     };
 72     enum { DataHeaderSize = sizeof(Data) - sizeof(void *) };
 73
 74     Data *detach(); // remove in 5.0                <<<<<< look at this
 75     Data *detach2();
 76     void realloc(int alloc);
 77     static Data shared_null;
 78     Data *d;
 79     void **erase(void **xi);
 80     void **append();
 81     ...
--- snip --- snip ---


Naive cuestion.. in which circumstances it is necesary to make a deep
copy of an instance of an implicitly shared class?

regards,

diego

_______________________________________________
Qt-interest mailing list
Qt-interest at trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-interest




More information about the Qt-interest-old mailing list