Access violation when setting up a serial port

This feels like it should be a very simple solution, but I'm very much a Boost newbie, and relearning C++. I'm trying to set up a serial port in a class constructor of a legacy program I am updating. The serial port and io_service definitions are in the header file. The class is MFC. Header file declarations: boost::asio::serial_port port; boost::asio::io_service io; Here is the class constructor: CCplusplus_TesterDlg::CCplusplus_TesterDlg(CWnd* pParent) : CDialog(CCplusplus_TesterDlg::IDD, pParent), port(io, "COM3") { m_hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME); } If I comment out the port and io declaration and the port initialization from the constructor, I can pass the constructor fine in debug mode. However, if I leave them in, I get the following: First-chance exception at 0x771a6b90 in Cplusplus_Tester.exe: 0xC0000005: Access violation writing location 0xccccccd0. Unhandled exception at 0x771a6b90 in Cplusplus_Tester.exe: 0xC0000005: Access violation writing location 0xccccccd0. Debug mode breaks in win_mutex.hpp at line 56: ::EnterCriticalSection (&crit_section); Go ahead. Make me feel like an idiot. Any clues as to what I am doing wrong? Thanks! -- View this message in context: http://boost.2283326.n4.nabble.com/Access-violation-when-setting-up-a-serial... Sent from the Boost - Users mailing list archive at Nabble.com.

Ok. I just made myself look like an idiot! I forgot how literal and procedural C++ was! Solution: serial_port uses io_service in its constructor. Since C++ instantiates class members in the order in which they are declared in the header file, I simply had to move the declaration of the io_service above the declaration of the serial_port in the header file. I slowly stepped through debugging to confirm that nothing was breaking, and when it looked like I was in the clear, I cleared all breakpoints and restarted the program. Result: Success! Wow am I an idiot! Thanks anyway. -- View this message in context: http://boost.2283326.n4.nabble.com/Access-violation-when-setting-up-a-serial... Sent from the Boost - Users mailing list archive at Nabble.com.
participants (1)
-
Tangleman