Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Fail to call size() from QVector

Fail to call size() from QVector

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 6 Posters 3.7k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Allerknappe
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      Post the stack trace at the crash please

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2
      • A Offline
        A Offline
        Allerknappe
        wrote on last edited by
        #3

        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>

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Charlie_Hdz
          wrote on last edited by
          #4

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

          89:

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

          Kind Regards,
          Enrique Hernandez
          gearstech.com.mx
          chernandez@gearstech.com.mx

          1 Reply Last reply
          1
          • A Offline
            A Offline
            Allerknappe
            wrote on last edited by
            #5

            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 -.-

            aha_1980A 1 Reply Last reply
            0
            • C Offline
              C Offline
              Charlie_Hdz
              wrote on last edited by
              #6

              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?

              Kind Regards,
              Enrique Hernandez
              gearstech.com.mx
              chernandez@gearstech.com.mx

              1 Reply Last reply
              1
              • A Allerknappe

                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 -.-

                aha_1980A Offline
                aha_1980A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi @Allerknappe

                2 Anfragen::anfsize Anfragen.cpp 237

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

                Thanks.

                Qt has to stay free or it will die.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  Allerknappe
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Hi,

                    Can you show the complete code ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    1
                    • A Offline
                      A Offline
                      Allerknappe
                      wrote on last edited by kshegunov
                      #10

                      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]

                      kshegunovK 1 Reply Last reply
                      0
                      • A Allerknappe

                        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]

                        kshegunovK Offline
                        kshegunovK Offline
                        kshegunov
                        Moderators
                        wrote on last edited by
                        #11

                        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.

                        Read and abide by the Qt Code of Conduct

                        1 Reply Last reply
                        4
                        • A Offline
                          A Offline
                          Allerknappe
                          wrote on last edited by
                          #12

                          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!

                          1 Reply Last reply
                          1

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved