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. [SOLVED] Some questions about memory leaks
QtWS25 Last Chance

[SOLVED] Some questions about memory leaks

Scheduled Pinned Locked Moved General and Desktop
15 Posts 3 Posters 4.5k Views
  • 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.
  • N Offline
    N Offline
    nicky j
    wrote on 19 Jun 2014, 21:13 last edited by
    #3

    bq. What you could be doing however is e.g. allocate something on the heap, give the pointer to that thing somewhere else, delete the object pointed to it in yet another place which will leave you with dangling pointers. The next time you use that point you’ll be trying to reach a memory region that is already freed hence a segmentation fault.

    What is this called? Sifting through thousands of lines of code to find dangling pointers seems nigh impossible, is there a quicker way to find them? Once I find them how can I fix them? What else could be an explanation for the QList causing the program to crash?

    Thanks!

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 19 Jun 2014, 21:34 last edited by
      #4

      Bad memory management ;)

      One thing you can use on your code is a static analysis tool like cppcheck. It should point you to some errors on your code. IIRC it will also warn you if you are allocating things and not deleting them. BUT, it cannot guess that some memory you are allocating in one class can be release in another one. However it should help you improve things.

      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
      0
      • C Offline
        C Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on 19 Jun 2014, 21:38 last edited by
        #5

        Like SGaist said what you describe is memory corruption, not a memory leak.
        Memory leak occurs when any part of memory you allocated becomes inaccessible (nothing pointing to it anymore). Most of the cases is caused by forgotten delete.
        Memory corruption occurs when you overwrite by mistake something you're not suppose to and this makes app behavior unpredictable. These are the hardest to find, because their symptoms usually are random or occur in totally unrelated (seemingly) parts of the program.

        There are many types of memory corruption. Some examples:
        @
        int a[3];
        a[3] = 42; //buffer overrun
        int* b;
        b = 42; //use of uninitialized pointer
        int c;
        int d = 2
        c; //use of uninitialized variable
        int* e = sumeFunc();
        e = 42; //use of potentially null pointer
        int
        f = new int();
        delete f;
        *f = 42; //accessing destroyed variable

        //and many more
        @

        My usual method to hunt these is to first fish for any bugs. Often by fixing one bug you fix 3 more you didn't know about and spot one or two other/related.
        There are tools that can help to some extent. For example "VLD":https://vld.codeplex.com/ has been invaluable to me for detecting memory leaks. While not directly causing segfaults, fixing these often lead you to spotting a potential memory corruption. Another useful tool I use is "cppcheck":http://cppcheck.sourceforge.net/ - a tool for static analysis of the code. It can spot many logical, structural and syntactical problems in your code before you even compile it. Depending on the tools you use, your compiler or IDE might offer some help too. For example MSVC offers "SDL":http://msdn.microsoft.com/en-us/library/jj161081.aspx switch to the compiler which helps detect things like use of uninitialized variables and pointers and buffer overrun errors.

        1 Reply Last reply
        0
        • N Offline
          N Offline
          nicky j
          wrote on 19 Jun 2014, 21:44 last edited by
          #6

          Are any of the tools you mentioned integrated into Qt Creator? If not can I easily integrate them?

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on 19 Jun 2014, 21:47 last edited by
            #7

            vld is a library, so it's pretty much as any other library.
            cppcheck is an external program. It doesn't know about your IDE (creator). You just point it to where you keep your project.
            sdl is a compiler switch so you just pass it to the compiler in your .pro file

            1 Reply Last reply
            0
            • N Offline
              N Offline
              nicky j
              wrote on 19 Jun 2014, 22:01 last edited by
              #8

              Ok, Ill look into these! Thanks for the help!

              1 Reply Last reply
              0
              • N Offline
                N Offline
                nicky j
                wrote on 19 Jun 2014, 22:03 last edited by
                #9

                It seems cppcheck is just for windows, I am running OS X. Any alternatives?

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on 19 Jun 2014, 22:09 last edited by
                  #10

                  Yeah, the tools I listed are all for windows (that's what I'm on mostly).

                  Clang has some nice tools I heard. Maybe this one: "clang static analyzer":http://clang-analyzer.llvm.org/

                  1 Reply Last reply
                  0
                  • N Offline
                    N Offline
                    nicky j
                    wrote on 19 Jun 2014, 22:27 last edited by
                    #11

                    Ok I'll look at it.

                    While messing about in my program I noticed something odd and possibly important. When I try to manipulate the Qlist in my nbhistory.h/cpp file, I get a crash. However if I do the EXACT SAME operations to a QList in my mainview.h/cpp file, it works and I don't get a crash. The biggest difference between these two files is that nbhistory.h/cpp contains multiple classes, while mainview.h/cpp contains only one. Could this have something to do with it? Why would a QList work in one file but not another?

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 19 Jun 2014, 22:30 last edited by
                      #12

                      cppcheck is available on OS X using e.g. macports

                      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
                      0
                      • C Offline
                        C Offline
                        Chris Kawa
                        Lifetime Qt Champion
                        wrote on 19 Jun 2014, 23:16 last edited by
                        #13

                        Well again - it might, it might not have something to do with the bug. It might be a coincidence if one of those "multiple" classes happens to overwrite something somewhere.

                        No point in asking us that sort of questions I think. Without looking at the actual code we're as good as guessing.

                        1 Reply Last reply
                        0
                        • N Offline
                          N Offline
                          nicky j
                          wrote on 19 Jun 2014, 23:28 last edited by
                          #14

                          Ive solved the problem!! I split up the classes into there own files.

                          Thank you for your help! I am going to mark this thread as solved :)

                          1 Reply Last reply
                          0
                          • C Offline
                            C Offline
                            Chris Kawa
                            Lifetime Qt Champion
                            wrote on 19 Jun 2014, 23:30 last edited by
                            #15

                            Sounds weird that it would help. Maybe you just covered it up, but ok, great.

                            1 Reply Last reply
                            0

                            12/15

                            19 Jun 2014, 22:30

                            • Login

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