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. Memory Problems!!!

Memory Problems!!!

Scheduled Pinned Locked Moved C / C++ / MFC
performancehelpquestion
8 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.
  • P Offline
    P Offline
    Paddy
    wrote on last edited by
    #1

    I hope someone can figure this one out for me! I have a simple function that takes 400 double values and graphs them in the client area. I run this about 25 times per second to give 25 FPS. This works fine however it uses up all the memory in my computer after about ten minutes!!! In the "Windows Task Manager" the mem usage just keeps increasing until my computer crashes. I'm sure it is this function that is causing the problem because when I leave it out this doesn't happen. Any ideas? void function3 (HWND hwnd, double a[], int iLength) { HDC hdc; PAINTSTRUCT ps; int i; hdc = GetDC(hwnd); Rectangle(hdc, 0, 0, 400, 256); MoveToEx(hdc, 0, 256, NULL); for (i=1; i< 400; i = i++) { int p = (int)a[i]; LineTo(hdc, i, 255-p); } EndPaint (hwnd, &ps) ; } Thanks in advance, Paddy

    R N 2 Replies Last reply
    0
    • P Paddy

      I hope someone can figure this one out for me! I have a simple function that takes 400 double values and graphs them in the client area. I run this about 25 times per second to give 25 FPS. This works fine however it uses up all the memory in my computer after about ten minutes!!! In the "Windows Task Manager" the mem usage just keeps increasing until my computer crashes. I'm sure it is this function that is causing the problem because when I leave it out this doesn't happen. Any ideas? void function3 (HWND hwnd, double a[], int iLength) { HDC hdc; PAINTSTRUCT ps; int i; hdc = GetDC(hwnd); Rectangle(hdc, 0, 0, 400, 256); MoveToEx(hdc, 0, 256, NULL); for (i=1; i< 400; i = i++) { int p = (int)a[i]; LineTo(hdc, i, 255-p); } EndPaint (hwnd, &ps) ; } Thanks in advance, Paddy

      N Offline
      N Offline
      Nitron
      wrote on last edited by
      #2

      Paddy wrote: void function3 (HWND hwnd, double a[], int iLength) { How often do you call this fcn? the double a[] with no bounds checking seems kinda shady... If I can recall, every time you call this fcn, you will make a copy of a[]. I would get the for loop out of this fcn, and pass a as a const reference. Also, why even use a double if you're casting to int X| And also, where do you set the size of a[]? I'm not a guru, but the whole thing looks a little shady. - Nitron


      "Those that say a task is impossible shouldn't interrupt the ones who are doing it." - Chinese Proverb

      P 1 Reply Last reply
      0
      • P Paddy

        I hope someone can figure this one out for me! I have a simple function that takes 400 double values and graphs them in the client area. I run this about 25 times per second to give 25 FPS. This works fine however it uses up all the memory in my computer after about ten minutes!!! In the "Windows Task Manager" the mem usage just keeps increasing until my computer crashes. I'm sure it is this function that is causing the problem because when I leave it out this doesn't happen. Any ideas? void function3 (HWND hwnd, double a[], int iLength) { HDC hdc; PAINTSTRUCT ps; int i; hdc = GetDC(hwnd); Rectangle(hdc, 0, 0, 400, 256); MoveToEx(hdc, 0, 256, NULL); for (i=1; i< 400; i = i++) { int p = (int)a[i]; LineTo(hdc, i, 255-p); } EndPaint (hwnd, &ps) ; } Thanks in advance, Paddy

        R Offline
        R Offline
        Rickard Andersson20
        wrote on last edited by
        #3

        Paddy wrote: hdc = GetDC(hwnd); You have to call ReleaseDC() if you've called GetDC() Rickard Andersson@Suza Computing C# and C++ programmer from SWEDEN! UIN: 50302279 E-Mail: nikado@pc.nu Speciality: I love C#, ASP.NET and C++!

        P 1 Reply Last reply
        0
        • R Rickard Andersson20

          Paddy wrote: hdc = GetDC(hwnd); You have to call ReleaseDC() if you've called GetDC() Rickard Andersson@Suza Computing C# and C++ programmer from SWEDEN! UIN: 50302279 E-Mail: nikado@pc.nu Speciality: I love C#, ASP.NET and C++!

          P Offline
          P Offline
          Paddy
          wrote on last edited by
          #4

          Thanks thats makes a big difference, I'm only losing a couple of Kb per minute now.

          1 Reply Last reply
          0
          • N Nitron

            Paddy wrote: void function3 (HWND hwnd, double a[], int iLength) { How often do you call this fcn? the double a[] with no bounds checking seems kinda shady... If I can recall, every time you call this fcn, you will make a copy of a[]. I would get the for loop out of this fcn, and pass a as a const reference. Also, why even use a double if you're casting to int X| And also, where do you set the size of a[]? I'm not a guru, but the whole thing looks a little shady. - Nitron


            "Those that say a task is impossible shouldn't interrupt the ones who are doing it." - Chinese Proverb

            P Offline
            P Offline
            Paddy
            wrote on last edited by
            #5

            I'm calling this function about 25 times per second, I know the code is a bit of a mess but I'm just worried about the increasing memory problem. I have included ReleaseDC(hwnd, hdc); now and it has made a significant difference but I'm still losing a bit of memory! I can't actually get rid of the for loop because I'm using this to draw lines between each point on the graph. Thanks for the help.

            N 1 Reply Last reply
            0
            • P Paddy

              I'm calling this function about 25 times per second, I know the code is a bit of a mess but I'm just worried about the increasing memory problem. I have included ReleaseDC(hwnd, hdc); now and it has made a significant difference but I'm still losing a bit of memory! I can't actually get rid of the for loop because I'm using this to draw lines between each point on the graph. Thanks for the help.

              N Offline
              N Offline
              Nitron
              wrote on last edited by
              #6

              Try:

              void function3 (HWND hwnd, double& a[400], int iLength)
              {
              HDC hdc;
              PAINTSTRUCT ps;
              int i = 0;

              hdc = GetDC(hwnd);

              Rectangle(hdc, 0, 0, 400, 256);

              MoveToEx(hdc, 0, 256, NULL);

              for (i=1; i< 400; i++) // <-- why start @ i=1 and not 0??
              {
              //int p = (int)a[i];
              LineTo(hdc, i, (255 - static_cast<int>(a[i]));
              }

              EndPaint (hwnd, &ps) ;
              }

              It may not solve all the memory issues, but may speed you up some and maybe ease it a little. - Nitron


              "Those that say a task is impossible shouldn't interrupt the ones who are doing it." - Chinese Proverb

              P 1 Reply Last reply
              0
              • N Nitron

                Try:

                void function3 (HWND hwnd, double& a[400], int iLength)
                {
                HDC hdc;
                PAINTSTRUCT ps;
                int i = 0;

                hdc = GetDC(hwnd);

                Rectangle(hdc, 0, 0, 400, 256);

                MoveToEx(hdc, 0, 256, NULL);

                for (i=1; i< 400; i++) // <-- why start @ i=1 and not 0??
                {
                //int p = (int)a[i];
                LineTo(hdc, i, (255 - static_cast<int>(a[i]));
                }

                EndPaint (hwnd, &ps) ;
                }

                It may not solve all the memory issues, but may speed you up some and maybe ease it a little. - Nitron


                "Those that say a task is impossible shouldn't interrupt the ones who are doing it." - Chinese Proverb

                P Offline
                P Offline
                Paddy
                wrote on last edited by
                #7

                Thanks I have it fixed now, there was another part in my app where I was creating a colored pen over and over again using hPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 255)); and not deleting it when I was finished. Just stuck in DeleteObject(hPen); and it's all sorted now! Thanks for the help, Paddy

                R 1 Reply Last reply
                0
                • P Paddy

                  Thanks I have it fixed now, there was another part in my app where I was creating a colored pen over and over again using hPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 255)); and not deleting it when I was finished. Just stuck in DeleteObject(hPen); and it's all sorted now! Thanks for the help, Paddy

                  R Offline
                  R Offline
                  Ravi Bhavnani
                  wrote on last edited by
                  #8

                  My product MemWatcher[^] will help identify if you're leaking resources. /ravi Let's put "civil" back in "civilization" http://www.ravib.com ravib@ravib.com

                  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