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. malloc, free problem ......

malloc, free problem ......

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++graphicsperformancequestion
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.
  • _ Offline
    _ Offline
    _crs_
    wrote on last edited by
    #1

    hello, i have a problem .... a can not free memory .... i have here 3 pointers iBuffer, jByffer, pDib at the end of the function I want to free iBuffer and pDib and it's get me an error when I run the program ... if I remove the "free" lines it works fine but is not right.... what can I do ??? please if anyone can help .... thank you ! void* DDBToDIB(HBITMAP _desktopBmp, DWORD *bmpsize) { BYTE *iBuffer, *iBufferTmp; int iWidth, iHeight; void *jBuffer; int jSize; BITMAPINFOHEADER head; void *pDib; BITMAP bm; GetObject(_desktopBmp, sizeof(BITMAP), (LPSTR) &bm); iHeight = bm.bmHeight; iWidth = bm.bmWidth; DWORD dwEffWidth = ((((bm.bmBitsPixel * bm.bmWidth) + 31) / 32) * 4); if (bm.bmBitsPixel == 8) head.biClrUsed = 256; else head.biClrUsed = 0; head.biSize = sizeof(BITMAPINFOHEADER); head.biWidth = bm.bmWidth; head.biHeight = bm.bmHeight; head.biPlanes = 1; head.biBitCount = (WORD)bm.bmBitsPixel; head.biCompression = BI_RGB; head.biSizeImage = dwEffWidth * bm.bmHeight; head.biClrImportant = 0; head.biXPelsPerMeter = 0; head.biYPelsPerMeter = 0; long mypalsize = (head.biClrUsed * sizeof(RGBQUAD)); long mysize = head.biSize + head.biSizeImage + mypalsize; pDib = malloc(mysize); BITMAPINFOHEADER* lpbi; lpbi = (BITMAPINFOHEADER*)(pDib); *lpbi = head; iBufferTmp = ((BYTE*)pDib + *(DWORD*)pDib + mypalsize); HDC dc = GetDC(NULL); GetDIBits(dc, _desktopBmp, 0, iHeight, iBufferTmp, (LPBITMAPINFO)pDib, DIB_RGB_COLORS); ReleaseDC(NULL, dc); iBuffer = (BYTE *)malloc(iHeight * iWidth * 3); for (int c = iHeight - 1; c >= 0; c--) { memcpy((BYTE *)iBuffer + ((iHeight - 1 - c) * iWidth * 3), (BYTE *)iBufferTmp + (c * iWidth * 3), iWidth * 3); } for (c = 0; c < iHeight * iWidth; c++) { iBufferTmp[c * 3] = iBuffer[c * 3 + 2]; iBufferTmp[c * 3 + 1] = iBuffer[c * 3 + 1]; iBufferTmp[c * 3 + 2] = iBuffer[c * 3]; } jBuffer = (BYTE*)Compress(iBufferTmp, iWidth, iHeight, 3, &jSize, 100); FILE *dest = fopen("sent.jpg", "wb"); fwrite(jBuffer, jSize, 1, dest); fclose(dest); // free(pDib); // free(iBuffer); *bmpsize = jSize; return jBuffer; }

    D 1 Reply Last reply
    0
    • _ _crs_

      hello, i have a problem .... a can not free memory .... i have here 3 pointers iBuffer, jByffer, pDib at the end of the function I want to free iBuffer and pDib and it's get me an error when I run the program ... if I remove the "free" lines it works fine but is not right.... what can I do ??? please if anyone can help .... thank you ! void* DDBToDIB(HBITMAP _desktopBmp, DWORD *bmpsize) { BYTE *iBuffer, *iBufferTmp; int iWidth, iHeight; void *jBuffer; int jSize; BITMAPINFOHEADER head; void *pDib; BITMAP bm; GetObject(_desktopBmp, sizeof(BITMAP), (LPSTR) &bm); iHeight = bm.bmHeight; iWidth = bm.bmWidth; DWORD dwEffWidth = ((((bm.bmBitsPixel * bm.bmWidth) + 31) / 32) * 4); if (bm.bmBitsPixel == 8) head.biClrUsed = 256; else head.biClrUsed = 0; head.biSize = sizeof(BITMAPINFOHEADER); head.biWidth = bm.bmWidth; head.biHeight = bm.bmHeight; head.biPlanes = 1; head.biBitCount = (WORD)bm.bmBitsPixel; head.biCompression = BI_RGB; head.biSizeImage = dwEffWidth * bm.bmHeight; head.biClrImportant = 0; head.biXPelsPerMeter = 0; head.biYPelsPerMeter = 0; long mypalsize = (head.biClrUsed * sizeof(RGBQUAD)); long mysize = head.biSize + head.biSizeImage + mypalsize; pDib = malloc(mysize); BITMAPINFOHEADER* lpbi; lpbi = (BITMAPINFOHEADER*)(pDib); *lpbi = head; iBufferTmp = ((BYTE*)pDib + *(DWORD*)pDib + mypalsize); HDC dc = GetDC(NULL); GetDIBits(dc, _desktopBmp, 0, iHeight, iBufferTmp, (LPBITMAPINFO)pDib, DIB_RGB_COLORS); ReleaseDC(NULL, dc); iBuffer = (BYTE *)malloc(iHeight * iWidth * 3); for (int c = iHeight - 1; c >= 0; c--) { memcpy((BYTE *)iBuffer + ((iHeight - 1 - c) * iWidth * 3), (BYTE *)iBufferTmp + (c * iWidth * 3), iWidth * 3); } for (c = 0; c < iHeight * iWidth; c++) { iBufferTmp[c * 3] = iBuffer[c * 3 + 2]; iBufferTmp[c * 3 + 1] = iBuffer[c * 3 + 1]; iBufferTmp[c * 3 + 2] = iBuffer[c * 3]; } jBuffer = (BYTE*)Compress(iBufferTmp, iWidth, iHeight, 3, &jSize, 100); FILE *dest = fopen("sent.jpg", "wb"); fwrite(jBuffer, jSize, 1, dest); fclose(dest); // free(pDib); // free(iBuffer); *bmpsize = jSize; return jBuffer; }

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

      _crs_ wrote: it's get me an error when I run the program Such as? _crs_ wrote: pDib = malloc(mysize); iBuffer = (BYTE *)malloc(iHeight * iWidth * 3); Here, pDib and iBuffer are not being checked against NULL. While malloc() is probably not failing, it would behoove you to step through the code and see at what point either of these variables is being stepped on, since an invalid pointer passed to free() can cause errors.

      _ 1 Reply Last reply
      0
      • D David Crow

        _crs_ wrote: it's get me an error when I run the program Such as? _crs_ wrote: pDib = malloc(mysize); iBuffer = (BYTE *)malloc(iHeight * iWidth * 3); Here, pDib and iBuffer are not being checked against NULL. While malloc() is probably not failing, it would behoove you to step through the code and see at what point either of these variables is being stepped on, since an invalid pointer passed to free() can cause errors.

        _ Offline
        _ Offline
        _crs_
        wrote on last edited by
        #3

        the error is "DAMAGE: after normal block (#xx)" and a made all pointers NULL befor malloc and the same ... however I resolved with pDib by lpbi = NULL; and now it works but for iBuffer don't ! if I remove the following lines /* for (c = 0; c < iHeight * iWidth; c++) { iBufferTmp[c * 3] = iBuffer[c * 3 + 2]; iBufferTmp[c * 3 + 1] = iBuffer[c * 3 + 1]; iBufferTmp[c * 3 + 2] = iBuffer[c * 3]; }*/ everything works but ... I need them .....

        D 1 Reply Last reply
        0
        • _ _crs_

          the error is "DAMAGE: after normal block (#xx)" and a made all pointers NULL befor malloc and the same ... however I resolved with pDib by lpbi = NULL; and now it works but for iBuffer don't ! if I remove the following lines /* for (c = 0; c < iHeight * iWidth; c++) { iBufferTmp[c * 3] = iBuffer[c * 3 + 2]; iBufferTmp[c * 3 + 1] = iBuffer[c * 3 + 1]; iBufferTmp[c * 3 + 2] = iBuffer[c * 3]; }*/ everything works but ... I need them .....

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

          If you attempt to free a block of memory after overwriting the end of its allocation, you will get the "DAMAGE: after normal block (#xx)" error.

          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