Howdy, Stranger!

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

Categories

VisualC++ LineTo() call causes high usage of resouce, causing more RAM

adepumadhuadepumadhu Member Posts: 1
When VC++ "LineTo" statement is called very often (say every 100 ms), it is using more resource and it is not getting released. If my application is running for longer period (say 2 hours), high usage of resource problem causing more RAM usage and memory is not getting released and growing continuously.

Sample code is given below (Please send your personal email id, so that I can email complete code)

[code]//------------------------------------------------------------------

//Callback method creation (100 ms)
m_nEventID = timeSetEvent(100,0,GeneratePeriodicCall,DWORD(this),TIME_PERIODIC);


//callback method
void CALLBACK GeneratePeriodicCall(UINT uID, UINT UMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
{
CTestAppView* pView = (CTestAppView*) dwUser;

//kill thread if active
if(g_hDrawChartThread != INVALID_HANDLE_VALUE)
{
DWORD dwExitCode =NULL;

DWORD dw = NULL;

GetExitCodeThread(g_hDrawChartThread, &dwExitCode);

if(dwExitCode == STILL_ACTIVE)
{
::CloseHandle(g_hDrawChartThread);

g_hDrawChartThread = NULL;
}
else
{
g_hDrawChartThread = NULL;
}
}

//create thread
g_hDrawChartThread = CreateThread(NULL,0,DrawChart,pView,0,0);
}


//Thread call
DWORD __stdcall DrawChart(LPVOID lParam)
{
CTestAppView* pView = (CTestAppView*)lParam;

CClientDC dc(pView);

int nSaveDC = dc.SaveDC();

long x = 0;

long y = 0;

for(long i = 0 ; i < 100000 ;i++)
{
dc.MoveTo(x,y);

//THIS LINE IS CAUSING SERIOUS RESOURCE LEAK
dc.LineTo(x + 1, y + 1);

x = x + 1;

y = y + 1;
}

dc.RestoreDC(nSaveDC);

return 0;
}
//------------------------------------------------------------------[/code]

Anyone can clarify me why "LineTo()" is not releasing resources & utilising more RAM. This RAM memory is not getting released until unless I close my VC++ application.

Any solution/hint, welcome.

Thanks in advance,
Madhu

Comments

  • LundinLundin Member Posts: 3,711
    A guess would be that the DC used by the main thread collides with the DC used in the "LineTo" thread. Calling SaveDC() and such functions with the DC passed as parameter doesn't look thread safe - what happens if you get a context switch in the middle of that function call and the main thread starts running WM_PAINT?

    Put a mutex around SaveDC and RestoreDC and see if it solves anything.
  • AsmGuru62AsmGuru62 Member Posts: 6,519
    [color=Blue]Another issue is that CloseHandle() will not really "kill thread". There are other APIs (dangerous ones) for it. Killing the running thread may cause all kind of issues. You need to signal the thread to exit on itself (from that huge loop of 100000 times) by using the interlocked API.

    CDC::LineTo() is a simple wrapper around an API call - the chance that it leaks is very small. If it was - it would have been noticed over the years of people running Windows.

    If you wish - I can take a look at full code.[/color]
  • LundinLundin Member Posts: 3,711
    Oh yeah, that is likely the main issue. CloseHandle only tells the system that "you may toss this thread away now, I don't want it any longer". The thread will still execute its work until finished. You'd need something like

    for(long i = 0 ; !timeToDie && i < 100000 ;i++)

    where timeToDie is a plain bool variable you can set from main. Then you can wait for the thread to actually finish through WaitForSingleObject(), before calling CloseHandle().


    (There is also the "TerminateThread()" function, but as mentioned, it is dangerous. That function will only give you trouble, and may result in further resource leaks.)
Sign In or Register to comment.