Jump to content
Fórum Script Brasil
  • 0

liberar memoria RAM entre chamadas de função


fesoalll
 Share

Question

Preciso de uma ajuda com esse codigo.

Toda vez que a função screen() é chamada a memoria alocada não é liberada, fazendo aumentar o uso da memoria RAM até travar o pc.

Como faço pra liberar essa memoria , segue o codigo:

#include <iostream>
#ifdef WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif // win32

using namespace std;
void sleepcp(int milliseconds);
char filename[64];
FILE* f;

void screen(char* fileName)
{
    HWND window = GetDesktopWindow();
    HDC _dc = GetWindowDC(window);
    HDC dc = CreateCompatibleDC(0);
    
    RECT re;
    GetWindowRect(window, &re);
    int w = re.right,
        h = re.bottom;
    void* buf = malloc(w*h*4);
    
    HBITMAP bm = CreateCompatibleBitmap(_dc, w, h);
    SelectObject(dc, bm);
    
    StretchBlt(dc, 0, 0, w, h, _dc, 0, 0, w, h, SRCCOPY);
    
    void* f =CreateFile(fileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
    
    GetObject(bm, 84, buf);
    
    tagBITMAPINFO bi;
    bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
    bi.bmiHeader.biWidth = w;
    bi.bmiHeader.biHeight = h;
    bi.bmiHeader.biPlanes = 1;
    bi.bmiHeader.biBitCount = 32;
    bi.bmiHeader.biCompression = 0;
    bi.bmiHeader.biSizeImage = 0;
    
    CreateDIBSection(dc, &bi, DIB_RGB_COLORS, &buf, 0, 0);
    GetDIBits(dc, bm, 0, h, buf, &bi, DIB_RGB_COLORS);
        
    BITMAPFILEHEADER bif;
    bif.bfType = MAKEWORD('B','M');
    bif.bfSize = w*h*4+54;
    bif.bfOffBits = 54;

    BITMAPINFOHEADER bii;
    bii.biSize = 40;
    bii.biWidth = w;
    bii.biHeight = h;
    bii.biPlanes = 1;
    bii.biBitCount = 32;
    bii.biCompression = 0;
    bii.biSizeImage = w*h*4;
    
    DWORD r;
    WriteFile(f, &bif, sizeof(bif), &r, NULL);
    WriteFile(f, &bii, sizeof(bii), &r, NULL);
    WriteFile(f, buf, w*h*4, &r, NULL);
    buf=NULL;
    
    free(buf);
    CloseHandle(window);
    CloseHandle(f);
    DeleteDC(_dc);
    DeleteDC(dc);
}

void sleepcp(int milliseconds) // cross-platform sleep function
{
    #ifdef WIN32
    Sleep(milliseconds);
    #else
    usleep(milliseconds * 1000);
    #endif // win32
}
int main()
{
    int A=0;
    char img[30];
for(;;)
{

  sprintf(img,"image%.3i.bmp",A);
  screen(img);
  printf("Imagem%.3i.bmp foi salva !\n",A);
sleepcp(8000);
A++;
}
return 0;
}

 

ATT.

Link to comment
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Forum Statistics

    • Total Topics
      149.6k
    • Total Posts
      646.2k
×
×
  • Create New...