PDA

View Full Version : warning in QDataStream template



tuli
28th March 2014, 12:59
I get a warning in the "QDataStream& operator>>(QDataStream& s, QVector<T>& v)" template function:

10198


how can i resolve that warning? Unfortunately, i am unable to pin it down to a specific overload of the operator. It looks to me that at one point a QVector<> of pointers to serializable objects is serialized, but it seems to work ok.


Qt: 4.8.4
os: xp sp3
ide: VS2008


thanks.

anda_skoa
30th March 2014, 12:40
What is the error message?
What is your T?
Can it be default constructed?

Cheers.
_

ChrisW67
31st March 2014, 00:58
There is no error message but a warning is displayed in the screen shot.
"warning C4700: uninitialized local variable 't' used"
Apparently this is treated as an error in MSVC 2012+, although that does not apply here.

It seems erroneous to me given that t is of type T and constructed on the line immediately before the offending stream operator. If T were not default constructable then the preceding line would be an error if, indeed, you ever managed to compile a declaration of the QVector<T>.

tuli
1st April 2014, 11:58
it compiles just fine, though i feed it to vs2012 later to see if that steers something up. Since the warning is only inside the template code you can see in the screenshot, i do not now what "T" refers to.


edit: hm, looking at the comit log, i would appear someone serializes a Qvector<xxx*>, ie a vector of pointers. Does that work at all? Deserializing seems to be ok.... :S Could it be related?

ChrisW67
3rd April 2014, 06:30
Yes. If you declare a QVector<Thingy*> then "T" is "Thingy*". If you then rely on the default, templated, operator>>() then you get the warning. The code that generates the warning looks like:


template<typename T>
QDataStream& operator>>(QDataStream& s, QVector<T>& v)
{
v.clear();
quint32 c;
s >> c;
v.resize(c);
for(quint32 i = 0; i < c; ++i) {
T t;
s >> t;
v[i] = t;
}
return s;
}

Line 9 and 10 effectively read:


Thingy* t;
s >> t;

Under these circumstances t is indeed not initialised (i.e. it's an invalid pointer). The program might compile but it will crash.

You cannot persist a pointer between sessions and expect to get the object it pointed to back at the other end. You should be able to provide a specific implementation of:


QDataStream& operator>>(QDataStream& s, QVector<Thingy*>& v);
// and
QDataStream& operator<<(QDataStream& s, const QVector<Thingy*>& v);

that the compiler should use in preference to the templated versions. These versions would have to arrange serialisation/deserialisation of the objects pointed to by each of the vector member pointers.

tuli
9th April 2014, 07:05
thanks!


I did supply the two functions and everything seems to work fine now. No more warning.


Interestingly, i tried it several times and the deserialization of the pointer vector seemed to work just fine. :) Which is why i went on to look for the problem somewhere else.

ChrisW67
9th April 2014, 08:11
The compiler will happily let your code write through the invalid pointer assuming there is actually an instance on the end of it. Eventually it will write somewhere that will trigger the termination of your program.