New QVector<CustomClass *>() segmentation fault.



  • Hello,

    I'm trying to create a new QVector object with template objects:

    @
    QVector<CustomClass *> *collection; //That's in class declaration.
    ...
    collection = new QVector<CustomClass *>(); //In constructor code, in order to init my vector
    @

    It crashes at runtime (Segmentation fault) when creating a new instance (collection = new QVector...). Any idea?
    I really want to use a pointer to my QVector.

    Thank you.


  • Lifetime Qt Champion

    Hi,

    Generally you don't need to allocate QVector object on the heap. Just use QVector<CustomClass *> collection.

    What is the output of the debugger ?



  • Hello, thank you for your fast reply.

    Well, I need this QVector pointer for other instances than the owner one. It means that I ve my owner like that:

    @
    QVector<CustomClass*> collection;
    @

    And the others that need some information registered in the QVector, declared as follow (as a pointer):

    @
    QVector<CustomClass*> *collection;
    @

    I want to avoid have multiple copies of the same information. Well, when I use my setter in the non-owner classes:

    @
    //Called from QVectorOwner
    object->setVector(&collection);

    //Inside the setter:
    void TableChromoDelegate::setDataVector(QVector<CustomClass *> *data)
    {
    this->collection = data;
    }

    @

    This setter works in most non-owner classes, but one. There is one where it crashes in pointer assignation:

    @
    void TableChromoDelegate::setDataVector(QVector<CustomClass *> *data)
    {
    qDebug() << data->count(); //It works
    this->collection = data; //Crash here: segmentation fault. How it comes It crashes when I'm just assigning???
    }

    //Header declaration is the same in all the non-owner classes:
    protected:
    QVector<CustomClass*> *collection;
    @

    So strange...


  • Lifetime Qt Champion

    QVector is implicitly shared, so until you modify it there's no copy done.

    But anyway, since you want to use it in many places, shouldn't you rather use the MVC pattern offered by Qt ? QAbstractItemViewModel and friends might be a better choice to share you data


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.