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