[Interest] Weird error using Visual C++ 2013

Krzysztof Kawa krzysiek.kawa at gmail.com
Wed Mar 5 13:24:34 CET 2014


2014-03-05 8:59 GMT+01:00 Yves Bailly <yves.bailly at sescoi.fr>:

>
>
> For what I can see, it seems there's some troubles between QString and
> Visual 2013
> initializer-lists implementattion.
>
> Keeping searching...
>
>

This is a bug in VS2013 initializer_list that basically makes
initializer_list useless for anything but simple types.
It's not a problem in QString or Qt for that matter.
Here's a simple example:

struct RefCount {
    RefCount(){ ref = new int; *ref = 1; }
    ~RefCount() { --(*ref); if(*ref == 0) delete ref; }
    RefCount(const RefCount& r) { ref = r.ref; ++(*ref); }
//I ommited operator= and && versions but you can =delete them to make sure
    int* ref;
};

struct Ref {
    Ref(const RefCount& r) : ref(r) {}
    RefCount ref;
};

int main() {
    //that's ok
    std::initializer_list<RefCount> refs1 {
        RefCount(),
        RefCount(),
        RefCount(),
        RefCount()
    };

    //that's not
    std::initializer_list<Ref> refs2 {
        Ref(RefCount()),
        Ref(RefCount()),
        Ref(RefCount()),
        Ref(RefCount())
    };

    //step into debugger here
    return 0;
}
//the debugger shows:
(refs1)._First+0   ref=1
(refs1)._First+1   ref=1
(refs1)._First+2   ref=1
(refs1)._First+3   ref=1

(refs2)._First+0   ref.ref=<garbage>
(refs2)._First+1   ref.ref=<garbage>
(refs2)._First+2   ref.ref=2
(refs2)._First+3   ref.ref=2

There's a copy construction in there so the ref count is briefly 2.
It seems it double-deletes the first half of the list and doesn't delete
the second (I checked with different number of elements, it's always like
that).
You can see the same with your class and QString. It's garbage for the
first half and QString's atomic counter is 2 for the other half.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20140305/d7698c39/attachment.html>


More information about the Interest mailing list