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};
    void * pvThreadData = NULL;
    wchar_t * pwcPortName = NULL;

    /* 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__);

    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.


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

transfering Text in text view from listview item from activity to a dialog box

android listview android-listview

i am tearing my hair out at this and cannot seem to get it working. I have an activity that has a listview, the single row in that list view consist of Three textviews and an imageview. Im trying t...

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.