Solved

Monitor Private bytes usage

Posted on 2009-05-05
12
547 Views
Last Modified: 2013-12-27
My Windows mobile app is crashing after many hours use.  I think it may be a memory leak.  I one time found a memory leak on my PC application by monitoring the private bytes around a certain function call.  I found that the private bytes usage would increase every time this function was called and eventually the app cashed.

How can I monitor the provate bytes usage in my windows mobile app, is there any c++ code to do this?
0
Comment
Question by:Wanting2LearnMan
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 150 total points
ID: 24302957
I've never tried this on a mobile device but it should work: Windows comes with built in tools for diagnosing memory leaks...

You can use _CrtSetDbgFlag to enable CRT heap allocation debugging. This should be at the very start of your program.
E.g. _CrtSetDbgFlag ( _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG ) | _CRTDBG_LEAK_CHECK_DF );
http://msdn2.microsoft.com/en-us/library/974tc9t1(VS.80).aspx

You can use _CrtDumpMemoryLeaks to generate an error report if the application failed to free all the memory it allocated. This should be at the very end of your program.
http://msdn2.microsoft.com/en-us/library/d41t22sb(VS.80).aspx

Use can use _CrtSetBreakAlloc or _crtBreakAlloc to set break points where specific heap is allocated (as reported by CrtDumpMemoryLeaks) so that you can see where the problem starts
http://support.microsoft.com/kb/151585

Memory leak detection and isolation: http://msdn2.microsoft.com/en-us/library/x98tx3cf(VS.80).aspx

You'll need to change the code as identified by the MSDN above, copy the new binary to the mobile device and then attached Visual Studio as a remote debugger to the application. When the application terminates you'll see a memory leakage report in the debugger output window. This will tell you what blocks leaked, you can then set break-points on the allocation lock to see where it all starts from. Of course, you'll still have to figure out why it leaks :(
0
 

Author Comment

by:Wanting2LearnMan
ID: 24303139
I am using eVC++ to build my app and my program is runninng on a WM6 device so I dont think I can get Visual Studio to act as a remote debugger.  Or can I??

0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 150 total points
ID: 24303161
>> I dont think I can get Visual Studio to act as a remote debugger.  Or can I??

You should be able to. I used to develop mobile apps back when WinCE 3.0 was the mobile OS of choice and you could certainly do it then (via ActiveSync) over serial (very slow) or network.
0
 

Author Comment

by:Wanting2LearnMan
ID: 24303222
Thanks,

Do you have any links or info on how to do this for an exe running on a WM6 device?

0
 

Author Comment

by:Wanting2LearnMan
ID: 24303225
I will be using ActiveSync by the way.
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 150 total points
ID: 24303283
>> Do you have any links or info on how to do this for an exe running on a WM6 device?
I'm sorry, no... I'd only end up Googling, and I@m sure your Google skills are just as good as mine (probably better in fact :) )

I did find this link that discusses debugging on non Windows mobile devices...
http://community.opennetcf.com/blogs/cf/200708/Debugging%20without%20ActiveSync.pdf
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Wanting2LearnMan
ID: 24303470
ok thanks
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 150 total points
ID: 24303529
No worries... just sorry I can't offer an authoritative solution.
0
 
LVL 40

Assisted Solution

by:mrjoltcola
mrjoltcola earned 50 total points
ID: 24303588
Also consider debugging the app in an emulator. If it leaks, it will leak on the emulator.

Also, isn't it time to port your project into Visual Studio 2005/2008? Why are you still using EVC4 to develop for WM6?
0
 

Author Comment

by:Wanting2LearnMan
ID: 24303769
>>Also consider debugging the app in an emulator. If it leaks, it will leak on the emulator.
OK I will try that also.

>>Also, isn't it time to port your project into Visual Studio 2005/2008?
Yes that is on my TO DO list.  I have tried to update it and I get loads and loads of errors and warnings.  I'll just have to take time out and do it some time.  (I dont have time at the moment)
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 300 total points
ID: 24306356
It is not so difficult to check your code for memory leaks without running it in the debugger.

(1) only use pointers if you really have to. For example

   string * ps = new string;
   ps = "Hello";
   someFunc(ps);
   delete ps;

could be written as

    string s = "Hello";
    someFunc(&s);

If the someFunc is your function you would change the prototype to

   void someFunc(const string & s);

and now have

     someFunc("Hello");


In C++ you normally would use pointers only for baseclass pointers (for virtual use), for function pointers and for interfaces which require a writeable pointer and where you cannot change that interface, e. g. when accessing shared memory via pointer. Pointers as class members should be used only for dynamically sized container classes. And then these pointers must be private and fully encapsulated and the destructor cares for delete.

(2) Always free (delete) pointers in the same context as they have allocated new storage.

  int* p = new int[10];
  dosomething(p);
  delete []p;

Don't delete pointers in the functions you were calling. Don't return newly created pointers to the caller. Instead, let the caller do allocating and deleting and let it pass the pointer (better reference) already fully allocated. If the caller doesn't know the size of the array, use a vector instead of the array and pass it by reference.

   vector<int> v;
   int n = funcThatFillsArray(v);  

int funcThatFillsArray(vector<int>& v)
{
    v.resize(10, 0);  // resizes and initializes array
    // pass an int* and number of elements
    funcThatNeedsIntPointer(&v[0], v.size());
    return v.size();
}

(3) Always initialize pointers (and values)

   int * pi = NULL;

   ....
   // here allocating arrays via vector would be much easier
   if (pi == NULL)
   {
        pi = new int[10];
        memset(pi, 0, 10*sizeof(int));
   }

    // don't need to check for NULL pointer on delete  
    delete [] pi;
    // but set it back to NULL after deletion
    pi = NULL;

(4) Never store the same pointer into two containers or have two pointer  variables for the same pointer value.

If ever possible do not store pointers but objects in a container. Then all objects automatically were destroyed with the container.

If storing pointers there are a few disadvantages:

- the array couldn't be sorted without providing a compare function that works on pointers rather than on values.
- you need to delete each single element when deleting the container
- whenever accessing the pointer you must assure that the pointer either is NULL or valid.

IMO, the only valid way to storing pointers in a container is to have a container class which fully encapsulate all pointer handling.

0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 300 total points
ID: 24306853
If applying the above rules it shouldn't be so much difficult to find the associated delete statement for each 'new' *and* check whether there is a way that the delete wasn't executed. You also should be able to find out whether a pointer gets a new assignment while the old pointer value got lost and the memory never was freed:

  int * pi = NULL;

  ...
  if (pi == NULL)    // good
  {
       pi = new int[10];                
       memset(pi, 0, 10* sizeof(int));
  }  

  ...
  int arr[100] = { 0 };  // all elements zero
  pi = &arr[0];    // potential memory leak as the old pointer was lost

The better way is:

  int arr[100] = { 0 };  // all elements zero
  if (pi != NULL)
  {
         delete []pi;
         pi = NULL;
  }
  pi = new int[20];
  memset(pi, 0, 20*sizeof(int);
  f(pi);
  delete []pi;
  pi = NULL;
 
Note, if a pointer was used for dynamic memory you never should use it for addresses on the stack or constant arrays.

   int * pi = new int[10];  
   memset(pi, 0, 10*sizeof(int);
   f(pi);
   delete []pi;
   pi = NULL;

   ....
   int arr[100] = { 0 };  // all elements zero
   pi = &arr[0];    // bad. pi now is pointing to memory that cannot be deleted
   ...
   
   if (pi != NULL)
   {
       delete []pi; // kaboom
       pi = NULL;
   }

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

With Windows Embedded Handheld, called Windows Mobile, Microsoft re-designed the user interface. The Start Icon moved down to the bottom, inside the menu bar area.   If you need to hide the Start Icon and/or the SIP (soft input panel, softwar…
Preface: This article is part of a series focused on cross platform mobile app development (specifically Android and iOS) using the Alloy framework and Titanium Studio made by Appcelerator (https://www.appcelerator.com/). This article presumes a wor…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now