Memory Leak Detection with LabWindows™/CVI

Contents

Introduction

One strength of the C programming language is its ability to give the developer low level control to optimize code.  Because the C language provides direct access to application memory and hardware-specific function calls, developers often have a greater ability to control program size and execution speed.

But with the power and flexibility of C, comes the additional complexity of memory management as shown by the many academic books discussing the topic.  One book mentioned that:

The overwhelming majority of "bugs" and crashes stem from problems of memory access, allocation, or deallocation. Such memory related errors are notoriously difficult to resolve … Most professional programmers learn about it entirely through actual experience of the problems it causes.

Instead of developers identifying memory leaks once their applications are deployed, LabWindows/CVI 9.0 now provide a tool, the Resource Tracking Window, to allow developers to identify and fix memory leaks earlier in the development process.

Overview of the Resource Tracking Window

For over a decade, LabWindows/CVI has provided automatic array bounds checking to catch memory errors that can cause unexpected behavior.  Building upon the history of delivering advanced user protection features not commonly available in C. LabWindows/CVI now includes an integrated tool to locate potential resource leaks that can decrease system performance over time.

The new Resource Tracking Window (available in LabWindows/CVI Full Development System and Developer Suite) records and tracks all resources allocated at runtime including dynamic memory, file handles, panels, GPIB/TCP handles, and thread pools. The tool also allows you to easily go to source code, view memory, break on deallocation and log tracked resources to disk for later review.

Select Extended in the Debugging Options section of the Build Options dialog box to enable interactive resource tracking. You also can enable and disable resource tracking programmatically using the GetEnableResourceTracking and SetEnableResourceTracking functions located in the Utility Library.

Using the Resource Tracking Window

The Resources column of the Resource Tracking window displays both allocated and deallocated resources that you allocate in your program. LabWindows/CVI displays newly allocated resources in red text, existing allocated resources in black text, and recently released resources in grey text. The resources are also grouped in categories according to type; for example, memory, toolbox, user interface resources, and so on. Double-click a resource in the Resources column to go directly to the location in the source code where the resource was allocated. The Stack Trace section of the Resource Tracking window shows the call stack of functions when the resource was allocated, displaying the most recently called function at the top of the list and the initial function at the bottom of the list.

Resource tracking is available at both the extended and standard debugging levels. Selecting Extended in the Debugging Options section of the Build Options dialog box enables resource tracking by default. If you select Standard, you must use the SetEnableResourceTracking function to enable resource tracking.

To view the Resource Tracking window, select Window»Resource Tracking in the active LabWindows/CVI window. Right-clicking in the Resources column of the Resource Tracking window displays the context menu with the following options:

  • Go to Source—Highlights the location in the Source window where the resource was allocated.
  • View Memory—Opens the Memory Display window and displays the memory contents that correspond to the selected item.
  • Break on Deallocation—Specifies that LabWindows/CVI suspends your program when the resource is released.
  • Show/Hide Thread IDs—Displays or hides the Thread ID column in the Resource Tracking window.
  • Clear Window—Removes all items from the Resource Tracking window.
  • Load from File—Loads a previously saved file.
  • Save to File—Saves the current contents of the Resource Tracking window to a text file for later use.
  • Sort—Opens the Sort Tree dialog box, which you can use to sort the resources in the Resource Tracking window.
  • Find—Opens the Find dialog box, which you can use to search for text in the column.
  • Expand Resource—Expands the resource category.
  • Collapse Resource—Collapses the resource category.
  • Expand All—Expands all resource categories.
  • Collapse All—Collapses all resource categories.
  • Release Window/Confine Window—LabWindows/CVI displays different options depending on whether the Resource Tracking window is in the Window Confinement Region. The Release Window command removes the window from the Window Confinement Region. The Confine Window command confines the window to the Window Confinement Region.

Right-clicking in the Stack Trace section of the Resource Tracking window displays the context menu with the following options:

  • Go to Source—Highlights the location in the Source window of the function you selected.
  • Find—Opens the Find dialog box, which you can use to search for text in the column.

Right-clicking on the tab of the Resource Tracking window displays the context menu with the following options:

  • Close—Closes the window.
  • Release Window—Removes the window from the Debugging Region.

Because tracking resources creates additional overhead, enabling resource tracking affects the performance of your program.

Additional Resources

Evaluate LabWindows/CVI

LabWindows/CVI Documentation

The mark LabWindows is used under a license from Microsoft Corporation. Windows is a registered trademark of Microsoft Corporation in the United States and other countries.

Was this information helpful?

Yes

No