Free Allocated Memory Generates Exception - C++

There is a function in my application in which memory is allocated for formatting a port name. CreateFile is called to open the port. At the end of the function free is called to attempt to free the allocated memory.

DWORD CSerialPort::Open( wchar_t * port )
{
    DCB dcb = {0};
    LPTHREAD_START_ROUTINE pThreadStart;
    void * pvThreadData = NULL;
    wchar_t * pwcPortName = NULL;
    DWORD dwRetVal = ERROR_SUCCESS;

    /* Validate parameters. */

    pwcPortName = (wchar_t *)malloc( wcslen( port ) + 6 );

    if ( pwcPortName == NULL )
    {
        TRACE(_T("CSerialPort::Open : Failed to allocate memory for formatted serial port name.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), ERROR_NOT_ENOUGH_MEMORY, __WFILE__, __LINE__);
        return ERROR_NOT_ENOUGH_MEMORY;
    }

    memcpy( pwcPortName, L"\\\\.\\", 4 * 2 );
    memcpy( pwcPortName + 4, port, wcslen( port ) * 2 + 2 );

    // Get a handle to the serial port.
    _hSerialPort = CreateFile(
        pwcPortName,                    // Formatted serial port
        GENERIC_READ | GENERIC_WRITE,   // Access: Read and write
        0,                              // Share: No sharing
        NULL,                           // Security: None
        OPEN_EXISTING,                  // OM port already exists
        FILE_FLAG_OVERLAPPED,           // Asynchronous I/O
        NULL                            // No template file for COM port
        );

    if ( _hSerialPort == INVALID_HANDLE_VALUE )
    {
        TRACE(_T("CSerialPort::Open : Failed to get the handle to the serial port.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), ::GetLastError(), __WFILE__, __LINE__);
        return ::GetLastError();
    }

    /* Initialize the DCB structure with COM port parameters with BuildCommDCB. */

    /* Set the serial port communications events mask with SetCommMask. */

    /* Set serial port parameters with SetCommState. */

    /* Set the serial port communications timeouts with SetCommTimeouts. */

    /* Create thread to handle received data with CreateThread. */

    free( pwcPortName );                 // <-- Exception thrown here.

    return dwRetVal;
}

Can anyone tell me what I'm doing wrong? Thanks.

Answers


malloc allocates bytes, but you are using the allocated memory to store wchar_t.

You have to change the malloc size parameter to match your existing memcpy usage:

pwcPortName = (wchar_t *)malloc( wcslen( port ) + 6 );

should be

pwcPortName = (wchar_t *)malloc( (wcslen( port ) + 6) * sizeof(wchar_t));

Need Your Help

Running XUnit.net Tests with TestDriven.net

xunit.net testdriven.net

I've used NUnit for years and I wanted to try XUnit. So I installed XUnit and ran the executable that allowed you to run XUnit via TD.net.

Java split string at “,” and end of line

java string split

I have some data that looks like this:

JUnit test for console input and output

java junit io console user-input

I have only one method main. How to check System.out.println() and replace Scanner to input values automatically using JUnit?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.