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. Cannot work out why this segfaults
Qt 6.11 is out! See what's new in the release blog

Cannot work out why this segfaults

Scheduled Pinned Locked Moved General and Desktop
14 Posts 3 Posters 7.1k Views 1 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.
  • JKSHJ Offline
    JKSHJ Offline
    JKSH
    Moderators
    wrote on last edited by
    #4

    Hi, and welcome to the Qt Dev Net!

    [quote]this->test is not NULL. I can get an address in memory with &this->test.[/quote]This alone isn't enough to prove that your pointer is OK. Uninitialized or dangling pointers can be non-NULL too.

    (If you haven't assigned anything to the pointer, it's uninitialized. If you assign an object to a pointer but then destroy the object, the pointer is dangling. These concepts apply to C pointers too.)

    How did you create your TestClass object?

    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #5

      I am worried the garbage collector is destroying the object or something.

      TestClass is created in a QList.

      @function QList<TestClass> *buildTests()
      {
      QList<TestClass> *tests = new QList<TestClass>();
      TestClass test = new TestClass("Hello World");
      tests->append(*test);
      return tests;
      }@

      I have a class later that takes this QList, iterates in a drop down menu. Later when the user activates the button clicked slot:

      @
      TestClass *testreturn;
      for (int i = 0; i < testList->count(); i++)
      {
      TestClass test= devicesList->at(i);
      if (test.returnTestString() == ui->testSelectionBox->currentText())
      {
      testreturn = &test;
      }
      // Later on:
      emit(test);
      @

      1 Reply Last reply
      0
      • jeremy_kJ Offline
        jeremy_kJ Offline
        jeremy_k
        wrote on last edited by
        #6

        [quote author="sn1994" date="1409651189"]I am worried the garbage collector is destroying the object or something.
        [/quote]

        Stop worrying! There is no garbage collector, unless you've added one or are using another framework that comes with one.

        I see that the QList contains TestClass objects, rather than pointers to the objects. Does TestClass implement a copy constructor and assignment operator? If not, I wonder if the default implementation is the problem.

        http://www.cplusplus.com/articles/y8hv0pDG/ describes what these are and why you might need to define customized versions.

        Alternatively, change the QList<TestClass> to QList<TestClass *>

        Asking a question about code? http://eel.is/iso-c++/testcase/

        1 Reply Last reply
        0
        • JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #7

          [quote author="sn1994" date="1409651189"]
          @
          TestClass test= devicesList->at(i);
          //...
          testreturn = &test;
          @
          [/quote]
          test is a local variable. testreturn is a pointer that points to that local variable.

          test will be destroyed when it goes out of scope.

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          0
          • jeremy_kJ Offline
            jeremy_kJ Offline
            jeremy_k
            wrote on last edited by
            #8

            [quote author="JKSH" date="1409655317"][quote author="sn1994" date="1409651189"]
            @
            TestClass test= devicesList->at(i);
            //...
            testreturn = &test;
            @
            [/quote]
            test is a local variable. testreturn is a pointer that points to that local variable.

            test will be destroyed when it goes out of scope.[/quote]

            Good point. There's also the emit(test) which may not be what is intended.

            Asking a question about code? http://eel.is/iso-c++/testcase/

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #9

              Sorry, I am doing emit (testreturn). I had to make my code clean to post here, so that is just a mistake in posting.

              Should I make testreturn a public: variable and use memcpy to copy it? Oddly the first slot is OK with receiving the TestClass*, it is only later when I try and reference the global version I assign with this.test it crashes

              1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #10

                p.s. I see your suggestion JKSH

                At the moment I have QList<TestClass> testlist*, do you mean change it to

                QList<TestClass*> testlist*

                or QList<TestClass*> testlist

                Thanks!

                1 Reply Last reply
                0
                • JKSHJ Offline
                  JKSHJ Offline
                  JKSH
                  Moderators
                  wrote on last edited by
                  #11

                  [quote author="sn1994" date="1409657238"]Sorry, I am doing emit (testreturn).[/quote]That doesn't work either: You need to emit a signal. What is the name of your signal?

                  [quote]Should I make testreturn a public: variable and use memcpy to copy it?[/quote]No, avoid using public variables. Also avoid using low-level functions like memcpy and malloc -- use the C++ equivalents instead (e.g. std::copy, new)

                  [quote author="sn1994" date="1409657238"]Oddly the first slot is OK with receiving the TestClass*, it is only later when I try and reference the global version I assign with this.test it crashes[/quote]It's still unclear to me what exactly your code is doing. Can you please post a minimal but complete example? Strip out all the parts that don't contribute to the segfault, and then post the full code for main(), MainWindow, and TestClass.

                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                  1 Reply Last reply
                  0
                  • ? Offline
                    ? Offline
                    A Former User
                    wrote on last edited by
                    #12

                    I can show you the proper code, I notice you are on Freenode. I tried to whois you but apparently you are not online.

                    1 Reply Last reply
                    0
                    • JKSHJ Offline
                      JKSHJ Offline
                      JKSH
                      Moderators
                      wrote on last edited by
                      #13

                      You can post it here :) I haven't been on Freenode for a long time; I should probably update my profile.

                      [quote author="sn1994" date="1409657846"]p.s. I see your suggestion JKSH

                      At the moment I have QList<TestClass> testlist*, do you mean change it to

                      QList<TestClass*> testlist*

                      or QList<TestClass*> testlist

                      Thanks![/quote]The suggestion actually came from jeremy_k. I believe he meant QList<TestClass*> testlist

                      You almost never need to allocate a QList itself using new. It is an implicitly shared class, so we just copy it around or pass it by const-reference. See http://qt-project.org/doc/qt-5/implicit-sharing.html for details.

                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                      1 Reply Last reply
                      0
                      • jeremy_kJ Offline
                        jeremy_kJ Offline
                        jeremy_k
                        wrote on last edited by
                        #14

                        Yes, please. Post here. This allows future readers to benefit from the conversation. It also puts a little pressure on posters to write brief, on point samples.

                        Confirmation on QList<TestClass *> testList.

                        Having a list of pointers makes it clear that fetching an item, manipulating it, and then going out of scope:

                        • will not create a new object,
                        • will modify the original in the list
                        • will not cause the object (original or copy of) to be destroyed

                        Asking a question about code? http://eel.is/iso-c++/testcase/

                        1 Reply Last reply
                        0

                        • Login

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