Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. VisualC++ LineTo() call causes high usage of resouce, causing more RAM usage - Memory Not Releasing

VisualC++ LineTo() call causes high usage of resouce, causing more RAM usage - Memory Not Releasing

Scheduled Pinned Locked Moved C / C++ / MFC
c++performancehelplearning
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    adepumadhu1
    wrote on last edited by
    #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) //------------------------------------------------------------------ //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; } //------------------------------------------------------------------ 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

    C P D 3 Replies Last reply
    0
    • A adepumadhu1

      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) //------------------------------------------------------------------ //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; } //------------------------------------------------------------------ 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

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      I don't know exactly why you encounter a memory leak, but one think that is sure is that you are doing it the wrong way: you should never access your user interface from a separate thread (which is what you are doing here). This is not the way to do it. Instead, in a separate thread, you can add data in a std::vector (for instance) which is protected against multi-threading access and then you send a message to the UI to refresh itself. The UI will then (in the main thread) extract information from the vector and draw the additional points. I don't know if this will fix your problem but this is something you'll have to fix or you'll encounter problems in the future.

      Cédric Moonen Software developer
      Charting control [v2.0] OpenGL game tutorial in C++

      1 Reply Last reply
      0
      • A adepumadhu1

        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) //------------------------------------------------------------------ //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; } //------------------------------------------------------------------ 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

        P Offline
        P Offline
        Perry Holman
        wrote on last edited by
        #3

        I don't think LineTo() causes high usage of resource&RAM. You are exactly doing it in wrong way. Firstly, you need to understand the callback function of a timer in VC can be re-called while previous call is not completed yet. Basically, each callback will create a thread. Secondly, I don't understand why you create a thread in the callback function, which will lead to run many threads at the same time. So i think running many threads at the same time causes your problem. If you want to update you UI, you can set a timer or a thread in your UI class which gets data and draw them on UI periodically. Meanwhile, there is another thead to prepare those drawing data.

        Welcome to www.softwaretree.net! This website is generated completely by static html pages transforming technology. You can find many excellent audio/video tools there!

        1 Reply Last reply
        0
        • A adepumadhu1

          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) //------------------------------------------------------------------ //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; } //------------------------------------------------------------------ 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

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          adepumadhu1 wrote:

          g_hDrawChartThread = CreateThread(NULL,0,DrawChart,pView,0,0);

          Are you calling this function 10 times per second?

          "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups