Qt-interest Archive, March 2002
QVector
Message 1 in thread
What is wrong with this code? It's cauding an Access Violation error.
QVector<TypeRecord> TypeRecordManager::getAll()
{
QVector<TypeRecord> records(2);
TypeRecord rec;
rec.setId(100);
rec.setName("Loaded record");
records.insert(0, &rec);
records.insert(1, &rec);
qWarning( records.isNull() ? "EMPTY2" : "NOT EMPTY2" );
return records;
}
...
TypeRecordManager manager;
TypeRecord* record = manager.getAll()[0];
...
Thanks,
--
[ signature omitted ]
Message 2 in thread
You are placing a pointer to your TypeRecords object inside a vector.
That typerecords object is temporary and exists only for the life of the
function TypeRecordManager::getAll().
You need to, instead, consider using maybe the stl vector (#include
<vector>), and not use pointers to temporaries, but actual objects in
your vector (something QVector can't handle, as I understand...).
I would re-write this function as follows:
#include <vector>
using namespace std;
...
vector<TypeRecord> TypeRecordManager::getAll()
{
vector<TypeRecord> records;
TypeRecord rec;
rec.setId(100);
rec.setName("Loaded record");
records.push_back(rec);
records.push_back(rec);
qWarning( records.size() ? "EMPTY2" : "NOT EMPTY2" );
return records;
}
On Fri, 22 Mar 2002, Eddy Young
wrote:
> What is wrong with this code? It's cauding an Access Violation error.
>
> QVector<TypeRecord> TypeRecordManager::getAll()
> {
> QVector<TypeRecord> records(2);
>
> TypeRecord rec;
> rec.setId(100);
> rec.setName("Loaded record");
>
> records.insert(0, &rec);
> records.insert(1, &rec);
>
> qWarning( records.isNull() ? "EMPTY2" : "NOT EMPTY2" );
>
> return records;
> }
>
> ...
>
> TypeRecordManager manager;
> TypeRecord* record = manager.getAll()[0];
> ...
>
> Thanks,
>
>
Message 3 in thread
On Friday 22 Mar 2002 5:34 pm, Calin A. Culianu wrote:
> You need to, instead, consider using maybe the stl vector (#include
> <vector>), and not use pointers to temporaries, but actual objects in
> your vector (something QVector can't handle, as I understand...).
Thanks, Calin. Paul's solution is already working. I would use the STL
vector class, but someone recommended that I use Qt container classes
preferably. Any criteria as to when I should use STL when developing an
application with Qt?
--
[ signature omitted ]
Message 4 in thread
Qvector is a vector of pointers. You're inserting two records into it
that point to a local variable. When getAll() returns, the vector is
copied by QVector's copy constructor but the pointers point still into
the current stack frame. On return, that stack frame vanishes, but the
copied container still has pointers into the old stack frame. When you
dereference the pointers held in the container, you'll get an AV.
Try allocating the records using new rather than allocating them on the
stack and set the auto-delete flag on the container to clean up.
-- Paul.
> -----Original Message-----
> From: Eddy Young [mailto:jeyoung@priscimon.com]
> Sent: 22 March 2002 11:11
> To: Qt Interest
> Subject: QVector
>
>
> What is wrong with this code? It's cauding an Access Violation error.
>
> QVector<TypeRecord> TypeRecordManager::getAll()
> {
> QVector<TypeRecord> records(2);
>
> TypeRecord rec;
> rec.setId(100);
> rec.setName("Loaded record");
>
> records.insert(0, &rec);
> records.insert(1, &rec);
>
> qWarning( records.isNull() ? "EMPTY2" : "NOT EMPTY2" );
>
> return records;
> }
>
> ...
>
> TypeRecordManager manager;
> TypeRecord* record = manager.getAll()[0];
> ...
>
> Thanks,
>
> --
> Eddy Young - jeyoung@priscimon.com - Tel: +44 (0)20 8337 6020
>
> --
> List archive and information: http://qt-interest.trolltech.com
>
Message 5 in thread
Quoting Paul Curtis <plc@rowley.co.uk>:
> Try allocating the records using new rather than allocating them on
> the
> stack and set the auto-delete flag on the container to clean up.
Thanks, Paul. That's working fine now. I was confused about the
different types of container classes. So, I guess it's back
to "Programming in Qt" ;-)
--
[ signature omitted ]
Message 6 in thread
Eddy,
I think the primary advantage of the QT containers is that they are portable
for any system you have QT on. The STL is supposed to be a 'standard' of
sorts, but not all C++ compilers currently support it. If portability isn't
a big issue for you and if you have the STL available you might want to use
it instead.
Craig
> ----------
> From: Eddy Young[SMTP:jeyoung@priscimon.com]
> Reply To: jeyoung@priscimon.com
> Sent: Friday, March 22, 2002 4:32 PM
> To: calin@ajvar.org
> Cc: Qt Interest
> Subject: Re: QVector
>
> On Friday 22 Mar 2002 5:34 pm, Calin A. Culianu wrote:
>
> > You need to, instead, consider using maybe the stl vector (#include
> > <vector>), and not use pointers to temporaries, but actual objects in
> > your vector (something QVector can't handle, as I understand...).
>
> Thanks, Calin. Paul's solution is already working. I would use the STL
> vector class, but someone recommended that I use Qt container classes
> preferably. Any criteria as to when I should use STL when developing an
> application with Qt?
> --
> Eddy Young - jeyoung@priscimon.com
>
> --
> List archive and information: http://qt-interest.trolltech.com
>