C++ find dangling pointer deletion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 51423benam
    New Member
    • Apr 2018
    • 31

    C++ find dangling pointer deletion

    Hello!

    I am working on the source code of Blender and trying to contribute, mainly through bug fixes. Now I have a problem: I am using Visual Studio and Blender crashes because of a dangling pointer (value 0xDDDDDDDD). Now I want to find the place where it is deleted or freed. Are there tools or solutions for that? Most tools seem to only search for memory leaks and so on, those, which find dangling pointers, don't usually search the location where it is deleted or freed.
    Thanks!

    EDIT: Sorry for the double post, when I asked the first one, it somehow disappeared for a while, I thought it didn't work, asked this question again. Then the first question was visible again, but it seems like there is a problem with it, the sub site is wrong somehow.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I believe a 0xDDDDDDDD is an uninitialized pointer. There is no DDDDDDDD address.

    I believe Visual Studio inserts this phony address as a tip-off should the pointer never be initialized.

    Comment

    • 51423benam
      New Member
      • Apr 2018
      • 31

      #3
      Read this, there it says that 0xDDDDDDDD means deallocated memory. https://www.softwareverify.com/memory-bit-patterns.php

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Yes. it's slowly coming back. 0xDDDDDDDD is deallocated memory and the 0xCCCCCCCC is the uninitialized pointer.

        OK. So here goes. You need to stop using pointers. Instead you use a smart pointer. A smart pointer is a class enveloping a pointer such that each time a pointer object is copied the constructor increments a reference count and each time a pointer object is deleted, the destructor decrements the reference count. Should the reference count become zero, the destructor will delete the memory pointed at by the enveloped pointer. The reference count can only be zero when the current object(this) is the only object left.

        There is a template for this in the C++ Standard Library. Look for std::shared_ptr .

        Comment

        • 51423benam
          New Member
          • Apr 2018
          • 31

          #5
          I know smart pointers, in fact, I am using them where I can already in my projects, but as I sad, this is work for an open-source software (Blender). So I can't really rewrite millions of lines of code to use smart pointers, I have to take the code as it is.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            I would suggest that at least the pointer causing the crash be converted to a std::shared_ptr .

            The behavior you describe indicates there is no reference count or shared logic protections on this thing.

            By the time you write through this pointer and crash the site of the crash is nowhere near where the delete error occurred.

            You might even consider logging allocations and deallocations during debug. At least this would give a stack map.

            I suspect this will be a difficult thing to find.

            Comment

            • 51423benam
              New Member
              • Apr 2018
              • 31

              #7
              I just found that popular memory tools like asan (adress sanitizer) output the location where it is deleted. Now I only have to get one of those working on windows.

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Is this Android or Linux? There are emulators for Windows 10. Install the emulator and launch through it.

                Comment

                • 51423benam
                  New Member
                  • Apr 2018
                  • 31

                  #9
                  Can you explain it please a little bit more detailed? What do you mean with is this Android or Linux? And which emulators?

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    There's Cygwin. You recompile the Linux code on Windows 10 and run it.

                    For Android try: https://windowsreport.com/best-andro...rs-windows-10/


                    Of course, I cannot recommend any of these products. I can only point them out.

                    Comment

                    • 51423benam
                      New Member
                      • Apr 2018
                      • 31

                      #11
                      I think I will try it with Dr. Memory or a similar tool which is windows compatible.

                      Comment

                      • weaknessforcats
                        Recognized Expert Expert
                        • Mar 2007
                        • 9214

                        #12
                        Let me know what happens.

                        Comment

                        • 51423benam
                          New Member
                          • Apr 2018
                          • 31

                          #13
                          Well, it seems to be working, but I clearly have to learn how to supress and whitelist errors, because it counts more than 2.000.000 just on Blender startup ��

                          Comment

                          • weaknessforcats
                            Recognized Expert Expert
                            • Mar 2007
                            • 9214

                            #14
                            Often the first error triggers a slew of other errors and this cascades into a huge mess.

                            If you can, fix only the first error and then rebuild.

                            Comment

                            • 51423benam
                              New Member
                              • Apr 2018
                              • 31

                              #15
                              I don't think so, the warnings are spread all over blender. But the problem is, the whitelist docs for dr. Memory are a bit... bot that detailed. It would be cool if I could whitelist only one file. Of course, I can suppress all other error types too, vut there seem to be still many uninitialized accesses or at least many possibilities for them.

                              Comment

                              Working...