Solved

Program get slower and slower then gradually freezes.

Posted on 2009-05-18
20
506 Views
Last Modified: 2013-12-21
I am trying to fix a problem where my Windows Mobile application freezes.  My Application just inserts data in to a database (codebase).  The user just presses a button and a series of other dialogs come up and ask questions etc then the info is inserted into a database.

I have tested it flat out for about 5 hours now and I notice that the dialogs gradually get slower and slower to launch.  Just a few moments ago it froze while one dialog was trying to launch (when it froze the pda screen was like a morph of the new dialog ontop of the main dialog).

I am just looking for general help as to possibly why windows MFC dialogs would get slower and slower to launch and then freeze.  I have been looking for memory leaks etc and have found nothing major that I can see to be the problem.

My application is written using MFC (it was an eVC4 application converted to VS2005 now).

How could I best solve this problem?  any tools that VS provides that could help me etc.
0
Comment
Question by:Wanting2LearnMan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
  • 3
  • +1
20 Comments
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 200 total points
ID: 24414822
I guess a resource (or memory) leak is the cause.

When you run in DEBUG mode do you get a message about memory leaks when you close the app?

Resource leaks are a major pain to track down.  :-(
0
 

Author Comment

by:Wanting2LearnMan
ID: 24414976
>>When you run in DEBUG mode do you get a message about memory leaks when you close the app?
Do you mean  build the debug version and run the exe on my device for a short while then close it.  
What kind of a message do you mean?  A pop up message box??

Thanks
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 200 total points
ID: 24415026
Nope, run within the IDE, check the output window of the IDE.  If you get something like
memory leak at blah blah..
then you are leaking memory which might be the cause of your problem.

Unfortunately the IDE won't tell you if you are leaking resources.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Wanting2LearnMan
ID: 24415094
OK I'll try that.
I am also going to test my app again only without launching certain dialogs, that way I will try to narrow it down to which dialogs might be causing the problem.
Is this a good way to go??
My problem is that it took 4.5 hours of use for it to freeze so its a bit of a nightmare....

Can you think of any other ways to track down resource leaks??
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 200 total points
ID: 24415197
>>Can you think of any other ways to track down resource leaks??

A third party tool such as bounds checker (I think) will do that.
0
 

Author Comment

by:Wanting2LearnMan
ID: 24415748
The users of my progam only ever use it for about one hour at a time.  And since it crashes after about 4 hours use, if I tell them to exit my program after they use it, will that will that mean that any leaks will be cleared?

This may buy me some time until I gat if fixed...

Let me know your thoughts sure.

Thanks
0
 
LVL 40

Accepted Solution

by:
evilrix earned 150 total points
ID: 24416475
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 (using, for example, activesync). 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 :(

Also, if you can't debug this on the device itself you might want to try using one of the emulators that comes with the Mobile SDK.
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 150 total points
ID: 24416485
>> When you run in DEBUG mode do you get a message about memory leaks when you close the app?
Sorry Andy, just realised that this is what you were eluding to. You need to enable CRT leak checking before you'll get this output, the links above will take you to the MSDN pages that show how to do that.
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 200 total points
ID: 24419047
@evilrix - no problems.  ps.  Even without setting anything the IDE will inform you the app leaked memory on closure, then you can start enabling things to assist in tracking it down.


>>The users of my progam only ever use it for about one hour at a time.  And since it crashes after about 4 hours use, if I tell them to exit my program after they use it, will that will that mean that any leaks will be cleared?

Yes, resources and memory should be freed when your app exits and is unloaded.  (If it stays behind - think outlook - then that may not be the case).
0
 
LVL 4

Assisted Solution

by:lhl60
lhl60 earned 150 total points
ID: 24419425
To ask the user to reset the program is never a good idea, if the application  truly  is stopped any memory leaks will be cleared, but its very annoying for the user and doest leave the impression of a trustworthy product ( " if it does this, what happens to my data ??" ).

Inspect your code into the details, it's often easy to spot if you make any allocations that are not released, new's made in the constructor should be deleted in the destructor.
 please note it's common that some API's calls return data that you have to release, If data "magically" appear (you didn't allocate it) after any data base calls you might have to delete it, read the documentation.

There might be some exceptions that you either ignore or catch "on the other" side of a new, and thus never get deleted.

Sounds like a hard nut to crack, if your program can survive 4-5 hours it must be very small chucks of memory that are leaked - difficult to find.

Before assuming you have memory leaks you should verify that is really is leaks, you can see you memory usage in the taskmanager ( not fine granulated ) process explorer might help you
http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx
A wonder full program for many things





0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 150 total points
ID: 24419652
>> it's often easy to spot if you make any allocations that are not released
I admire your optimism :)

Ultimately, the best way to solve leak issues is to code to avoid them by using the RAII idiom.
http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization

>> new's made in the constructor should be deleted in the destructor.
Sadly, that doesn't actually always work... exceptions in the constructor mean the destructor isn't called... causing a leak if resources have already been allocated prior to the exception. Again, RAII will solve this without needing to write any additional code.
0
 
LVL 4

Assisted Solution

by:lhl60
lhl60 earned 150 total points
ID: 24419754
evilrix: Being a sad amateur  I hadn't heard about RAII. good way to avoid the exception problem...
Will use it my self , thanks.

Waitin2learnman: you problem could also be caused, by memory exchanged between threads ( e.g: from your main program to your dialog boxes and v.s.). Please also note some of the Cdialog events must be passed on to your parent class (Cdialog)
e.g:

int Myclass::IinitIinstance()
{
   Cdialog:: initinstance() ;    / / let Cdialog handle default contructions etc.

  ... you code
}

and

int Myclass: exitinstance()
{
.. your code

return CDialog:exitinstance();     // required cleanup.
}

0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 200 total points
ID: 24419807
IMHO - this sounds like a resource leak, just if there is a memory leak then it is quickly determined to be happening.  All that needs is for the app to be run in DEBUG mode from the IDE and look at the output pane.
0
 
LVL 4

Assisted Solution

by:lhl60
lhl60 earned 150 total points
ID: 24419840
I agree

0
 

Author Comment

by:Wanting2LearnMan
ID: 24427826
Thanks for all the comments.

I have built my program in debug mode and started to run it in the emulator.  
The first dialog screen shows up fine but when I press a button to launch the next 'main menu' screen I get an error at line 1. below:  (Please see the attached file for the error).  

void CBegin::OnOkButton()
{
       1. CMainMenu dlgMainMenu;
       2. dlgMainMenu.DoModal();
}

Perhaps this is nothing or it could be an indication of a problem.

I appreciate any help you can give me.
Error.jpg
0
 

Author Comment

by:Wanting2LearnMan
ID: 24430268
>>All that needs is for the app to be run in DEBUG mode from the IDE and look at the output pane.

I cant run it in the output pane due to the above error.   :o(  ANy ideas?
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 200 total points
ID: 24443413
Looks like you have used the stack up.
Is something getting called recursively?
0
 

Author Comment

by:Wanting2LearnMan
ID: 24443552
I ran my program in debug and stepped through it and noticed some things which were showing to be a problem in debug mode.
For example:

In my OnPaint I was Loading a bitmap like so:
CBitmap bmp;
bmp.LoadBitmap(IDB_ARROWUP_BITMAP);      

and I forgot to do
bmp.DeleteObject(); afterwards.  This caused the debugger to crash.

Also I had some strange behaviour when I tried to launch dialogs where the program would also crash in debug mode so I increased the stack reserve also from the default 64k to 128K.  Hopefully this will not just 'delay' the problem.
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 200 total points
ID: 24448189
The destructor of the CBitmap (and other display objects) should do any DeleteObject calls for you.


>>In my OnPaint I was Loading a bitmap like so:
Why not have a member var of the class as the bitmap and load just once in the OnInitDialog ?
0
 

Author Comment

by:Wanting2LearnMan
ID: 24449052
>>Why not have a member var of the class as the bitmap and load just once in the OnInitDialog ?
Ok I will do that :)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

If you are anything like me, you install many apps on your phone and have your life on it, sometimes literally.  When I bought my current phone, a Samsung Galaxy S5 from Verizon, they were only selling the versions with 16 GB internal.  I didn't rea…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

690 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