Partager via


Step by Step: Monitoring Resource Leaks Using the Application Verifier Tool for Windows Mobile 5.0

4/7/2010

Microsoft Corporation

February 2007

Summary

You will use the Microsoft® Windows® CE 5.0 Test Kit to connect to a Windows Mobile® 5.0 Pocket PC Phone Emulator, launch the Application Verifier Tool, run a test application, and examine the logs that are generated. This HOL will take 45 minutes to complete. (34 printed pages)

Applies To

Microsoft Windows CE 5.0 Test KitMicrosoft ActiveSync®Microsoft Visual Studio® 2005Windows Mobile 5.0Application Verifier Tool for Windows Mobile 5.0

The following applications are required to run this HOL:

  • Windows XP Professional with Service Pack 2

  • Visual Studio 2005
    This HOL requires Visual Studio 2005 Team Edition. It will not work with any of the Express Editions. If you do not have the correct edition of Visual Studio 2005, find out how you can acquire it from the Visual Studio 2005 Developer Center.

  • Microsoft Device Emulator version 1.0 ENU

  • ActiveSync 4.1
    ActiveSync 4.1 allows for connectivity between a Windows Mobile device and your computer.
    Download and install ActiveSync 4.1.

  • Windows Mobile 5.0 SDK for Pocket PC
    The Windows Mobile 5.0 SDK for Pocket PC allows you to build applications for Windows Mobile Pocket PCs in Visual Studio 2005.
    Download and install Windows Mobile 5.0 SDK for Pocket PC.

    Note

    If you used an emulator in a previous HOL, you should close the emulator before starting this HOL. On the emulator, click File | Exit, and then select No when asked to save the emulator state.

  • Microsoft Windows CE 5.0 Test Kit
    The kit is a collection of tools that monitor the quality of a Windows CE 5.0 device or platform.
    Download Windows CE 5.0 Test Kit.

  • Application Verifier Tool for Windows Mobile 5.0
    The tool monitors resource leaks and memory corruption. Although the Window CE 5.0 Test Kit contains the Application Verifier Tool, the version of the binaries that ship with the Test Kit may not be compatible with the version of the Windows Mobile 5.0 SDK that you download. Consequently, you may need to replace the Application Verifier binaries in the Test Kit with the version available in the Application Verifier Tool for Windows Mobile 5.0. Be sure to follow the replacement instructions on the download page. For more information, see Application Verifier for Windows CE and Windows Mobile 5.0.

Introduction

Lab 1: Setting Up and Using the Application Verifier Tool

Conclusion

Introduction

This self-paced, hands-on lab (HOL) guides you through setting up and using the Application Verifier Tool for Windows Mobile 5.0 to examine possible resource leaks in an application that runs on Windows Mobile 5.0 software. The Application Verifier tool identifies possible scenarios in your application where resources are allocated but are not freed after they are used. The tool can monitor leaks of allocated memory, handles, and Graphics Device Interface (GDI) objects as well as memory corruption.

Lab 1: Setting Up and Using the Application Verifier Tool

This lab uses ActiveSync to make a connection between the Pocket PC Phone Emulator and the development workstation. If this connection is lost, be sure it is restored. For more information about setting up an ActiveSync connection, see "To launch and set up Microsoft ActiveSync" in Exercise 1. Also, be sure that the Pocket PC Phone Emulator is running and is cradled at all times in this lab.

Lab Objective

The objective of this lab is to use the Microsoft Windows CE 5.0 Test Kit and the Application Verifier Tool to monitor possible resource leaks and memory corruptions while running an application on a Windows Mobile 5.0 Pocket PC Phone Emulator. You will then use the Application Verifier Tool to examine the generated logs.

In this lab, you will perform the following exercises:

  • Launching ActiveSync and the Windows Mobile 5.0 Pocket PC Phone Emulator
  • Copying the required program files from the desktop computer to the emulator by using ActiveSync
  • Launching and setting up the Windows CE 5.0 Test Kit
  • Launching the Application Verifier Tool from the Windows CE 5.0 Test Kit and setting it up to run the test application
  • Running the test application and obtaining the logged data
  • Analyzing the logged data with the Application Verifier Tool

Exercise 1: Launching ActiveSync and the Windows Mobile 5.0 Pocket PC Phone Emulator

In this exercise, you will launch ActiveSync and then launch the Windows Mobile 5.0 Pocket PC Phone Emulator from Visual Studio 2005.

To launch and set up ActiveSync

  1. On the desktop computer, click Start | All Programs | Microsoft ActiveSync. This step launches the Microsoft ActiveSync, as shown in Figure 1.

    Bb278113.2cc82a62-2f09-4707-b86a-ab1f1ca4a458(en-us,MSDN.10).gif

    Figure 1. Microsoft ActiveSync

  2. In ActiveSync, click File | Connection Settings.

  3. Select the following setting in the Connection Settings dialog box, as shown in Figure 2:

    • Select Show status icon in taskbar.
    • In the Select Allow connections to one of the following box, select DMA.
    • In the This computer is connected to box, select Automatic.
    • Select Open ActiveSync when my device connects.

    Bb278113.3c9509a6-77ae-4212-980b-d6f774f85c59(en-us,MSDN.10).gif

    Figure 2. Connection Settings dialog box

  4. Click OK to save the settings.

  5. Minimize ActiveSync.

To launch the Windows Mobile 5.0 Pocket PC Phone Emulator from Visual Studio 2005

  1. On the desktop computer, click Start | All Programs | Microsoft Visual Studio 2005 | Microsoft Visual Studio 2005.

  2. In Visual Studio, click Tools | Device Emulator Manager to open the Device Emulator Manager, as shown in Figure 3.

    Bb278113.654da58e-ccc1-410a-8d7e-2bc33b822ca2(en-us,MSDN.10).gif

    Figure 3. Device Emulator Manager. Click the thumbnail for a larger image.

  3. Minimize Visual Studio.

  4. In the Device Emulator Manager window, right-click Windows Mobile 5.0 Pocket PC Phone Emulator in the list of emulators, and then click Connect to launch the emulator. The emulator opens.

  5. Wait for about a minute or so as the emulator boots up completely. If you get a dialog box that says that a connection error has occurred, dismiss and ignore it.

    After the emulator is booted, you should see the Today screen on the emulator.

  6. Minimize the Pocket PC Phone Emulator window.

  7. In the Device Emulator Manager window, verify that there is a green icon next to Windows Mobile 5.0 Pocket PC Phone Emulator, as shown in Figure 4. If the green icon is not present, the emulator may not have fully booted up yet. Wait for a few more moments.

    Bb278113.84fe74e7-dcd8-466a-8cb4-1fc289dc98bd(en-us,MSDN.10).gif

    Figure 4. Green icon symbolizing that the emulator is connected. Click the thumbnail for a larger image.

  8. Right-click Windows Mobile 5.0 Pocket PC Phone Emulator, and then click Cradle to cradle the emulator, as shown in Figure 5.

    Bb278113.a05b52a7-c9e9-4cef-8b72-d483ebef8efd(en-us,MSDN.10).gif

    Figure 5. Cradling the emulator. Click the thumbnail for a larger image.

  9. Wait for a minute or so as ActiveSync connects to the emulator.

  10. If you get an ActiveSync message, "You will be able to synchronize only files, folders, and other non-Outlook items if you continue", click OK.

    The Synchronization Setup Wizard appears, as shown in Figure 6.

    Bb278113.dfcacccb-b4bf-460b-b804-a53c2db660d5(en-us,MSDN.10).gif

    Figure 6. Synchronization Setup Wizard

  11. Click Cancel to close the wizard.

    The Microsoft ActiveSync window appears.

  12. Verify that the text Connected appears in the Microsoft ActiveSync window, as shown in Figure 7.

    Bb278113.a42aeb18-6737-4d89-afad-10ff23e32300(en-us,MSDN.10).gif

    Figure 7. ActiveSync: Connected

  13. Minimize the Microsoft ActiveSync window, and then minimize the Device Emulator Manager window.

If you don't get to this point, be sure your ActiveSync connection settings are as specified in Step 2 of the "To launch and set up Microsoft ActiveSync" procedure.

Exercise 2: Copying the Required Program Files from the Desktop Computer to the Emulator by Using ActiveSync

In this exercise, you will copy the required program files from the desktop computer to the emulator.

To copy the required program files from the desktop computer to the emulator

  1. On the desktop computer, find the Lab Program Files folder, and then double-click it.

  2. Select leakdemo.exe, and then click Edit | Copy.

  3. On the desktop computer, click Start | All Programs | Microsoft ActiveSync.

  4. On the Microsoft ActiveSync window, click the Explore icon to open the File Explorer, as shown in Figure 8.

    Bb278113.0f468ee8-382e-4ba0-925f-a47b351f1b58(en-us,MSDN.10).gif

    Figure 8. File Explorer on the desktop computer

  5. Double-click the My Windows Mobile-Based Device folder.

  6. Paste the copied program file, leakdemo.exe, to this location. If a message appears that says that ActiveSync may need to convert the file, click OK.

  7. Wait for a few moments as the file is copied onto the emulator.

  8. Close File Explorer and the Lab Programs Files folder if you have not done so already.

  9. Minimize the Microsoft ActiveSync window.

Exercise 3: Launching and Setting Up the Windows CE 5.0 Test Kit

In this exercise, you will launch and set up the Windows CE 5.0 Test Kit (CETK).

To launch and set up the CETK

  1. On the desktop computer, click Start | All Programs | Microsoft Windows CE 5.0 | Microsoft Windows CE 5.0 Test Kit.

  2. If you get a Windows Security Warning dialog box with the warning "Windows Firewall has blocked some features of this program," click the Unblock button to enable all features of the Test Kit.

    Note

    If the Windows Firewall security warning appears, it may not appear in the foreground. You may need to use the Windows taskbar to activate the window to unblock the program. You should be aware of this note as you continue through the following steps.

  3. In the Windows CE Test Kit window, click Connection | Start client…, as shown in Figure 9.

    Bb278113.b419d9cc-7a59-4fff-8c48-5b575dd9aea5(en-us,MSDN.10).gif

    Figure 9. Windows CE Test Kit

  4. The Device Connection dialog box appears, as shown in Figure 10.

    Bb278113.8b1e048e-9bcf-4884-a738-f087bb062768(en-us,MSDN.10).gif

    Figure 10. Device Connected dialog box. Click the thumbnail for a larger image.

  5. Click Settings. The Windows CE Platform Manager Configuration dialog box appears, as shown in Figure 11.

    Bb278113.2149fbbd-081a-48bd-8e66-54808310da55(en-us,MSDN.10).gif

    Figure 11. Platform Manager Configuration dialog box

  6. Under Windows CE Default Platform, select Default Device.

  7. Click Properties. The Device Properties dialog box appears.

  8. In the Transport box, select Microsoft ActiveSync.

  9. In the Startup Server box, select Microsoft ActiveSync, as shown in Figure 12.

    Bb278113.0051b452-5bf9-4b10-8bfb-d00ce29f0c6a(en-us,MSDN.10).gif

    Figure 12. Device Properties dialog box

  10. Click OK to save the settings.

  11. If a dialog box appears with the message, "You have not tested with the current transport and/or server," click Yes to dismiss the message.

  12. On the Windows CE Platform Manager Configuration dialog box, click OK.

To connect the CETK to the emulator

  1. On the Device Connection dialog box, be sure the Use Windows Sockets for the client/server communication check box is not selected, as shown in Figure 13.

    Bb278113.0e401afd-494b-4d5c-aa6f-56bfd063f648(en-us,MSDN.10).gif

    Figure 13. Device Connection dialog box. Click the thumbnail for a larger image.

  2. Click Connect to make a connection between the Test Kit and the emulator.

    The Select a Windows CE Device dialog box appears, as shown in Figure 14.

    Bb278113.4ffe7a71-9477-406b-a4a6-2460154d4694(en-us,MSDN.10).gif

    Figure 14. Select a Windows CE Device for connection

  3. Select Default Device, and then click OK to initiate the connection.

  4. Wait for a few moments as the connection is made.

  5. If you get a Windows Security Warning dialog box with the warning, "Windows Firewall has blocked some features of this program," click Unblock.

    The Pocket PC emulator should now be listed in the Windows CE Test Kit window, as shown in Figure 15.

    Bb278113.8a214da8-2af8-4dba-a04a-616d08c31883(en-us,MSDN.10).gif

    Figure 15. The emulator appears in the Windows CE Test Kit window

Note

Although the Pocket PC Phone Edition device emulator may be minimized, it displays a ClientSide window with continually updating status messages.

Exercise 4: Launching the Application Verifier Tool from the Windows CE 5.0 Test Kit and Setting It Up to Run the Test Application

In this exercise, you will launch the Application Verifier Tool, and then set it up to run the test application.

Note

The Application Verifier Tool is shipped as part of the Windows CE 5.0 Test Kit. You can also download the Application Verifier Tool by itself and run it without the Test Kit (see the HOL Requirements section). However, for the stand-alone Application Verifier Tool to work, it may require some connection components that ship with the Test Kit or Windows CE 5.0 (the Platform Builder toolset). For this lab, you will launch the Application Verifier Tool from the Test Kit.

To launch the Application Verifier Tool

  1. In the Windows CE Test Kit window, right-click Pocket_PC (ARMV4I), and then click Tools | Application Verifier, as shown in Figure 16.

    Bb278113.e022e959-2188-4bca-901d-6c7ebe6423e6(en-us,MSDN.10).gif

    Figure 16. Launching Application Verifier

  2. Wait a few moments as the Application Verifier Tool opens.

    The Application Verifier Tool attempts to initiate a connection to the emulator. The Device Connection dialog box appears.

  3. Click Connect to connect to the Application Verifier Tool to the emulator.

  4. In the Select a Windows CE Device dialog box, select Default Device, and then click OK.

  5. Wait (this could take up to a minute) as the connection is made.

    After the connection is made, the Device Connection dialog box automatically closes, and the Application Verifier Tool appears, as shown in Figure 17.

    Bb278113.3f370264-2000-4dbd-8199-77007f195324(en-us,MSDN.10).gif

    Figure 17. The Application Verifier Tool. Click the thumbnail for a larger image.

  6. Do one of the following:

    • If the Application Verifier Tool appears, minimize the Windows CE Test Kit window if you have not done so already. Proceed to the next procedure: To load the test application in the Application Verifier Tool.

    • If the Application Verifier Tool does not appear, repeat this procedure from Step 1. When you get to Step 3, click the Settings button, select the Default Device, and then click the Properties button. Be sure that the Transport and Startup Server list values are both set to Microsoft ActiveSync, as shown in Figure 18, and then click OK to save the settings. On the Windows CE Platform Manager Configuration dialog box, click OK, and then continue with Step 4.
      Bb278113.a904ffbb-7672-4ced-adca-48200ce0be74(en-us,MSDN.10).gif

      Figure 18. Device Properties dialog box

To load the test application in the Application Verifier Tool

  1. In the Application Verifier Tool, click Add. This step opens the Add Application dialog box.

  2. Select the test application, Leakdemo.exe, as shown in Figure 19, and then click Open.

    Bb278113.f172c841-e5b0-4274-9ce6-7462db82e832(en-us,MSDN.10).gif

    Figure 19. Adding the test application to the Application Verifier Tool

    The Application Verifier Tool now shows leakdemo.exe in the list of applications to monitor.

  3. In the Test Settings box, select Heap Verifier, Handle Leak Tracker, and Shell Verifier, as shown in Figure 20.

    Bb278113.98db2ffc-f9a4-4c54-82a3-44c640bb4d1e(en-us,MSDN.10).gif

    Figure 20. Test application loaded in Application Verifier. Click the thumbnail for a larger image.

You are now ready to run the application on the emulator.

Note

The following information may be useful when you are using the Application Verifier Tool outside this HOL. For the best results, enable as many test settings as possible. Additional test settings are available via the Options button. Note that the test settings are stored in the registry of the device, so they persist until you perform a clean reboot. You can monitor more than one application at the same time. Simply add all of the applications that you are interested in by clicking the Add button. To stop monitoring an application, remove it from the list of applications by clicking the Remove button.

Exercise 5: Running the Test Application and Obtaining the Logged Data

In this exercise, you will run the test application on the emulator, and then obtain the logged data.

To run the test application on the emulator

  1. In the Application Verifier Tool, click Run.

    Note

    When running the Application Verifier Tool outside this lab, in addition to using the Run button, you can start an application directly from the device. For example, you can start an application from a file browser, a shortcut, by clicking Start | Programs (for Pocket PC), or by clicking Start (for Smartphone). The Run button simply causes a CreateProcess("Application Name") call, which only works if your application is on the PATH. Also, remember to load your applications into the Application Verifier Tool (as explained in the "To load the test application in the Application Verifier Tool" procedure), and then choose your test settings before you begin to run the application. If your application is already loaded into memory before you choose the Application Verifier Tool settings, those settings will not take effect until the next time the application is reloaded into memory.

  2. On the Windows taskbar, restore the Pocket PC Phone Emulator window.

  3. Wait until the test application is running in the foreground of the emulator with the title Minimal Windows App. Allow enough time (about 45 seconds for this lab) for the application to be fully loaded.

  4. Click the X button at the top right of the test application once to close the application.

  5. Wait for about 15 seconds as the logs are generated.

    Note

    The Application Verifier Tool automatically generates logs on process exit. However, it is still possible to get the logs at other times too. The logs are useful when you are testing drivers or other modules that do not get unloaded. On Windows Mobile 5.0-based Pocket PCs, you can find out which programs are running from the memory control panel in the Settings control panel (Click Start | Settings. Click the System tab, and then click Memory. Click the Running Programs tab). From this location, you can also stop running applications.

  6. Minimize the emulator's window.

To transfer the logged data from the emulator onto your development workstation

  1. In the Application Verifier Tool, click the Get Logs button. This step opens the File Browser.

  2. Browse to the Desktop | Logs folder, and then click OK.

  3. Wait until the Get Logs message appears, which notifies you that the logs have been successfully copied.

  4. Click OK to close the message box.

Although the Application Verifier Tool stores the logged data in text files, it is much more convenient to use the viewer on the Application Verifier Tool. The log files are named according to the following convention: AppVerifier_ProcessName_ProcessIdentifier.log.

To view the log files by using the Application Verifier log viewer

  1. In the Application Verifier Tool, click the View Exported Logs button. This step opens the Windows file browser.

  2. Browse to the Desktop | Logs folder, and then select the log file you previously transferred.

    Note

    Placing your cursor over a file in the file browser tells you the modification date and time. You can use this information to distinguish between multiple log files in the same directory.

  3. Click Open to open the log files. This step opens the Browse for Folder dialog box, where you can select the folder that contains the .map file that corresponds to the test application, as shown in Figure 21.

    Bb278113.eb0f6e1e-c519-4322-b65d-5ae7b42bb4b5(en-us,MSDN.10).gif

    Figure 21. Browse to the folder that contains the .map files

  4. Select Desktop | Lab Program Files, and then click OK.

Note

When monitoring an application outside this lab, you may get a message that says, "Unresolved callstacks" while opening your logged data. If the application has corresponding .map files, use the file browser that appears to select the folder that contains the .map files. If you have no such files, click OK to continue. The .map files contain extra debug information, such as function names, that can help you track potential resource leaks faster and better. To generate map files when developing an application with Visual Studio 2005, you may have to set the appropriate Linker options. To do so, right-click the project in Solution Explorer, and then click Properties. Under Configuration Properties, click Linker | Debugging, and then set the Generate Map File option to Yes.

Exercise 6: Analyzing the Logged Data with the Application Verifier Tool

In this exercise, you will analyze the logged data by using the Application Verifier Tool.

To analyze your data

  1. Expand Leakdemo in the viewer. The data displays in the format as shown in Figure 22.

    Bb278113.8debd95c-1379-49a9-bd2f-e496773301eb(en-us,MSDN.10).gif

    Figure 22. Logged data. Click the thumbnail for a larger image.

  2. Browse to various sections to investigate potential problems.

The Application Verifier Tool collects data that you can use as a guide when troubleshooting resource leaks and memory corruptions. The following is brief breakdown of each section of the log:

  • The Started section tells you when the application was started.
  • The Stopped section tells you when the application was stopped.
  • The Heap corruption section reports potential heap corruption problems in your application.
  • The Double free section reports potential double freeing of allocated memory.
  • The Mismatched Load/FreeLibrary section reports LoadLibrary calls that do not have corresponding FreeLibrary calls. These alerts may be the result of intended delay-loads for dynamic-linked libraries.
  • The Potential leak section indicates possible memory leaks.
  • The Leaked handles sections refer to resource handles that were possibly leaked

Expanding each section reveals the call stack for the potential problem.

For example, expanding the Heap corruption bullet shows the stack trace for a potential heap corruption that occurs in the WndProc function of Leakdemo.exe, as shown in Figure 23. You can resize the viewer as needed. Notice the Allocation, Free, and Current call stacks leading to this

Bb278113.4383eaa1-2dbf-4e63-9e25-434db1127fd5(en-us,MSDN.10).gif

Figure 23. Heap Corruption log results. Click the thumbnail for a larger image.

Expanding the Double free bullet illustrates a double free in the WndProc function of Leakdemo.exe. The Allocation, Reallocation, Free, and Current call stacks leading to this are shown in Figure 24.

Bb278113.1821a4b4-4468-4b02-9d41-3bfca94d9942(en-us,MSDN.10).gif

Figure 24. Double free log results. Click the thumbnail for a larger image.

Next, drill down into a Potential leak section, as shown in Figure 25.

Bb278113.6cd4abd2-86a7-4a4b-a950-7fabec2ecad0(en-us,MSDN.10).gif

Figure 25. Memory leak in DrawLogo - LoadBitmap. Click the thumbnail for a larger image.

This stack trace shows a memory leak. It identifies the leaked item that results from a LoadBitmap call in the function DrawLogo (the second line in call stack). The following code is the relevant section of the source code for Leakdemo.exe.

void DrawLogo (PAINTSTRUCT *ps)
{
    HBITMAP hBmp;
    HBITMAP hBmpOld;
    BITMAP bmp;
    HDC hDCTemp;
    int iXPos;
    int iYPos;

    hBmp = LoadBitmap (ghInstance, MAKEINTRESOURCE(IDB_BITMAP));
    hDCTemp = CreateCompatibleDC (ps->hdc);

    GetObject (hBmp, sizeof (bmp), & bmp);

    hBmpOld = (HBITMAP) SelectObject (hDCTemp, hBmp);

    iXPos = (ps->rcPaint.right-bmp.bmWidth) / 2;
    iYPos = (ps->rcPaint.bottom-bmp.bmHeight) / 2;

BitBlt (ps->hdc, iXPos, iYPos, bmp.bmWidth, bmp.bmHeight, hDCTemp, 0, 0,            
        SRCCOPY);

    // BUG - Most common GDI programming error
    // DeleteDC (hDCTemp);
    // DeleteObject (hBmp);
}

In the previous code, you did not delete the bitmap handle returned by the call to LoadBitmap after using it. This is responsible for the above leaked item, as shown in the log (Figure 25).

The same block of code is responsible for the next leak violation identified by the Application Verifier Tool, as shown in Figure 26. Notice that, in the DrawLogo function, you did not delete the memory device context handle returned by the call to CreateCompatibleDC after using it.

Bb278113.d5fdafd9-3357-4782-8773-d60c4dbf4e8e(en-us,MSDN.10).gif

Figure 26. Memory leak in DrawLogo - CreateCompatibleDC. Click the thumbnail for a larger image.

The Application Verifier Tool identifies the leak that results from the CreateCompatibleDC call in the DrawLogo function (the second line in the call stack).

Note

You can copy the call stack for an identified potential problem onto the Clipboard. To do so, select a bullet, and then click the Copy button in the viewer.

Conclusion

In this lab, you performed the following exercises:

  • Launching ActiveSync and the Windows Mobile 5.0 Pocket PC Phone Emulator
  • Copying the required program files from the desktop computer to the emulator by using ActiveSync
  • Launching and setting up the Windows CE 5.0 Test Kit
  • Launching the Application Verifier Tool from the Windows CE 5.0 Test Kit and setting it up to run the test application
  • Running the test application and obtaining the logged data
  • Analyzing the logged data with the Application Verifier Tool