我正在尝试理解在将
编辑:valgrind没有帮助,无论我是否释放内存,它都没有显示任何信息。
X509
对象推入STACK_OF(X509)
结构后是否需要释放它们,或者sk_X509_free()
函数调用是否会包括内容在内一并释放。我在OpenSSL中没有找到相关文档。请注意,sk_X509_free()
会释放所有内容。std::vector<std::string> caPems;
// Fill the vector from input
// ...
BIO *bufio = NULL;
X509 *x509 = NULL, *x509_ca = NULL;
bool success = false;
STACK_OF(X509)* x509_ca_stack;
x509_ca_stack = sk_X509_new_null();
if (x509_ca_stack) {
success = true;
for (const std::string& caPem : caPems) {
BIO_new_mem_buf(caPem.c_str(), caPem.size());
PEM_read_bio_X509(bufio, &x509_ca, NULL, NULL);
BIO_free_all(bufio);
if (x509_ca != nullptr) {
sk_X509_push(x509_ca_stack, x509_ca);
x509_ca = NULL; // should I free after push???
} else
success = false;
}
if (success)
foo(x509_ca_stack);
sk_X509_free(x509_ca_stack); // or is this free enough for the entire stack?
} else {
printf("ERROR: failed loading cert\n");
}
编辑:valgrind没有帮助,无论我是否释放内存,它都没有显示任何信息。
sk_X509_push()
时会复制数据。sk_X509_push()
调用OPENSSL_sk_push()
,它又调用OPENSSL_sk_insert()
。因此,您应该释放本地数据的副本。期待其他评论。 - Frankie_C