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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Memory leak

Memory leak

Scheduled Pinned Locked Moved C / C++ / MFC
c++hardwaresecurityperformancehelp
4 Posts 2 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.
  • G Offline
    G Offline
    gunnar66
    wrote on last edited by
    #1

    Hi It seems like my exe prog. have a memory leak problem. The program read from the lpCmdLine, 6 different files, merges this to one output file and then close. I close all handle before i Exit.Is there something i have to do, to free memory? I call the exe file 15 times per minute. By the way the program is programmed in Embedded C++, and runs on a CE panel. Some code: #include "stdafx.h" #include "Afx.h" #include "Winuser.h" #include "stdlib.h" int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { DWORD dwBytesRead, dwBytesWritten, dwPos; char buff[4096]; TCHAR* filnavn1; TCHAR* filnavn2; TCHAR* filnavn3; TCHAR* filnavn4; TCHAR* filnavn5; TCHAR* filnavn6; TCHAR* filnavnhtml; DWORD dwBytesRead, dwBytesWritten, dwPos; HANDLE hFile1, hFile2, hFile3, hFile4, hFile5, hFile6, hAppend; hFile1 = CreateFile (filnavn1, // Open .txt GENERIC_READ, // Open for reading 0, // Do not share NULL, // No security OPEN_EXISTING, // Existing file only FILE_ATTRIBUTE_NORMAL, // Normal file NULL); // No template file //I open 6 files hAppend = CreateFile (filnavnhtml, // Create output file. GENERIC_WRITE, // Open for writing 0, // Do not share NULL, // No security CREATE_ALWAYS, // Open or create FILE_ATTRIBUTE_NORMAL, // Normal file NULL); // No template file dwPos = SetFilePointer (hAppend, 0, NULL, FILE_END); do { if (ReadFile (hFile1, buff, 4096, &dwBytesRead, NULL)) { WriteFile (hAppend, buff, dwBytesRead, &dwBytesWritten, NULL); } } while (dwBytesRead == 4096); // I do this with all 6 files //close all files CloseHandle (hAppend); CloseHandle (hFile1); Return 0; } Thanks

    D 1 Reply Last reply
    0
    • G gunnar66

      Hi It seems like my exe prog. have a memory leak problem. The program read from the lpCmdLine, 6 different files, merges this to one output file and then close. I close all handle before i Exit.Is there something i have to do, to free memory? I call the exe file 15 times per minute. By the way the program is programmed in Embedded C++, and runs on a CE panel. Some code: #include "stdafx.h" #include "Afx.h" #include "Winuser.h" #include "stdlib.h" int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { DWORD dwBytesRead, dwBytesWritten, dwPos; char buff[4096]; TCHAR* filnavn1; TCHAR* filnavn2; TCHAR* filnavn3; TCHAR* filnavn4; TCHAR* filnavn5; TCHAR* filnavn6; TCHAR* filnavnhtml; DWORD dwBytesRead, dwBytesWritten, dwPos; HANDLE hFile1, hFile2, hFile3, hFile4, hFile5, hFile6, hAppend; hFile1 = CreateFile (filnavn1, // Open .txt GENERIC_READ, // Open for reading 0, // Do not share NULL, // No security OPEN_EXISTING, // Existing file only FILE_ATTRIBUTE_NORMAL, // Normal file NULL); // No template file //I open 6 files hAppend = CreateFile (filnavnhtml, // Create output file. GENERIC_WRITE, // Open for writing 0, // Do not share NULL, // No security CREATE_ALWAYS, // Open or create FILE_ATTRIBUTE_NORMAL, // Normal file NULL); // No template file dwPos = SetFilePointer (hAppend, 0, NULL, FILE_END); do { if (ReadFile (hFile1, buff, 4096, &dwBytesRead, NULL)) { WriteFile (hAppend, buff, dwBytesRead, &dwBytesWritten, NULL); } } while (dwBytesRead == 4096); // I do this with all 6 files //close all files CloseHandle (hAppend); CloseHandle (hFile1); Return 0; } Thanks

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

      gunnar66 wrote: It seems like my exe prog. have a memory leak problem. How do you know this? Are you allocating memory for filnavn1 and filnavnhtml?


      "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

      G 1 Reply Last reply
      0
      • D David Crow

        gunnar66 wrote: It seems like my exe prog. have a memory leak problem. How do you know this? Are you allocating memory for filnavn1 and filnavnhtml?


        "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

        G Offline
        G Offline
        gunnar66
        wrote on last edited by
        #3

        Hi In the CE panel the used memory is growing when my exe prog is running. I am not allocating any memory for filnavn1 and filnavnhtml

        D 1 Reply Last reply
        0
        • G gunnar66

          Hi In the CE panel the used memory is growing when my exe prog is running. I am not allocating any memory for filnavn1 and filnavnhtml

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

          gunnar66 wrote: I am not allocating any memory for filnavn1 and filnavnhtml They are declared as pointers. Shouldn't you be allocating memory for them like:

          TCHAR *filnavn1 = new TCHAR[some_size];
          ...
          delete [] filnavn1;

          Or since they are used for filename purposes, you could just use a stack-based buffer of size MAX_PATH like:

          TCHAR filnavn1[MAX_PATH];


          "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

          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