我正在开发一些模块来验证 Linux 中 C++ 的签名。 而且,我正在 Visual Studio 2015 中测试和调试它。 但是,对于内存泄漏测试,我不知道如何检测它。
首先,我尝试使用vld.h和crtdbg.h,它们都无法检测Bio*等openssl元素内存泄漏。或 X509*。 还有其他工具支持检测 openssl 元素的内存泄漏吗?
当然,我尝试使用“int CRYPTO_mem_leaks(Bio *)”,但它被定义为如下所示的注释,并且在 Visual Studio 中我不知道如何使它们处于事件状态。 像:
# ifndef OPENSSL_NO_CRYPTO_MDEBUG
# define OPENSSL_mem_debug_push(info) \
CRYPTO_mem_debug_push(info, OPENSSL_FILE, OPENSSL_LINE)
# define OPENSSL_mem_debug_pop() \
CRYPTO_mem_debug_pop()
int CRYPTO_mem_debug_push(const char *info, const char *file, int
line);
int CRYPTO_mem_debug_pop(void);
void CRYPTO_get_alloc_counts(int *mcount, int *rcount, int *fcount);
/*-
* Debugging functions (enabled by CRYPTO_set_mem_debug(1))
* The flag argument has the following significance:
* 0: called before the actual memory allocation has taken place
* 1: called after the actual memory allocation has taken place
*/
void CRYPTO_mem_debug_malloc(void *addr, size_t num, int flag,
const char *file, int line);
void CRYPTO_mem_debug_realloc(void *addr1, void *addr2, size_t num,
int flag,
const char *file, int line);
void CRYPTO_mem_debug_free(void *addr, int flag,
const char *file, int line);
int CRYPTO_mem_leaks_cb(int (*cb) (const char *str, size_t len, void *u),
void *u);
# ifndef OPENSSL_NO_STDIO
int CRYPTO_mem_leaks_fp(FILE *);
# endif
int CRYPTO_mem_leaks(BIO *bio);
# endif
最佳答案
来自 CRYPTO_set_mem_functions() 的文档:
If no allocations have been done, it is possible to "swap out" the default implementations for OPENSSL_malloc(), OPENSSL_realloc and OPENSSL_free() and replace them with alternate versions (hooks). CRYPTO_get_mem_functions() function fills in the given arguments with the function pointers for the current implementations. With CRYPTO_set_mem_functions(), you can specify a different set of functions. If any of m, r, or f are NULL, then the function is not changed.
如果您怀疑在某些情况下出现某些问题,您可以使用此机制插入您自己的内存分配跟踪函数或创造性地设置断点。在使用 Windows 时,您可以调用 _malloc_dbg() , _realloc_dbg()和 _free_dbg()来自这些钩子(Hook)内部的函数。
更一般地说,the Visual Studio debugger and C Run-time Library (CRT) can help you detect and identify memory leaks 。在我看来,这是最好的起点。从头到尾阅读该页面将为您提供很多工具来解决内存泄漏问题。
要获得全面的洞察力、逐步执行 OpenSSL 代码并获得完整的调试功能,最好在启用调试的情况下在本地重建 OpenSSL 库(如果您尚未这样做)。
关于visual-studio-2015 - 使用openssl时如何检测内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53593527/