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

Eric Clark eclark at ara.com
Thu Jul 2 18:47:36 CEST 2009


Thiago,

If you had followed all of my messages on the board, you would know that I have taken your advice. I am trying to get the vcproj files for Qt built and am having issues. The reason for getting the vcproj files was so that I could compile nedmalloc into Qt, like you suggested. I am not one to just sit back and do nothing. I like to knock out all possible fixes. The best fix would be one that does not require me to have to rebuild Qt with nedmalloc in it, but if that is the only thing that is going to work, then that is what I will do. I would have done this already if I could figure out what my problem is with trying to get the vcproj files built. Thank you for your response, but please read all of my messages before saying that I have "ignored" your suggestion.

Eric

-----Original Message-----
From: qt-interest-bounces at trolltech.com [mailto:qt-interest-bounces at trolltech.com] On Behalf Of Thiago Macieira
Sent: Thursday, July 02, 2009 2:06 AM
To: qt-interest at trolltech.com
Subject: Re: [Qt-interest] QList: Force a deep copy?

Samuel Rødal wrote:
>Eric Clark wrote:
>> I don't typically compile Qt. Usually, I use the binaries that have 
>> already been compiled. It may come to me having to do that though. I 
>> did notice in the QList class that they are use the placement new 
>> operator. Does anyone know what Qt expects this to do?
>>
>> template <typename T>
>> Q_INLINE_TEMPLATE void QList<T>::node_construct(Node *n, const T &t) 
>> {
>>     if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) n->v = new 
>> T(t); else if (QTypeInfo<T>::isComplex) new (n) T(t);
>>     else *reinterpret_cast<T*>(n) = t; }
>>
>> In the second line of the function:
>>
>> else if (QTypeInfo<T>::isComplex) new (n) T(t);
>>
>> Qt is using the placement new operator here. We do not overload this 
>> operator in our stuff. What does Qt expect it to do? Does it expect 
>> realloc to get called, or does it just expect "n" to be returned?
>>
>> Thanks,
>> Eric
>
>Placement new simply initializes an instance of T in the given memory 
>area, calling the constructor in the process. No new memory is 
>allocated. See  
>http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.10
>
>Also, when placement new is used delete isn't called, but the 
>destructor is called explicitly, like this in QList's case:
>
>reinterpret_cast<T*>(n)->~T()
>
>So it doesn't sound like placement new should cause any problems in 
>combination with nedmalloc, since it doesn't do any memory allocation 
>by itself.

Correct. However, QList does allocate/deallocate memory for large (larger than sizeof(void*)) or static types:

template <typename T>
Q_INLINE_TEMPLATE void QList<T>::node_construct(Node *n, const T &t) {
    if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) n->v = new T(t);
    else if (QTypeInfo<T>::isComplex) new (n) T(t);
    else *reinterpret_cast<T*>(n) = t;
}

Eric was having problems with QModelIndexList, which is QList<QModelIndex>. QModelIndex is movable (non-static), but is larger than void*. So the first line of the node_construct function applies.

The problem is nedmalloc. Eric, on Saturday, you had already asked his question. And I replied on Sunday that he should rebuild all his dependencies using nedmalloc, or use it nowhere (see Message-id: 
<200906280039.40821.thiago.macieira at trolltech.com>).

Yet Monday you had ignored my advice and tried to find a workaround that is obviously not working.

--
Thiago Macieira - thiago.macieira (AT) nokia.com
  Senior Product Manager - Nokia, Qt Software
      Sandakerveien 116, NO-0402 Oslo, Norway




More information about the Qt-interest-old mailing list