SetTimer and Thread

Hi,I'm looking for some help using SetTimer & thread.
What I want to do is: keep checking the memory every 3 seconds. If the memory is low, I popup a message and ask the user to Exit from the application or close other applications and continue with the present one. There are Stop & Continue buttons. From the time I popup this dialog box, I have to wait only for 20 seconds. If there's no response from the user, I simply assume that he wants to quit the application and exit. However, before I quit, I want to recheck the memory status now. If the memory is still low, I have to Exit. Otherwise, close the dialog and continue with the application. But the memory check should continue as long as the application is running.

I created a thread. starting a timer within the thread callback. The timer procedure will actually do all the memcheck. if
there's less memory, i display a message with Stop & Continue buttons. Call another timer for about 15 seconds.

If there's no user input (Stop or Continue), the second timer proc is called, it should again check the memory. if the memory is still less then post a WM_QUIT msg, otherwise, kill the second timer and continue with the application.

If there's a user input, "Stop" I call WM_QUIT msg and for "Continue", kill both the timers and call the memory check function again.

The reason for creating the thread is I dont want the timer to be running in the main thread.

The reason for starting the 2nd timer is, I want to allow some time for the memory refresh and meanwhile the user closes other applications and release some memory.

But by the end of the second timer (15 seconds), the memory is still not released, I post a forcible quit of the main
application and exit.

However, I have only one timer proc for both the timers.

Now the prob:
The condition for the second timer in the timer proceduce is never satisfied. As a result, the second timer runs in an infinite loop using 100% CPU time and high memory and never gets killed.

Given below the sample code. Could anyone figure out what could be the prob. Also any other better way of doing it.

Thanks in advance.



#define AUTO_CLOSE_MEM_ERR_DLG 100

MemCheck( )
{
//Here goes Createthread code.
}

// Tread callback
ThreadProc( )
{
iTimer1 = ::SetTimer(NULL,1, 15000, (TIMERPROC) timerProc);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

// timer callback
timerProc( UINT nIDEvent)
{
//Here goes the mem check code

if (nIDEvent == AUTO_CLOSE_MEM_ERR_DLG)
{
// IT NEVER COMES TO THIS CODE
KillTimer(NULL, AUTO_CLOSE_MEM_ERR_DLG);
if (AvlMem < 1024)
{
g_bContinue = FALSE;
StopTimer();
PostMessage(pFrame->m_hWnd, WM_QUIT,0,0);
}
else
g_bContinue = TRUE;
}
else
{
if (AvlMem < 1024)
{
//Low Memory dialog should come here with Stop&Continue buttons.
SetTimer(NULL, AUTO_CLOSE_MEM_ERR_DLG, 15000, (TIMERPROC) timerProc);
}
}
}

OnStop()
{
StopTimer();
// Exit from appln
}

OnContinue()
{
StopTimer();
MemCheck();
}

«1

Comments

  • [blue]Somehow, I do not get it... why do you need to go to such lengths? If you want to know when Windows is close to "Out of memory" status - just respond to WM_COMPACTING message in your main application window.[/blue]
  • : [blue]Somehow, I do not get it... why do you need to go to such lengths? If you want to know when Windows is close to "Out of memory" status - just respond to WM_COMPACTING message in your main application window.[/blue]
    :

    Hi, some how I didnt see this message. How will WM_COMPACTING help. What I understand from MSDN is that it will increase the CPU compacting time but it doesnot resolve hte problem. My problem is the product bloats memory and the system runs out of memory and crashes. Adding more memory doesnt seem to help.
  • : [blue]Somehow, I do not get it... why do you need to go to such lengths? If you want to know when Windows is close to "Out of memory" status - just respond to WM_COMPACTING message in your main application window.[/blue]
    :

    Hi, some how I didnt see this message. How will WM_COMPACTING help. What I understand from MSDN is that it will increase the CPU compacting time but it doesnot resolve hte problem. My problem is the product bloats memory and the system runs out of memory and crashes. Adding more memory doesnt seem to help.
  • : : [blue]Somehow, I do not get it... why do you need to go to such lengths? If you want to know when Windows is close to "Out of memory" status - just respond to WM_COMPACTING message in your main application window.[/blue]
    : :
    :
    : Hi, some how I didnt see this message. How will WM_COMPACTING help. What I understand from MSDN is that it will increase the CPU compacting time but it doesnot resolve hte problem. My problem is the product bloats memory and the system runs out of memory and crashes. Adding more memory doesnt seem to help.
    :
    [green]To me, it sounds as there's a memory leak in the pgm. Try and fix it (if you have the source code). Otherwise, ask the vendor to fix it (or look for a patch or an update).[/green]
  • : : : [blue]Somehow, I do not get it... why do you need to go to such lengths? If you want to know when Windows is close to "Out of memory" status - just respond to WM_COMPACTING message in your main application window.[/blue]
    : : :
    : :
    : : Hi, some how I didnt see this message. How will WM_COMPACTING help. What I understand from MSDN is that it will increase the CPU compacting time but it doesnot resolve hte problem. My problem is the product bloats memory and the system runs out of memory and crashes. Adding more memory doesnt seem to help.
    : :
    : [green]To me, it sounds as there's a memory leak in the pgm. Try and fix it (if you have the source code). Otherwise, ask the vendor to fix it (or look for a patch or an update).[/green]
    :
    [blue]Tell me more about the program and the compiler... I know how to fix leaks with VC++.[/blue]
  • : : : [blue]Somehow, I do not get it... why do you need to go to such lengths? If you want to know when Windows is close to "Out of memory" status - just respond to WM_COMPACTING message in your main application window.[/blue]
    : : :
    : :
    : : Hi, some how I didnt see this message. How will WM_COMPACTING help. What I understand from MSDN is that it will increase the CPU compacting time but it doesnot resolve hte problem. My problem is the product bloats memory and the system runs out of memory and crashes. Adding more memory doesnt seem to help.
    : :
    : [green]To me, it sounds as there's a memory leak in the pgm. Try and fix it (if you have the source code). Otherwise, ask the vendor to fix it (or look for a patch or an update).[/green]
    :
    There are no memory leaks or atleast very minimal. But looks like MFC and some COM APIs hogs memory. We are using lot of COM stuffs. Its a thick client with many GUI controls. There isnt any problem with NT and 2000, but the worst victims are Win9x.
    I'm looking at why it could happen so in Win9x even though I supply more memory.
    Secondly, why MFC applications use so much memory. Could there be memory leaks in MFC dlls.
    I also observed creating a simple win32 app and a MFC app. Execute MFC app, on win95, 3% of user and system resource drops down. But the win32 app does not show any difference in the resource usage.
    Secondly, open dev studio compile some project, close dev studio. It does not release the whole memory that was before opening dev studio.

    Any suggestions or URL with related info will help me.

    Thanks.
  • [blue]So, it is VC++... now, before you say there is no leaks - did you check them? Did you do some diagnostics? How did you do it?

    Now, look at the 3rd party products used in your application. There may be leaks there which will not be reported, because, say COM or DLL are built as Release and no tracking is done inside it.

    How does Win9x report about a problem?
    Can you send an image of a box or whatever you are getting?

    It looks interesting.

    Also, I think the answer to your original question will not help you
    to recover from such stuff...[/blue]
  • : [blue]So, it is VC++... now, before you say there is no leaks - did you check them? Did you do some diagnostics? How did you do it?
    :
    : Now, look at the 3rd party products used in your application. There may be leaks there which will not be reported, because, say COM or DLL are built as Release and no tracking is done inside it.
    :
    : How does Win9x report about a problem?
    : Can you send an image of a box or whatever you are getting?
    :
    : It looks interesting.
    :
    : Also, I think the answer to your original question will not help you
    : to recover from such stuff...[/blue]
    :

    He does, indeed, seem to be treating the symptom not the cause. There is, however, insufficient information to really continue a diagnosis.

    Med venlig hilsen,

    Adrian...

  • : : [blue]So, it is VC++... now, before you say there is no leaks - did you check them? Did you do some diagnostics? How did you do it?
    : :
    : : Now, look at the 3rd party products used in your application. There may be leaks there which will not be reported, because, say COM or DLL are built as Release and no tracking is done inside it.
    : :
    : : How does Win9x report about a problem?
    : : Can you send an image of a box or whatever you are getting?
    : :
    : : It looks interesting.
    : :
    : : Also, I think the answer to your original question will not help you
    : : to recover from such stuff...[/blue]
    : :
    :
    : He does, indeed, seem to be treating the symptom not the cause. There is, however, insufficient information to really continue a diagnosis.
    :
    : Med venlig hilsen,
    :
    : Adrian...
    :
    :

    Thanks for your responses.
    We have tried using boundschecker, but it doesnt show any drastic leak in our application.

    But when we execute the app, the resource meter on Win9x shows about 90% of memory usage. Sometimes windows message pops up for "out of memory" and in many cases, it takes us directly to the "blue screen".
    There's no problem on NT/Win2K as I believe they support a better virtual memory management system.

    Well this is the problem. What all we tried:

    1. Adding GBs of memory to a Win9x system doesnot help a bit.
    2. Possible causes are memory leaks, have checked and have been checking still.
    3. Developed some utilities that give us the free memory available at frequent intervals while the application is running. (The first question was for this as I was trying to use SetTimer. But yes, I managed with threads instead of timers).
    4. Our appln uses VC COM at large. Converted one COM app to a dll and tested both of them. While dll shows no increase in memory usage, COM drops free memory by 2-3%. But it releases after Release( ) is called.
    But right at this moment we are sure that it might be a solution. As converting all the COM is going to pay us offa lot.
    5. Just on curiosity, I created simple win32, mfc apps. Executing Win32 app doesnt consume memory, but the mfc shows about 2-3% drop in memory. The memory sometimes does not get released.
    6. This is the case if we simply open dev studio and compile some project and close dev studio, it does not release complete memory.

    Well, yes I should check for memory leaks in our 3rd products. But I'm not sure if the actual problem is just a memory leak or something else.

    A little more on the product, its a client-server architecture and backend is Sybase server.

    If I can get any directions to proceed will help me lots.

    Thanks.
  • : : [blue]So, it is VC++... now, before you say there is no leaks - did you check them? Did you do some diagnostics? How did you do it?
    : :
    : : Now, look at the 3rd party products used in your application. There may be leaks there which will not be reported, because, say COM or DLL are built as Release and no tracking is done inside it.
    : :
    : : How does Win9x report about a problem?
    : : Can you send an image of a box or whatever you are getting?
    : :
    : : It looks interesting.
    : :
    : : Also, I think the answer to your original question will not help you
    : : to recover from such stuff...[/blue]
    : :
    :
    : He does, indeed, seem to be treating the symptom not the cause. There is, however, insufficient information to really continue a diagnosis.
    :
    : Med venlig hilsen,
    :
    : Adrian...
    :
    :

    Thanks for your responses.
    We have tried using boundschecker, but it doesnt show any drastic leak in our application.

    But when we execute the app, the resource meter on Win9x shows about 90% of memory usage. Sometimes windows message pops up for "out of memory" and in many cases, it takes us directly to the "blue screen".
    There's no problem on NT/Win2K as I believe they support a better virtual memory management system.

    Well this is the problem. What all we tried:

    1. Adding GBs of memory to a Win9x system doesnot help a bit.
    2. Possible causes are memory leaks, have checked and have been checking still.
    3. Developed some utilities that give us the free memory available at frequent intervals while the application is running. (The first question was for this as I was trying to use SetTimer. But yes, I managed with threads instead of timers).
    4. Our appln uses VC COM at large. Converted one COM app to a dll and tested both of them. While dll shows no increase in memory usage, COM drops free memory by 2-3%. But it releases after Release( ) is called.
    But right at this moment we are sure that it might be a solution. As converting all the COM is going to pay us offa lot.
    5. Just on curiosity, I created simple win32, mfc apps. Executing Win32 app doesnt consume memory, but the mfc shows about 2-3% drop in memory. The memory sometimes does not get released.
    6. This is the case if we simply open dev studio and compile some project and close dev studio, it does not release complete memory.

    Well, yes I should check for memory leaks in our 3rd products. But I'm not sure if the actual problem is just a memory leak or something else.

    A little more on the product, its a client-server architecture and backend is Sybase server.

    If I can get any directions to proceed will help me lots.

    Thanks.
  • [b][red]This message was edited by AsmGuru62 at 2003-4-4 14:40:17[/red][/b][hr]
    [code]5. Just on curiosity,
    I created simple win32, mfc apps. Executing Win32
    app doesnt consume memory, but the mfc shows
    about 2-3% drop in memory.
    The memory sometimes does not get released.[/code]
    [blue]MFC takes more - that's a given, but it is not leaking and as soon as application (or process) is gone - all consumed memory goes with it - it is not like DOS.

    Now, as I can see in MSDN the memory monitoring is not supported by Win9x - tough luck! Maybe the best is to check the code, see what is the biggest pieces will be allocated and monitor the release of that memory - just by tracking the code. If the memory usage bloated fast then this is a good method.

    So, how the leaks are monitored in your code?
    Did you call [italic]_CrtDumpMemoryLeaks()[/italic] or not?[/blue]



  • : [b][red]This message was edited by AsmGuru62 at 2003-4-4 14:40:17[/red][/b][hr]
    : [code]5. Just on curiosity,
    : I created simple win32, mfc apps. Executing Win32
    : app doesnt consume memory, but the mfc shows
    : about 2-3% drop in memory.
    : The memory sometimes does not get released.[/code]
    : [blue]MFC takes more - that's a given, but it is not leaking and as soon as application (or process) is gone - all consumed memory goes with it - it is not like DOS.
    :
    : Now, as I can see in MSDN the memory monitoring is not supported by Win9x - tough luck! Maybe the best is to check the code, see what is the biggest pieces will be allocated and monitor the release of that memory - just by tracking the code. If the memory usage bloated fast then this is a good method.
    :
    : So, how the leaks are monitored in your code?
    : Did you call [italic]_CrtDumpMemoryLeaks()[/italic] or not?[/blue]
    :
    Well yes, the problem is when the application is running.

    Perf Monitor is not supported in Win9x. But we have Resource Meter, which shows the % of user, system and GDI free memory.

    Memory leaks are checked using Numega's Boundschecker. We have not used _CrtDumpMemoryLeaks( ).

    My main doubt Is this a memory leak problem only.
  • : [b][red]This message was edited by AsmGuru62 at 2003-4-4 14:40:17[/red][/b][hr]
    : [code]5. Just on curiosity,
    : I created simple win32, mfc apps. Executing Win32
    : app doesnt consume memory, but the mfc shows
    : about 2-3% drop in memory.
    : The memory sometimes does not get released.[/code]
    : [blue]MFC takes more - that's a given, but it is not leaking and as soon as application (or process) is gone - all consumed memory goes with it - it is not like DOS.
    :
    : Now, as I can see in MSDN the memory monitoring is not supported by Win9x - tough luck! Maybe the best is to check the code, see what is the biggest pieces will be allocated and monitor the release of that memory - just by tracking the code. If the memory usage bloated fast then this is a good method.
    :
    : So, how the leaks are monitored in your code?
    : Did you call [italic]_CrtDumpMemoryLeaks()[/italic] or not?[/blue]
    :
    Well yes, the problem is when the application is running.

    Perf Monitor is not supported in Win9x. But we have Resource Meter, which shows the % of user, system and GDI free memory.

    Memory leaks are checked using Numega's Boundschecker. We have not used _CrtDumpMemoryLeaks( ).

    My main doubt Is this a memory leak problem only.
  • [blue]Tell me - when you look at the resource meter, can you see the resource consumption 'live'? What I mean: is this meter ALWAYS checks the status in a loop? If so, you can perform different parts of your program and look at the meter, maybe you can pinpoint the component/module which gives this effect? To narrow a code review a little bit...[/blue]
  • : [blue]Tell me - when you look at the resource meter, can you see the resource consumption 'live'? What I mean: is this meter ALWAYS checks the status in a loop? If so, you can perform different parts of your program and look at the meter, maybe you can pinpoint the component/module which gives this effect? To narrow a code review a little bit...[/blue]
    :
    The Resource meter is like the TaskManager of NT that shows the cpu and memory usage. Resource meter constantly checks for available memory resource (System, User and GDI resource) and reports. Well yes, I have monitored the reoursce consumption by performing some activities in our application. There's enuf GDI resoure but only system and user resources are eaten up. During download of files (FTP) it consumes lot of memory. I need to look into this. But there are other areas also which shows high consumption of resource.
    Leave this alone, I wonder why ever win9x not able to use memory whatever we add. Even if I add 256 or 512 MB of memory, it doesnt help us in any way. Same app runs fine with 64MB on NT and Win2K. Any idea what could be the cause for this.
    Another question, is there anyway I can check if one process is attempting to write on some other process address space. This could be the worse cause for BSOD.

    Thanks.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories