In multithreaded ex allocating heap memory

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gnanapoongothai
    New Member
    • Jun 2007
    • 62

    In multithreaded ex allocating heap memory

    hi,
    the error i am getting in main for allocating heap memory for threads is
    [code=c]
    #include <stdio.h>
    #include "winsock2.h "
    #include <stdlib.h>
    #include <windows.h>
    #include <strsafe.h>
    #define BUF_SIZE 255
    #define nofthreads 2

    typedef struct data
    {
    char a[36];
    }char_36;
    // Initialize Winsock.
    WSADATA wsaData1,wsaDat a2;
    // Create a socket.
    SOCKET m_socket1,m_soc ket2;
    SOCKADDR_IN client1Service, client2Service;
    BOOL wiatall = TRUE;
    CRITICAL_SECTIO N cs;

    // Send and receive data.
    //int bytesSent;
    int bytesRecv1,byte sRecv2 = SOCKET_ERROR;
    //char sendbuf[32] = "Client: Sending data.";
    char recvbuf1[36] = "";
    char recvbuf2[36] = "";

    DWORD WINAPI workerProc( LPVOID lpParam )
    {
    char recvbuf1[36] = "";
    int status ;
    size_t cchStringSize;
    int iResult;

    TCHAR msgBuf[BUF_SIZE];



    iResult = WSAStartup( 0X0202, &wsaData1 );
    if ( iResult != NO_ERROR )

    StringCchPrintf (msgBuf, BUF_SIZE, TEXT("error at WSASTARTUP"));



    m_socket1 = socket( AF_INET, SOCK_STREAM,IPP ROTO_TCP);

    if ( m_socket1 == INVALID_SOCKET )
    {

    StringCchPrintf (msgBuf, BUF_SIZE, TEXT("error at socket"));

    WSACleanup();
    return(0);
    }


    client1Service. sin_family = AF_INET;
    client1Service. sin_addr.s_addr = inet_addr( "192.168.0. 239" );
    client1Service. sin_port = htons(27015);

    do
    {
    status = connect( m_socket1, (SOCKADDR *) &client1Service , sizeof(client1S ervice) );
    StringCchPrintf (msgBuf, BUF_SIZE, TEXT("failed to connect"));
    WSACleanup();
    return(0);
    }while(status == SOCKET_ERROR);
    StringCchPrintf (msgBuf, BUF_SIZE, TEXT("connected to client 1"));

    while( bytesRecv1 == SOCKET_ERROR )
    {

    bytesRecv1 = recv( m_socket1, recvbuf1, 36, 0 );
    if ( bytesRecv1 == 0 || bytesRecv1 == WSAECONNRESET )
    {
    StringCchPrintf (msgBuf, BUF_SIZE, TEXT("connectio n closed"));
    break;
    }
    if (bytesRecv1 < 0)
    return(0);
    StringCchPrintf (msgBuf, BUF_SIZE, TEXT("bytes received"));
    }
    HeapFree(GetPro cessHeap(), 0, recvbuf1);

    return(0);
    }

    DWORD WINAPI ThreadProc( LPVOID lpParam )
    {
    TCHAR msgBuf[BUF_SIZE];
    size_t cchStringSize;
    char recvbuf2[36] = "";
    int iResult = WSAStartup( 0X0202, &wsaData2 );
    if ( iResult != NO_ERROR )

    StringCchPrintf (msgBuf, BUF_SIZE, TEXT("error at WSASTARTUP"));

    m_socket2 = socket( AF_INET, SOCK_STREAM,IPP ROTO_TCP);

    if ( m_socket2 == INVALID_SOCKET )
    {
    StringCchPrintf (msgBuf, BUF_SIZE, TEXT("error at socket"));
    WSACleanup();
    return(0);
    }


    client2Service. sin_family = AF_INET;
    client2Service. sin_addr.s_addr = inet_addr( "192.168.0. 091" );
    client2Service. sin_port = htons(27016);

    do
    {
    connect( m_socket2, (SOCKADDR *) &client2Service , sizeof(client2S ervice) );
    StringCchPrintf (msgBuf, BUF_SIZE, TEXT("failed to connect"));
    WSACleanup();
    return(0);
    }while(connect == SOCKET_ERROR);

    StringCchPrintf (msgBuf, BUF_SIZE, TEXT("Connected to client 2"));
    while( bytesRecv2 == SOCKET_ERROR )
    {

    bytesRecv2 = recv( m_socket2, recvbuf2, 36, 0 );
    if ( bytesRecv2 == 0 || bytesRecv2 == WSAECONNRESET )
    {
    StringCchPrintf (msgBuf, BUF_SIZE, TEXT("connectio n closed"));
    break;
    }
    if (bytesRecv2 < 0)
    return(0);
    StringCchPrintf (msgBuf, BUF_SIZE, TEXT("bytes received"));
    }
    HeapFree(GetPro cessHeap(), 0, recvbuf2);

    return(0);
    }



    int main()
    {
    DWORD i=1;
    HANDLE hThread,access;
    DWORD dwThreadId,Idth read;
    int Prio=2;
    recvbuf1 = HeapAlloc(GetPr ocessHeap(), HEAP_ZERO_MEMOR Y,sizeof(char_3 6));

    recvbuf2 = HeapAlloc(GetPr ocessHeap(), HEAP_ZERO_MEMOR Y,sizeof(char_3 6));




    hThread = CreateThread(
    NULL, // default security attributes
    0, // use default stack size
    ThreadProc, // thread function
    recvbuf1, // argument to thread function
    0, // use default creation flags
    NULL); // returns the thread identifier
    access = CreateThread(
    NULL, // default security attributes
    0, // use default stack size
    workerProc, // thread function
    recvbuf2, // argument to thread function
    0, // use default creation flags
    NULL); // returns the thread identifier



    if (hThread == NULL)
    {
    ExitProcess(1);
    }
    if(access == NULL)
    {
    ExitProcess(2);
    }
    WaitForMultiple Objects(1,hThre ad,TRUE,INFINIT E);
    //WaitForSingleOb ject(hThread,IN FINITE);
    CloseHandle(hTh read);
    CloseHandle(acc ess);

    return(0);
    }
    [/code]



    \..\integration \integration\Cl ient.c(150): error: expression must be a modifiable lvalue
    recvbuf2 = HeapAlloc(GetPr ocessHeap(), HEAP_ZERO_MEMOR Y,sizeof(char_3 6));

    char_36 i have used

    typedef struct data
    {
    char a[36];
    }char_36;

    what could the error.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    The error is in the way you declare your data

    [code=c]
    char recvbuf1[36] = "";
    char recvbuf2[36] = "";

    DWORD WINAPI workerProc( LPVOID lpParam )
    {
    char recvbuf1[36] = "";
    ...
    }
    [/code]
    You have declared recvbuf1 and recvbuf2 as arrays of char, HeapAlloc returns a pointer (void *) and it sould be allocated to a variable with a pointer type not an array identifier.

    Then additionally in workerProc (and in ThreadProc) you declare receiveBuf1 again hiding your global definition of it. If you want the data on the heap I would have thought you could declare and allocated it inside workerProc removing the need to global data.

    Comment

    • gnanapoongothai
      New Member
      • Jun 2007
      • 62

      #3
      Originally posted by Banfa
      The error is in the way you declare your data

      [code=c]
      char recvbuf1[36] = "";
      char recvbuf2[36] = "";

      DWORD WINAPI workerProc( LPVOID lpParam )
      {
      char recvbuf1[36] = "";
      ...
      }
      [/code]
      You have declared recvbuf1 and recvbuf2 as arrays of char, HeapAlloc returns a pointer (void *) and it sould be allocated to a variable with a pointer type not an array identifier.

      Then additionally in workerProc (and in ThreadProc) you declare receiveBuf1 again hiding your global definition of it. If you want the data on the heap I would have thought you could declare and allocated it inside workerProc removing the need to global data.
      i have removed the heap allocation entirely and made null in thread create and now there is a access violation error

      Unhandled exception at 0x7c809576 in Client.exe: 0xC0000005: Access violation reading location 0x000017f4.

      the thread s are being created and again in the main the error is

      > 2968 __tmainCRTStart up _CrtIsValidHeap Pointer Normal 0

      how to debug the program

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Have you also removed the calls to HeapFree?

        Run the program in the debugger from your IDE. When it stops view the call stack to locate the point in your program where it has gone wrong.

        Comment

        • gnanapoongothai
          New Member
          • Jun 2007
          • 62

          #5
          i have removed the heapfree command also now the error lies in the
          waitformultiple instance()
          there are two threads with the handels access and hThread now how should i give the wait,

          waitformultiple instance(2,hThr ead,access,TRUE ,INFINITE) is this ok or how to specify two handels.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            The second parameter is wrong, it needs to be an array of handles to wait on. You are passing a single handle.

            Comment

            • gnanapoongothai
              New Member
              • Jun 2007
              • 62

              #7
              i changed the handels to array like hthread[0] and hthread[1] then given an array in the waitformultiple objects(2,hthre ad,TRUE,INFINTE )
              then while debuuging threads are created and close handel are also being executed while coming to the return state ment the error is like

              Run-Time Check Failure #2 - Stack around the variable 'hThread.12113' was corrupted.

              what could have possibly went wrong.
              thanx for ur help bafna,

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                Most likely is that somehow you have written outside the bounds of the array.

                Comment

                Working...