tags:

views:

82

answers:

2

Hi!

I don't have much experience with Qt but somehow I think this is acting strange. Compiled with VS2005:

    class Entry {
    public: Entry(const QString& aName, bool checked) : 
        name(aName), isChecked(checked)
    { 
        // empty 
    };

    public: Entry(const Entry& entry) : 
        name(entry.name), isChecked(entry.isChecked)
    { 
        // empty 
    };

    public: Entry& operator=(const Entry& entry)
    {
        name = entry.name;
        isChecked = entry.isChecked;
        return *this;
    }

    public: QString name;
    public: bool isChecked;
};
typedef QList<conduit::Entry> EntryVector;

When using EntryVector in the following lines, the entry in QList becomes a bad pointer:

void EntryWidget::setEntries(QStringList& names)
{
        QStringList::iterator member;
    EntryVector list;
    for (member = names.begin(); member != names.end(); ++member)
    {
    Entry entry(*member, false);
    list.append(entry);
    }
    m_model.setEntryData(list);
    }

Somehow, entry.name in the list will become a bad pointer. My understanding is that QList should be able to support any data in its template, but for some reason that I yet do not understand this does not work. Everything works just fine if I use a STL Vector.

Any ideas? Thanks

A: 

"entry.name" isn't a pointer - it's declared as a QString, here:

public: QString name;

So, what do you mean by "entry.name will become a bad pointer"?

Most likely, some other part of your program is causing a memory corruption.

davmac
BadPtr is what VS debugger shows when debugging through the setEntries method. No other thread is active, so I do not see any reason why that as soon as the entry is added to the list, and after the copy constructor has been called, the list no longer holds a valid entry for the name. This happens as soon as 'list.append(entry);' is called. The copied Entry in QList just looses the name QString in it.
bmsantos
I've never used the VS debugger, but: I suspect that it's not entry.name which is the bad pointer, but rather entry itself, because it has gone out of scope (it is in scope at the start of the loop, and goes out of scope at the end of the loop; there's a brief point of time between iterations of the loop where it is arguably out-of-scope).Try a more traditional debugging approach - print out the contents of the list to the console after each time you add an entry. I'm sure you'll see that the entries are being added correctly, and the Entry in the QList is not losing the name.
davmac