diff --git a/cfg_parse/cfg_parser.cpp b/cfg_parse/cfg_parser.cpp index a5ccb7b..38a5fd9 100644 --- a/cfg_parse/cfg_parser.cpp +++ b/cfg_parse/cfg_parser.cpp @@ -3884,9 +3884,13 @@ int parse_rpt_log_ini() //lnk20250208不使用apr_pstrdup,使用固定大小,后续都在这块内存上直接复用 //ied_usr->LD_info[cpuno].LD_name = apr_pstrdup(g_init_pool, str);//domin // 从 g_init_pool 内存池中分配固定 256 字节的内存 - ied_usr->LD_info[cpuno].LD_name = (char *)apr_palloc(g_init_pool, 256); +// ied_usr->LD_info[cpuno].LD_name = (char *)apr_palloc(g_init_pool, 256);//lnk20250212重复申请删除 + + // 清空内存,防止残留数据 +// memset(ied_usr->LD_info[cpuno].LD_name, 0, 256); + // 将 str 中的内容复制到预先分配的内存中,最多复制 256 字节(包含结束符) - apr_cpystrn(ied_usr->LD_info[cpuno].LD_name, str, 256); +// apr_cpystrn(ied_usr->LD_info[cpuno].LD_name, str, 256); delete[] tmp; @@ -11756,12 +11760,13 @@ void ledger(const char* terminal_id, QIODevice* outputDevice) { void value_print(const char *variableName, QTcpSocket *clientSocket) { char buffer[256]; // 用于存储变量值的缓冲区 + pthread_mutex_lock(&mtx); // 打印变量值 if (strcmp(variableName, "frontindex") == 0) { sprintf(buffer, "frontindex = %d", g_front_seg_index); // 将 int 转换为字符串 clientSocket->write(buffer); // 发送字符串到客户端 } else if (strcmp(variableName, "iedcount") == 0) { - sprintf(buffer, "g_node->n_clients = %d, ied config count = %d", IED_COUNT); + sprintf(buffer, "g_node->n_clients = %d, ied config count = %d",g_node->n_clients ,IED_COUNT); clientSocket->write(buffer); } else if (strcmp(variableName, "frontfun") == 0) { sprintf(buffer, "frontfun = %s", subdir); @@ -11769,7 +11774,7 @@ void value_print(const char *variableName, QTcpSocket *clientSocket) { } else { clientSocket->write("Unknown variable name\n> "); } - + pthread_mutex_unlock(&mtx); clientSocket->flush(); } @@ -12516,9 +12521,13 @@ int parse_device_cfg_web() //lnk20250208不使用apr_pstrdup,后续直接复用 //ied_usr->LD_info[cpuno - 1].LD_name = apr_pstrdup(g_init_pool, str);//将 str 中的格式化字符串复制到内存池 g_init_pool 中。ied_usr->LD_info[cpuno - 1].LD_name 存储了这个字符串的副本,LD_name 现在是 PQMonitorPQM{cpuno} 的形式。 // 从 g_init_pool 内存池中分配固定 256 字节的内存 - ied_usr->LD_info[cpuno].LD_name = (char *)apr_palloc(g_init_pool, 256); + ied_usr->LD_info[cpuno - 1].LD_name = (char *)apr_palloc(g_init_pool, 256); + + // 清空内存,防止残留数据 + memset(ied_usr->LD_info[cpuno - 1].LD_name, 0, 256); + // 将 str 中的内容复制到预先分配的内存中,最多复制 256 字节(包含结束符) - apr_cpystrn(ied_usr->LD_info[cpuno].LD_name, str, 256); + apr_cpystrn(ied_usr->LD_info[cpuno - 1].LD_name, str, 256); //这里申请的空间基于ied的数量,挂载到ied上 ied_usr->LD_info[cpuno - 1].ht_fcd = apr_hash_make(g_init_pool); //这两行代码分别为 ied_usr->LD_info[cpuno - 1] 的两个成员(ht_fcd 和 ht_full_fcda)创建了空的哈希表。apr_hash_make(g_init_pool) 会在 g_init_pool 内存池中为这两个哈希表分配内存空间 @@ -14272,29 +14281,10 @@ int update_one_terminal_ledger(terminal* update, int i,ied_t* ied,int terminal_i loginfo_t **loginfo = NULL; rptinfo_t **rptinfo = NULL; - //这些可能是已有的内存,如果存在则需要清除,(在台账删除时就应该清除) - apr_snprintf(str, sizeof(str), "PQMonitorPQM%d", cpuno); - // 仅在没有值时更新 LD_name,避免重复分配内存 - if (ied_usr->LD_info[cpuno - 1].LD_name == NULL) { + //调试 + printf("check error111 !!!!!!!!!!!!!!\n"); - //lnk20250208调试用 - std::cout << "new space for LD_name" << std::endl; - - //lnk20250208不使用apr_pstrdup,使用固定大小,后续都在这块内存上直接复用 - //ied_usr->LD_info[cpuno - 1].LD_name = apr_pstrdup(g_init_pool, str); - // 从 g_init_pool 内存池中分配固定 256 字节的内存 - ied_usr->LD_info[cpuno].LD_name = (char *)apr_palloc(g_init_pool, 256); - // 将 str 中的内容复制到预先分配的内存中,最多复制 256 字节(包含结束符) - apr_cpystrn(ied_usr->LD_info[cpuno].LD_name, str, 256); - - } else {//已有则替换,在原有空间上覆盖 - - //lnk20250208调试用 - std::cout << "old space for LD_name" << std::endl; - - apr_cpystrn(ied_usr->LD_info[cpuno - 1].LD_name, str, sizeof(str)); - } - ldname = ied_usr->LD_info[cpuno - 1].LD_name; + ///////// //清空已有的补招数组占用空间 if (ied_usr->LD_info[cpuno - 1].autorecallcount != 0) { @@ -14310,10 +14300,15 @@ int update_one_terminal_ledger(terminal* update, int i,ied_t* ied,int terminal_i ied_usr->LD_info[cpuno - 1].autorecallcount = 0; } + //调试 + printf("check error112 !!!!!!!!!!!!!!\n"); + //////// //记录原有的报告和日志数组 loginfo = ied_usr->LD_info[cpuno - 1].loginfo?ied_usr->LD_info[cpuno - 1].loginfo:NULL; rptinfo = ied_usr->LD_info[cpuno - 1].rptinfo?ied_usr->LD_info[cpuno - 1].rptinfo:NULL; + //调试 + printf("check error113 !!!!!!!!!!!!!!\n"); //避免重复分配内存,如果使用已存在的ied,那么它之前初始化就分配了哈希表,如果在删除台账时没有清除,则需要将原来的清除 if (ied_usr->LD_info[cpuno - 1].ht_fcd != NULL) { @@ -14323,10 +14318,58 @@ int update_one_terminal_ledger(terminal* update, int i,ied_t* ied,int terminal_i apr_hash_clear(ied_usr->LD_info[cpuno - 1].ht_full_fcda); } + //调试 + printf("check error116 !!!!!!!!!!!!!!\n"); + + //这些可能是已有的内存,如果存在则需要清除,(在台账删除时就应该清除) + apr_snprintf(str, sizeof(str), "PQMonitorPQM%d", cpuno); + + // 仅在没有值时更新 LD_name,避免重复分配内存 + if (!ied_take) { + + //lnk20250208调试用 + std::cout << "new space for LD_name" << std::endl; + + //lnk20250208不使用apr_pstrdup,使用固定大小,后续都在这块内存上直接复用 + //ied_usr->LD_info[cpuno - 1].LD_name = apr_pstrdup(g_init_pool, str); + // 从 g_init_pool 内存池中分配固定 256 字节的内存 + ied_usr->LD_info[cpuno - 1].LD_name = (char *)apr_palloc(g_init_pool, 256); + + // 清空内存,防止残留数据 + memset(ied_usr->LD_info[cpuno - 1].LD_name, 0, 256); + + // 将 str 中的内容复制到预先分配的内存中,最多复制 256 字节(包含结束符) + apr_cpystrn(ied_usr->LD_info[cpuno - 1].LD_name, str, 256); + + } else {//已有则替换,在原有空间上覆盖 + + //lnk20250208调试用 + std::cout << "old space for LD_name:" << ied_usr->LD_info[cpuno].LD_name <LD_info[cpuno - 1].LD_name, 0, 256); + + apr_cpystrn(ied_usr->LD_info[cpuno - 1].LD_name, str, 256); //原有空间覆盖 + } + + ldname = ied_usr->LD_info[cpuno - 1].LD_name; + + //调试 + printf("check error114 !!!!!!!!!!!!!!\n"); + ied_usr->LD_info[cpuno - 1] = line_info;//这个放在后面,因为需要先判断原有的指针 ied_usr->LD_info[cpuno - 1].ied = ied; //这个放在后面,因为需要先判断原有的指针 + + //调试 + printf("check error115 !!!!!!!!!!!!!!\n"); + + + ied_usr->LD_info[cpuno - 1].LD_name = ldname;//记录原有的或者新的ldname + //调试 + printf("ledger ied_usr->LD_info[cpuno - 1].LD_name: %s\n", ied_usr->LD_info[cpuno - 1].LD_name); + ied_usr->LD_info[cpuno - 1].ht_fcd = apr_hash_make(g_init_pool); //重新创建哈希表 ied_usr->LD_info[cpuno - 1].ht_full_fcda = apr_hash_make(g_init_pool); @@ -14334,7 +14377,7 @@ int update_one_terminal_ledger(terminal* update, int i,ied_t* ied,int terminal_i printf("rptcount: %d\n", ied_usr->LD_info[cpuno - 1].rptcount); //使用原有的报告日志空间 - ied_usr->LD_info[cpuno - 1].loginfo = loginfo; + ied_usr->LD_info[cpuno - 1].loginfo = loginfo; //不管是新的还是旧的空间,后续初始化报告都会将它覆盖 ied_usr->LD_info[cpuno - 1].rptinfo = rptinfo; if (cpuno > ied->cpucount) {//新台账的逻辑号大于ied原有的cpu数(初始化是0),则更新cpu数,用来记录有多少个监测点 @@ -14510,6 +14553,8 @@ int parse_rpt_log_ini_one(ied_t* ied) apr_snprintf(str, sizeof(str), tmp, cpuno + 1); //ied_usr->LD_info[cpuno].LD_name = apr_pstrdup(g_init_pool, str);//lnk20250122 +//lnk20250212重复申请删除 +#if 0 // 仅在没有值时更新 LD_name,避免重复分配内存 if (ied_usr->LD_info[cpuno].LD_name == NULL) { @@ -14521,6 +14566,10 @@ int parse_rpt_log_ini_one(ied_t* ied) //ied_usr->LD_info[cpuno].LD_name = apr_pstrdup(g_init_pool, str); // 从 g_init_pool 内存池中分配固定 256 字节的内存 ied_usr->LD_info[cpuno].LD_name = (char *)apr_palloc(g_init_pool, 256); + + // 清空内存,防止残留数据 + memset(ied_usr->LD_info[cpuno].LD_name, 0, 256); + // 将 str 中的内容复制到预先分配的内存中,最多复制 256 字节(包含结束符) apr_cpystrn(ied_usr->LD_info[cpuno].LD_name, str, 256); @@ -14529,9 +14578,15 @@ int parse_rpt_log_ini_one(ied_t* ied) // 调试用 std::cout << "strlen(str):" << strlen(str) << "strlen(ied_usr->LD_info[cpuno].LD_name)" << strlen(ied_usr->LD_info[cpuno].LD_name) <LD_info[cpuno].LD_name, str, sizeof(str));//apr_palloc 只是分配了一个固定大小的内存块,而没有提供直接的机制来跟踪或验证它的空间是否已被使用或填充到最大值,直接覆盖即可 + // 清空内存,防止残留数据 + memset(ied_usr->LD_info[cpuno].LD_name, 0, 256); + + apr_cpystrn(ied_usr->LD_info[cpuno].LD_name, str, 256);//apr_palloc 只是分配了一个固定大小的内存块,而没有提供直接的机制来跟踪或验证它的空间是否已被使用或填充到最大值,直接覆盖即可 } +#endif + //调试 + printf("logini ied_usr->LD_info[cpuno - 1].LD_name: %s\n", ied_usr->LD_info[cpuno - 1].LD_name); delete[] tmp;//Get_IED中分配了内存,使用后删除