Edit: I solved the double free error myself, however the question (question 1) why it behaves differently from command line and inside CLion still remains...
I am trying to use gmock and gtest. I want to mock a function that takes a pointer as an argument and fills it with content. My mock class has the following functions:
//MockUdpServer class
MOCK_METHOD(int, receive_msg_impl, (unsigned char *buf, int sec, int usec));
virtual int receive_msg(unsigned char *buf, int sec = -1, int usec = -1) override {
return receive_msg_impl(buf, sec, usec);
}
My test code looks as follows:
TEST(Action, ref) {
MockUdpServer mock_server;
unsigned char test[]={0x0, 0x61}
EXPECT_CALL(mock_server, receive_msg_impl)
.WillOnce(SetArrayArgument<0>(test, test+2));
unsigned char a[256];
mock_server.receive_msg(a);
}
Now after the call to receive_msg
I expect array a
to have the content of test
. When I execute this in CLion a
just has random content. When I build it separately with cmake and run it, I get the error message
double free or corruption (out)
Aborted (core dumped)
There are two things that confuse me:
- Why is there a difference between running inside CLion and running it from command line? I guess it could be some compiler flags?
- (This setup does work under different circumstances, e.g. I have another object, which takes the
MockUdpServer
and internally callsreceive_msg
on it. This call is successful. Why does it not work directly in the test itself? Answer: In the other test cases I used DoAll and specified both the return value and the array content.)
Edit: Apparently the test code has to specify the return value, even though it is not being used. The following code does not produce any error:
TEST(Action, ref) {
MockUdpServer mock_server;
unsigned char test[]={0x0, 0x61}
EXPECT_CALL(mock_server, receive_msg_impl)
.WillOnce(DoAll(
SetArrayArgument<0>(test, test+2),
Return(2)));
unsigned char a[256];
mock_server.receive_msg(a);
}