jeudi 30 mai 2019

c++11 list push_back() instanciation fault?

I am adding elements to a c++11 list via push_back(). Before adding an element I am printing out a debug message via cout. When I now remove the cout statement, the values in the element I append to the list with push_back are corrupted. I can test this with googletest.

Following you see the working code. When i comment out the cout statement the values in the last element of the sensor_scan_list get seemingly corrupted. The error lies in my opinion either in the instanciation of the list or in the code of the test. Only the state variable of the current_sensor.state seems corrupted (see below).

When I leave the cout statement in, everything works perfectly fine. It is such a weird issue, has someone a clue why this could happen?

code snippet:

void MessageHandler::processSensorHeader(TelemetryMessageInterface * p_Message)
{
    cout << "DEBUG: sensor header element added\n";
    addSensorHeader(p_Message);
    send_message(encoder->confirm(((SensorHeaderMessage *)p_Message)->getMessageId())); //ACK
}

void MessageHandler::addSensorHeader(TelemetryMessageInterface * p_message)
{
    Sensor new_sensor;

    new_sensor.manufacturerId = ((SensorHeaderMessage *)p_message)->getManufacturerId();
    new_sensor.deviceId = ((SensorHeaderMessage *)p_message)->getDeviceId();
    new_sensor.state |= SENSOR_HEADER_DETECTED;

    sensor_scan_list.push_back(new_sensor);
}

Googletest code:

TEST(MessageHandlerTest, processSensorHeader)
{
TelemetryEncoder * encoder = new TelemetryEncoder();
TelemetryMessageInterface * p_Message;
MessageHandler message_handler;
Sensor current_sensor;

list <Sensor> :: iterator p_List;

p_Message = encoder->encodeSensorHeader(0x0101,0x0202,0x0303); 
//add sensor
message_handler.processSensorHeader(p_Message);

p_List = message_handler.sensor_scan_list.begin();

current_sensor = *p_List;
EXPECT_EQ(current_sensor.deviceId, 0x0303);
EXPECT_EQ(current_sensor.manufacturerId, 0x0202);
EXPECT_EQ(current_sensor.state, 1);

p_Message = encoder->encodeSensorHeader(0x0203,0x0302,0x0403); 
//add new sensor
message_handler.processSensorHeader(p_Message);

current_sensor = *++p_List;
EXPECT_EQ(current_sensor.deviceId, 0x0403);
EXPECT_EQ(current_sensor.manufacturerId, 0x0302);
EXPECT_EQ(current_sensor.state, 1);

p_Message = encoder->encodeSensorHeader(0x0102,0x0202,0x0303);
message_handler.processSensorHeader(p_Message);

current_sensor = *++p_List;
EXPECT_EQ(current_sensor.deviceId, 0x0303);
EXPECT_EQ(current_sensor.manufacturerId, 0x0202);
EXPECT_EQ(current_sensor.state, 1);
}

Sensor class:

class Sensor : public sensor_interface {
public:
Sensor();
virtual ~Sensor();

uint16_t manufacturerId;
uint16_t deviceId;

char name[SENSOR_INFORMATION_LENGTH];

uint8_t state;
};

Googletest output for broken code (cout commented out):

[ RUN      ] MessageHandlerTest.processSensorHeader
../test/inc/test_messageHandler.cpp:77: Failure
  Expected: current_sensor.state
  Which is: '\xC1' (193)
To be equal to: 1
../test/inc/test_messageHandler.cpp:93: Failure
  Expected: current_sensor.state
  Which is: '\xF1' (241)
To be equal to: 1
[  FAILED  ] MessageHandlerTest.processSensorHeader (0 ms)

Aucun commentaire:

Enregistrer un commentaire