-2

C++ Win32Api GUI, I Am trying to display received screenshots on server from client. I am trying to create a Remote Desktop Viewer. Client is capturing screenshots and then sending them to the server, where i am trying to receive and display to a window, but there are is a problem on which i am stuck at maybe a week. It is throwing std::bad_alloc when i received the image and try to add to memory on SERVER.(the screenshots are in jpeg format). Sometimes it throws std::bad_alloc instantly, but sometimes after received a few images.

Server.cpp {window procedure, the bad_alloc error is in WM_PAINT}:

    case WM_PAINT:
    {
        SOCKET selectedSocket = clientSockets[itemIndex];

        Sleep(1000 / 15);
        ULONGLONG bufferLenNetworkOrder = 0;
        int bytesReceived = 0;
        int rerr = 0;
        const int MAX_BYTES = 100000;
        while (bytesReceived < sizeof(bufferLenNetworkOrder)) {
            rerr = recv(selectedSocket, (char*)&bufferLenNetworkOrder + bytesReceived, sizeof(bufferLenNetworkOrder) - bytesReceived, 0);
            bytesReceived += rerr;
        }
        int bufferLen = ntohll(bufferLenNetworkOrder);

        imageBuffer.reset(new char[bufferLen]);
        bytesReceived = 0;

        while (bytesReceived < bufferLen && bufferLen < MAX_BYTES) {
            MessageBoxA(NULL, std::to_string(bufferLen - bytesReceived).c_str(), "CHECK", MB_OK);
            rerr = recv(selectedSocket, imageBuffer.get() + bytesReceived, bufferLen - bytesReceived, 0);
            if (rerr > 0 && rerr <= bufferLen - bytesReceived) {
                bytesReceived += rerr;
            }
            else {
                MessageBoxA(NULL, "ERROR", "CHECK", MB_OK);
                return {};
            }
        }

        if (bufferLen > MAX_BYTES)
        {
            return {};
        }

        HGLOBAL hGlobal = GlobalAlloc(GHND, bufferLen);
        void* pData = GlobalLock(hGlobal);
        memcpy(pData, imageBuffer.get(), bufferLen);

        GlobalUnlock(hGlobal);

        IStream* pStream = NULL;
        CreateStreamOnHGlobal(hGlobal, TRUE, &pStream);

        Gdiplus::Bitmap bitmap(pStream);
        Gdiplus::Status status = bitmap.GetLastStatus();


        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(Chwnd, &ps);

        int imgWidth = bitmap.GetWidth();
        int imgHeight = bitmap.GetHeight();

        Gdiplus::Graphics graphics(hdc);

        RECT clientRect;
        GetClientRect(Chwnd, &clientRect);

        graphics.DrawImage(&bitmap, 0, 0, imgWidth, imgHeight);
        GlobalFree(hGlobal);
        pStream->Release();
        imageBuffer.reset(new char[bufferLen]);
        EndPaint(Chwnd, &ps);
        
        break;
    }
Rocka
  • 59
  • 7
  • 1
    `imageBuffer.reset(new char[bufferLen]);` maybe `bufferLen` is less than 0 or a really big number. In either case if you had a debugger you could easily look when it crashes and know that. – drescherjm Jan 23 '23 at 22:41
  • 1
    Why `auto it = std::find(clientSockets.begin(), clientSockets.end(), selectedSocket);`? You are not even using the iterator. – Ted Lyngmo Jan 23 '23 at 22:43
  • updated the code already but still cant make it work :/ i think it will be a problem adding it to memory from `HGLOBAL hGlobal = GlobalAlloc(GHND, bufferLen); ...` – Rocka Jan 23 '23 at 23:05
  • 3
    My advice is to not update the code until you understand the cause of the problem. Debug it first to determine why the allocation size is invalid. Or if you are writing a 32 bit application you could have a leak and have actually ran out of address space. – drescherjm Jan 23 '23 at 23:09
  • 1
    GlobalAlloc() is an OS API and it will not throw std::bad_alloc. `new` can throw bad_alloc. – drescherjm Jan 23 '23 at 23:27
  • 1
    Too many things going on in this code, many places where a failure could happen. Please reduce the code down to a [mcve]. – Remy Lebeau Jan 23 '23 at 23:35
  • it got fixed already, by checking the bufferLen, thanks drescherjm. – Rocka Jan 24 '23 at 15:19

1 Answers1

0

I fixed it by checking if the bufferLen is not zero or less than zero in a do-while loop. Big thanks to drescherjm.

Rocka
  • 59
  • 7