我不知道为什么在第一个测试用例中会发生内存泄漏(肯定是假阳性),但是在相同的第二个测试中却没有检测到泄漏。
我怀疑opensl稍后会释放内存,而不是至少在rsa结构下调用RSA_free时会释放内存。
TEST(TestCaseName, test1) {
_CrtMemState state1;
_CrtMemCheckpoint(&state1);
auto rsa=RSA_new();
RSA_free(rsa);
_CrtMemState state2;
_CrtMemCheckpoint(&state2);
_CrtMemState state3;
if (_CrtMemDifference(&state3, &state1, &state2))//memory leak detected
_CrtMemDumpStatistics(&state3);
}
TEST(TestCaseName, test2) {
_CrtMemState state1;
_CrtMemCheckpoint(&state1);
auto rsa = RSA_new();
RSA_free(rsa);
_CrtMemState state2;
_CrtMemCheckpoint(&state2);
_CrtMemState state3;
if (_CrtMemDifference(&state3, &state1, &state2))//ok no leak
_CrtMemDumpStatistics(&state3);
}
第一个测试的_CrtMemDumpStatistics输出如下:
0 bytes in 0 Free Blocks.
48 bytes in 2 Normal Blocks.
0 bytes in 0 CRT Blocks.
0 bytes in 0 Ignore Blocks.
0 bytes in 0 Client Blocks.
Largest number used: 0 bytes.
Total allocations: 168 bytes.
第二次测试没有输出。 有谁知道为什么在第一次测试中检测到内存泄漏而在第二次测试中一切正常吗?