Fail to call size() from QVector



  • Hello,

    i´ve got a runtime error when i´m trying to call QVector::size() in line 89:
    inline int size() const { return d->size; }

    I try to call it from an other class a QVector<class*>
    first time it works and a seccond time i get a error.

    maybe some solution?



  • Post the stack trace at the crash please



  • here the stack trace, maybe someone can help?

    1 QVector<Anfrage *>::size qvector.h 89 0x465608
    2 Anfragen::anfsize Anfragen.cpp 237 0x424e5a
    3 Funktionen::savelocal Funktionen.cpp 103 0x42d0ce
    4 AnfrAnzeigen::openKontaktmenu AnfrAnzeigen.cpp 212 0x402ecd
    5 AnfrAnzeigen::qt_static_metacall moc_AnfrAnzeigen.cpp 78 0x4376af
    6 QMetaObject::activate qobject.cpp 3740 0x6bb05782
    7 QMetaObject::activate qobject.cpp 3602 0x6bb050a2
    8 QAbstractButton::clicked moc_qabstractbutton.cpp 307 0x13f061d1
    9 QAbstractButtonPrivate::emitClicked qabstractbutton.cpp 411 0x13f03f33
    10 QAbstractButtonPrivate::click qabstractbutton.cpp 404 0x13f03ed0
    11 QAbstractButton::mouseReleaseEvent qabstractbutton.cpp 1009 0x13f0520d
    12 QWidget::event qwidget.cpp 8785 0x13e36e5d
    13 QAbstractButton::event qabstractbutton.cpp 966 0x13f05082
    14 QPushButton::event qpushbutton.cpp 679 0x13f9c06e
    15 QApplicationPrivate::notify_helper qapplication.cpp 3799 0x13dffb70
    16 QApplication::notify qapplication.cpp 3273 0x13dfd8b8
    17 QCoreApplication::notifyInternal2 qcoreapplication.cpp 988 0x6badc6e7
    18 QCoreApplication::sendSpontaneousEvent qcoreapplication.h 234 0x1417c865
    19 QApplicationPrivate::sendMouseEvent qapplication.cpp 2769 0x13dfc563
    20 QWidgetWindow::handleMouseEvent qwidgetwindow.cpp 617 0x13e4fb2a
    ... <More>



  • @Allerknappe said in Fail to call size() from QVector:

    89:

    I'm afraid I don't see any code!



  • the code is inside the qvector.h at line 89: inline int size() const { return d->size; }
    (it stands under line 89 in my earlier post)
    it stopped at this point with Segmentation fault ( Signal SIGSEGV)

    i try to call the size of the vector in class Anfragen with a getter method, but it seems that it will not work -.-



  • When a Segmentation fault is detected, it catches it inside the qvector.h even though the error is in your code...Is Anfragen your own implementation?



  • Hi @Allerknappe

    2 Anfragen::anfsize Anfragen.cpp 237

    Please show us your function that is around line 237 in Anfragen.cpp

    Thanks.



  • so i want to explain my steps:

    In stack trace #4 AnfrAnzeigen::openKontaktmenu{
    calling
    Funktionen *funk = new Funktionen();
    Anfragen *anf = new Anfragen();
    funk->savelocal(anf);
    }
    so the method savelocal will call in stack trace #3 Funktionen::savelocal(Anfragen anf){
    for(qint32 i = 0; i < anf->anfsize(); i++)
    {
    ....
    }
    }
    now i call the anfsize method from stack trace #2 Anfragen::anfsize{
    return anf.size();
    }
    anf is a QVector<Anfrage
    > type which is declared in the class Anfragen. Anfrage is an also self created class.

    so when i try to get the size or call isEmpty() i get the runtime error.
    strange is, that a previous call give me the size back and the next call gives me a runtime error.


  • Lifetime Qt Champion

    Hi,

    Can you show the complete code ?



  • so here a part of the code, which is propably part of the problem..

    class AnfrAnzeigen : public QWidget, private Ui::AnfrAnzeigen
    {
        Q_OBJECT
        public:
            AnfrAnzeigen(const QRect winSize,Profil *prof);
            AnfrAnzeigen(const QRect winSize,Profil *prof, Anfragen *anf);
            ~AnfrAnzeigen() {}
        protected slots:
            void no(qint32 id);		//Anfrage ablehnen
            void yes(qint32 id);			//Anfrage zustimmen
            void openKontaktmenu();
        private:
            Funktionen *funk;
            Kontakte *kon;
            Profil *prof;
            Anfragen *anf;
    
    };
    
    void AnfrAnzeigen::openKontaktmenu()
    {
    	Chats *chat = new Chats();
    	Termine *ter = new Termine();
    	Terminanfragen *teranf = new Terminanfragen();
    	Terminnachrichten *terna = new Terminnachrichten();
    	funk->savelocal(prof, anf, chat, ter, teranf,terna);
          this->close();
        Kontaktmenu *KoMenu = new Kontaktmenu(this->geometry());
        KoMenu->show();
    }
    
    void Funktionen::savelocal(Profil *prof, Anfragen *anf, Chats *chat, Termine *ter, Terminanfragen *teranf, Terminnachrichten *terna)	// zum generieren und speichern der lokalen datei
    {
        QFile file("user.dat");
    	file.remove();
    
        Profil *pro = prof;
        Anfragen *an = anf;
        Chats *cha = chat;
        Termine *te = ter;
        Terminanfragen *teran = teranf;
        Terminnachrichten *tern = terna;
    	
    	
    	
    	
    	
        bool ok = file.open(QIODevice::ReadWrite);
        if(ok==0)
            return;
        QTextStream write(&file);
        
        write << "Anfragen:\r\n" << endl;
    
        for(qint32 i = 0; i < an->anfsize(); i++)
            {
               write << QVariant(anf->anf[i]->getanfragendeID()).toString()+"\r\n" << endl;
               write << anf->anf[i]->getgrund()+"\r\n" << endl;
              write << anf->anf[i]->getdate()+"\r\n" << endl;
            }
        ...
    }
    
    qint16 Anfragen::anfsize()
    {
    	return anf.size();
    }
    
    class Anfragen 
    {
    
    	
        public:
    	
        Anfragen();
        void addAnfrage(qint32 anfragendeID, QString grund, QString date);
        void deleteAnfrage(qint32 position);
    	qint16 anfsize();
    	~Anfragen();
    		
        private:
    	QVector<Anfrage*> anf;
    };
    

    [Added code tags ~kshegunov]


  • Qt Champions 2016

    First off, my advice is not to use classes that are differentiated only by number - Anfrage and Angragen is mighty confusing to read in code. Secondly, it's a good idea, at least as long as we are talking programming, to use english for both class' names and comments. It's a tragic fact of life that code is much more often read than written and using German actually shrinks the amount of people who can read and understand it.

    As for your original question, to get that kind of segmentation fault the problem is almost universally usage of a wild pointer. Your AnfrAnzeigen::anf, I'm sure, points to nowhere when you call Anfragen::anfsize(), which references Anfragen::anf, which in turn has an invalid address, and ultimately you get the segfault in the QVector::size(). Look for the place where you delete AnfrAnzeigen::anf and from there on trace the place where you call Anfragen::anfsize() after the object had been deleted.



  • thank you for the answer.
    to first: yes your right with the english description, but as i start to writing this code i was not thinking about it. at some point i took english description but the old german description is still there.

    to the second:
    yes your right, at some point i destruct the anfragen class and so i the vector is uninitialised. if i thinking about it it´s a simple mistake-.-

    so at least:
    Thank your for your answer!


Log in to reply
 

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