修复内存检测的bug

This commit is contained in:
2024-12-03 18:38:56 +08:00
parent f2a470c59d
commit c43236aa58
4 changed files with 32 additions and 26 deletions

View File

@@ -104,4 +104,10 @@ class LatchRecipe(ConanFile):
)
def package_info(self):
self.cpp_info.libs = ["latch"]
self.cpp_info.components["latch"].libs = ["latch"]
self.cpp_info.components["latch"].set_property("pkg_config_name", "latch")
self.cpp_info.components["latch_debug"].libs = ["latch_debug"]
self.cpp_info.components["latch_debug"].set_property(
"pkg_config_name", "latch_debug"
)

View File

@@ -4,16 +4,15 @@
#include <stdlib.h>
#include <string.h>
static lt_memory_debug_info_list_t LT_MEMORY_DEBUG_INFO_LIST = {
NULL, NULL, 0};
static lt_memory_debug_info_list_t LT_MEMORY_DEBUG_INFO_LIST = {NULL, NULL, 0};
/**
* @brief 内存调试信息添加
* @param file 文件名
* @param line 行号
* @param ptr 内存地址
* @param size 内存大小
*/
* @brief 内存调试信息添加
* @param file 文件名
* @param line 行号
* @param ptr 内存地址
* @param size 内存大小
*/
void lt_memory_debug_info_add(const char *file,
int line,
void *ptr,
@@ -28,11 +27,10 @@ void lt_memory_debug_info_add(const char *file,
info->next = NULL;
if (LT_MEMORY_DEBUG_INFO_LIST.head == NULL) {
LT_MEMORY_DEBUG_INFO_LIST.head = info;
LT_MEMORY_DEBUG_INFO_LIST.tail = info;
} else {
LT_MEMORY_DEBUG_INFO_LIST.tail->next = info;
LT_MEMORY_DEBUG_INFO_LIST.tail = info;
}
LT_MEMORY_DEBUG_INFO_LIST.tail = info;
LT_MEMORY_DEBUG_INFO_LIST.size++;
}
@@ -55,32 +53,35 @@ void lt_memory_debug_print_info() {
}
/**
* @brief 内存调试信息移除指定内存记录
*/
* @brief 内存调试信息移除指定内存记录
*/
bool lt_memory_debug_info_remove_prt(void *ptr) {
lt_memory_debug_info_t *i = LT_MEMORY_DEBUG_INFO_LIST.head;
lt_memory_debug_info_t *pre = NULL;
// 找到该内存记录
while (i != NULL) {
if (i->ptr == ptr) {
if (pre != NULL)
pre->next = i->next;
else
LT_MEMORY_DEBUG_INFO_LIST.head = i->next;
free(i->file);
free(i);
break;
}
pre = i;
i = i->next;
}
if (i == NULL)
return false;
free(i->file);
free(i);
LT_MEMORY_DEBUG_INFO_LIST.size--;
if (LT_MEMORY_DEBUG_INFO_LIST.size == 0) {
LT_MEMORY_DEBUG_INFO_LIST.head = NULL;
LT_MEMORY_DEBUG_INFO_LIST.tail = NULL;
} else if (i == NULL) {
return false;
} else {
LT_MEMORY_DEBUG_INFO_LIST.tail = pre;
}
return true;
}

View File

@@ -44,8 +44,7 @@ void lt_free_direct(void *ptr, char *file, int line) {
#endif
void lt_memory_set_functions(void *(*malloc_func)(size_t size),
void *(*calloc_func)(size_t nmemb,
size_t size),
void *(*calloc_func)(size_t nmemb, size_t size),
void *(*realloc_func)(void *ptr, size_t size),
void (*free_func)(void *ptr)) {
__lt_malloc = malloc_func;

View File

@@ -8,7 +8,7 @@ add_executable(
)
target_link_libraries(
${PROJECT_NAME}_string
latch
latch_debug
gtest::gtest
)
gtest_discover_tests(${PROJECT_NAME}_string)