Arm Community
Arm Community
  • Site
  • User
  • Site
  • Search
  • User
  • Groups
    • Research Collaboration and Enablement
    • DesignStart
    • Education Hub
    • Innovation
    • Open Source Software and Platforms
  • Forums
    • AI and ML forum
    • Architectures and Processors forum
    • Arm Development Platforms forum
    • Arm Development Studio forum
    • Arm Virtual Hardware forum
    • Automotive forum
    • Compilers and Libraries forum
    • Graphics, Gaming, and VR forum
    • High Performance Computing (HPC) forum
    • Infrastructure Solutions forum
    • Internet of Things (IoT) forum
    • Keil forum
    • Morello Forum
    • Operating Systems forum
    • SoC Design and Simulation forum
    • 中文社区论区
  • Blogs
    • AI and ML blog
    • Announcements
    • Architectures and Processors blog
    • Automotive blog
    • Graphics, Gaming, and VR blog
    • High Performance Computing (HPC) blog
    • Infrastructure Solutions blog
    • Innovation blog
    • Internet of Things (IoT) blog
    • Operating Systems blog
    • Research Articles
    • SoC Design and Simulation blog
    • Tools, Software and IDEs blog
    • 中文社区博客
  • Support
    • Arm Support Services
    • Documentation
    • Downloads
    • Training
    • Arm Approved program
    • Arm Design Reviews
  • Community Help
  • More
  • Cancel
Arm Community blogs
Arm Community blogs
High Performance Computing (HPC) blog Fixing Dangling Pointers
  • Blogs
  • Mentions
  • Sub-Groups
  • Tags
  • Jump...
  • Cancel
More blogs in Arm Community blogs
  • AI and ML blog

  • Announcements

  • Architectures and Processors blog

  • Automotive blog

  • Embedded blog

  • Graphics, Gaming, and VR blog

  • High Performance Computing (HPC) blog

  • Infrastructure Solutions blog

  • Internet of Things (IoT) blog

  • Operating Systems blog

  • SoC Design and Simulation blog

  • Tools, Software and IDEs blog

Tags
  • High Performance Computing (HPC)
  • Arm DDT
  • HPC Tools
  • Development Tools
  • Arm Forge
  • infrastructure
Actions
  • RSS
  • More
  • Cancel
Related blog posts
Related forum threads

Fixing Dangling Pointers

Mark O'Connor
Mark O'Connor
December 22, 2015
4 minute read time.

What are dangling pointers?

Dangling pointers are pointers whose memory has been freed but which have not been set to null (or 0x0). This allows a particularly tricky class of bug to arise, because it is often possible for subsequent code to keep on using the dangling pointer and to keep on getting valid-looking data – until suddenly that memory is repurposed for something else!

They can lead to unpredictable behavior, silent corruption and program crashes.

How do I tell if a bug is related to a dangling pointer?

Arm Forge 6.0 has better memory debugging than ever - fully-integrated, scalable and packed with features for tracking down and fixing problems. One of the new additions is a feature to find and fix dangling pointers so quickly it feels like magic!

Arm advocates using the scientific method to debug problems that are not solved within a few minutes of ad-hoc investigation. In this case, our hypothesis is that our program behaves in a particular way because of a dangling pointer problem. We will test this with an experiment, then draw our conclusions and come up with a new hypothesis until we arrive at the root cause.

DDT is a powerful tool for uncovering information about all kinds of memory errors, including dangling pointers. In this case we want to use DDT's hardware free protection feature. This advanced feature adds very little overhead to your program but allows DDT to instantly stop your program at the exact line of code that tries to re-use a dangling pointer.

To enable it, put a tick in the “Memory Debugging” option in the Run dialog, then click on “Details...” to open the Memory Debugging Options window. Turn up the “Heap Debugging” to one level higher than Fast. You'll see “free-protect” appears in the “Enabled Checks” window. Click on the “More Information” link to read our documentation on the rich variety of protection DDT can add to your code.

This level of memory debugging is usually all you need to find dangling pointer problems. Now click “Run” to start your program under DDT. If a dangling pointer is reused, DDT will stop your program at the exact line of code that reuses it with an error like this one:

Our hypothesis was proven correct! We know we have a dangling pointer error – and that this line of code is using memory that has been freed. Sometimes that's all we need to know to understand where the problem came from, but frequently it's only the first step of the journey.

DDT can also tell us which pointer is dangling and exactly where it was originally allocated. Click “Pause” to pause execution on the line of code with the error, then look at the variables being used on the line. These also appear in the “Current Line(s)” window on the right. Right-click on any pointers or dynamically-allocated arrays and choose “View Pointer Details” from the menu:

This window shows a wealth of information about any pointer in your program:

  • Whether the pointer is valid, not yet allocated or dangling
  • What size of allocation is or was made
  • The exact position in the code that the pointer was allocated at – even if it is a dangling pointer that has since been freed
  • The exact position in the code that the pointer was deallocated again, if it is indeed a dangling pointer

Arm DDT tells us at once that this pointer is dangling (it says it points to an allocation that has already been freed) and shows us the full stack of function calls that led to its allocation. In particular, we can see that it was allocated at hello.c:88 and simply clicking on this jumps us to that location in the source code viewer right away.

It also tells us where this memory was deallocated. In this case, at hello.c:89.

We now have all the information we need to help us solve this puzzle. We have identified:

  • The cause of the error: reuse of a dangling pointer.
  • Which pointer is reused (dynamicArray).
  • The point of allocation (hello.c:88).
  • The point of deallocation (and where it is not set to NULL – hello.c:89).
  • The point of reuse of the invalid pointer (hello.c:92)

With all the locations relating to this pointer's lifecycle just a click away we can now read the source code to understand why the pointer was used like this and to understand whether it was released too early, not cleared to NULL, or reused inappropriately. After all, the tools can only take you so far – ultimately the program does exactly as it was told to do. The real error is in our expectation that it would work, when we unambiguously wrote that it should not!

Arm Forge helps you to navigate and understand complex parallel codebases efficiently, getting you back on track as quickly as possible. We also provide on-site training courses in effective debugging at all scales using the scientific method and the appropriate tools – including Arm DDT.

Read more about what the memory debugger can help with by visiting the memory debugging in Arm DDT page.

Good hunting!

Anonymous
  • Stuart Longland
    Offline Stuart Longland over 3 years ago

    If I try attaching to an existing program running on my ARM Cortex M3 via gdbserver in OpenOCD, I can't see the "Memory Debugging" option mentioned above, but can otherwise debug the program.

    If I go to the "Run", it tries to run the ARM binary on my x86-64 host (resulting in an "exec format error" message) instead on the Cortex M3 where it's needed.

    How do I enable memory debugging when debugging a Cortex M3 target?

    • Cancel
    • Up 0 Down
    • Reply
    • More
    • Cancel
High Performance Computing (HPC) blog
  • Arm Compiler for Linux and Arm PL now available in Spack

    Annop Wongwathanarat
    Annop Wongwathanarat
    We are happy to announce that Arm Compiler for Linux (ACfL) and Arm Performance Libraries (Arm PL) are now available as installable packages in Spack, a widely used package manager in the HPC community…
    • May 22, 2023
  • Using vector math functions on Arm

    Chris Goodyer
    Chris Goodyer
    In this post, we highlight the scale of performance increases possible, detail the accuracy requirements, and explain in detail how to use the libamath library that ships with ACfL.
    • May 16, 2023
  • Arm Compiler for Linux and Arm Performance Libraries 23.04

    Chris Goodyer
    Chris Goodyer
    Arm Compiler for Linux 23.04 is now available with improved compilers and libraries. In this blog, we explore what is new in this first major release of 2023.
    • May 9, 2023