Memory leak detection method, device, computer equipment and storage medium

By generating control flow diagrams and hash table operations, the memory operation of the code to be detected in the smart grid is simulated, and the problem of high memory leakage detection cost in the smart grid is solved, and the method of detecting memory leakage without running code is realized, reducing operation Cost and improve detection efficiency.

CN114238086BActive Publication Date: 2024-11-19GUANGZHOU POWER SUPPLY BUREAU GUANGDONG POWER GRID CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202111455500.0
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-12-01
Publication Date
2024-11-19
Estimated Expiration
2041-12-01

AI Technical Summary

Technical Problem

When detecting memory leaks in smart grids, the existing technology requires running the code to be detected, which consumes a large amount of operating costs and cannot effectively reduce costs.

Method used

By generating a control flow diagram corresponding to the code to be detected, simulating the memory operation of the node path based on the correspondence between the code instructions and the hash table operation, and updating the hash table status, determining whether there is a memory leak, avoiding running the code to be detected The process.

Benefits of technology

Without running the code to be detected, it can detect whether there is a memory leak, which saves running costs, and accurately determines the code instruction location of the memory leak, improving detection efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114238086B_ABST
    Figure CN114238086B_ABST
Patent Text Reader

Abstract

The present application relates to a memory leak detection method, device, computer equipment, storage medium and computer program product. The method includes: obtaining a code to be detected corresponding to a memory operation; generating a control flow graph corresponding to the code to be detected, and determining a node path according to the control flow graph; executing corresponding hash table operations on the code instructions of each node in the node path in turn according to the correspondence between the code instructions and the hash table operations, and updating the hash table state; determining whether there is a memory leak in the code to be detected according to the hash table state corresponding to the executed node path. The method can detect whether there is a memory leak in the code to be detected without running the code to be detected, thereby saving operating costs.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of smart grid technology, and in particular to a memory leak detection method, apparatus, computer equipment, storage medium and computer program product. Background Art

[0002] In recent years, with the rapid development of smart grid technology, in order to ensure the reliable and stable operation of smart grid, it is necessary to ensure the accuracy and reliability of code instructions in smart grid. One of the exceptions of code instructions is the memory leak in code instructions. Memory leak refers to the dynamic application of memory by code instructions, and the untimely release, no release or wrong release after the memory is used up, resulting in this part of memory not being recycled by the system, that is, memory leak. Based on the characteristics of large user access and strong continuity of smart grid system, with the operation of software in smart grid and continuous user access, memory leaks continue to accumulate, and the consumed system resources cannot be reused, resulting in the continuous increase of program memory and insufficient system memory, thus affecting the normal operation of smart grid: at the least, it will cause delays and abnormalities when users access smart grid, and at the worst, it will cause the system crash of smart grid. Memory leak is not an explicit error, that is, programmers cannot directly check and find the problem of memory leak through code form. For the problem of memory leak in smart grid, the current technical solution is generally to obtain the code to be detected corresponding to the memory operation from the data stream of smart grid, run the code to be detected, and judge whether there is a memory leak in the code to be detected according to the running result of the code to be detected. However, in the current technical solution, different codes to be detected need to be run for detection, which consumes a lot of running costs.

[0003] Therefore, how to reduce the consumption of operating costs during the memory leak detection process is a technical problem that those skilled in the art currently need to solve. Summary of the invention

[0004] Based on this, it is necessary to provide a memory leak detection method, apparatus, computer equipment, computer-readable storage medium and computer program product that can reduce operating cost consumption in response to the above technical problems.

[0005] In a first aspect, the present application provides a memory leak detection method, the method comprising:

[0006] Get the code to be detected corresponding to the memory operation;

[0007] Generate a control flow graph corresponding to the code to be detected, and determine a node path according to the control flow graph;

[0008] According to the correspondence between the code instructions and the hash table operations, the corresponding hash table operations are sequentially executed on the code instructions of each node in the node path, and the hash table state is updated;

[0009] Determine whether there is a memory leak in the code to be detected according to the hash table state corresponding to the node path that has been executed.

[0010] In one embodiment, according to the correspondence between the code instruction and the hash table operation, the corresponding hash table operation is sequentially performed on the code instruction of each node in the node path, and the hash table state is updated, including:

[0011] Input each of the node paths in the control flow graph into an intermediate model;

[0012] Utilizing the intermediate model to parse each of the nodes, and transferring each of the nodes to the intermediate model in the form of the control flow graph;

[0013] Each of the nodes in the node path is called in sequence through the intermediate model, and according to the correspondence between the code instructions and the hash table operations, the corresponding hash table operations are performed on the code instructions of each of the nodes, and the hash table state is updated.

[0014] In one embodiment, calling each of the nodes in the node path in sequence through the intermediate model, performing corresponding hash table operations on the code instructions of each of the nodes according to the correspondence between the code instructions and the hash table operations, and updating the hash table state includes:

[0015] Calling each of the nodes in the node path in sequence through the intermediate model;

[0016] Determining the hash table operation corresponding to the code instruction of the node according to the correspondence between the code instruction and the hash table operation;

[0017] The hash table state is updated according to the hash table operation and the mapping relationship between the pointer and the memory block.

[0018] In one embodiment, generating a control flow graph corresponding to the code to be detected, and determining a node path according to the control flow graph, includes:

[0019] Determine nodes and control flow lines according to the process relationship in the code to be detected, and determine the control flow graph according to the nodes and the control flow lines;

[0020] The node path is determined by using the nodes and control flow lines between the start node and the end node in the control flow graph.

[0021] In one embodiment, the information in each of the nodes in the control flow graph includes the node number, the line number of the code instruction corresponding to the node, the content of the code instruction corresponding to the node, and the node number of the successor node of the node.

[0022] In one embodiment, the method further comprises:

[0023] Determine an abnormal operation statement according to the abnormal data of the hash table state and the hash table operation;

[0024] The code instruction position where the memory leak occurs in the code to be detected is determined according to the abnormal operation statement.

[0025] In a second aspect, the present application further provides a memory leak detection device, the device comprising:

[0026] An acquisition module, used for acquiring the code to be detected corresponding to the memory operation;

[0027] A determination module, used to generate a control flow graph corresponding to the code to be detected, and determine a node path according to the control flow graph;

[0028] An operation module, configured to perform corresponding hash table operations on the code instructions of each node in the node path in sequence according to the correspondence between the code instructions and the hash table operations, and update the hash table state;

[0029] The detection module is used to determine whether there is a memory leak in the code to be detected according to the hash table state corresponding to the node path that has been executed.

[0030] In a third aspect, the present application further provides a computer device. The computer device includes a memory and a processor, the memory stores a computer program, and the processor implements the following steps when executing the computer program:

[0031] In a fourth aspect, the present application further provides a computer-readable storage medium. The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the following steps are implemented:

[0032] Get the code to be detected corresponding to the memory operation;

[0033] Generate a control flow graph corresponding to the code to be detected, and determine a node path according to the control flow graph;

[0034] According to the correspondence between the code instructions and the hash table operations, the corresponding hash table operations are sequentially executed on the code instructions of each node in the node path, and the hash table state is updated;

[0035] Determine whether there is a memory leak in the code to be detected according to the hash table state corresponding to the node path that has been executed.

[0036] In a fifth aspect, the present application further provides a computer program product. The computer program product includes a computer program, and when the computer program is executed by a processor, the following steps are implemented:

[0037] Get the code to be detected corresponding to the memory operation;

[0038] Generate a control flow graph corresponding to the code to be detected, and determine a node path according to the control flow graph;

[0039] According to the correspondence between the code instructions and the hash table operations, the corresponding hash table operations are sequentially executed on the code instructions of each node in the node path, and the hash table state is updated;

[0040] Determine whether there is a memory leak in the code to be detected according to the hash table state corresponding to the node path that has been executed.

[0041] The above-mentioned memory leak detection method, device, computer equipment, storage medium and computer program product, the method simulates the memory operation of the node path in the control flow graph corresponding to the code to be detected through hash table operation based on the correspondence between the preset code instructions and the hash table operation, and updates the hash table state, and determines whether there is a memory leak in the code to be detected according to the hash table state corresponding to the executed node path; therefore, the method can detect whether there is a memory leak in the code to be detected without running the code to be detected, thereby saving operating costs. BRIEF DESCRIPTION OF THE DRAWINGS

[0042] Figure 1 A schematic diagram of a memory leak detection method according to an embodiment of the present invention;

[0043] Figure 2a to Figure 2c A schematic diagram of a flow chart of a memory leak detection method in another embodiment;

[0044] Figure 3 A schematic diagram of a flow chart of a memory leak detection method in another embodiment;

[0045] Figure 4 is a structural block diagram of a memory leak detection device in one embodiment;

[0046] Figure 5 FIG. 4 is a diagram showing the internal structure of a computer device in one embodiment. DETAILED DESCRIPTION

[0047] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.

[0048] In one embodiment, Figure 1 As shown, a memory leak detection method is provided. This embodiment uses the method applied to a terminal as an example for illustration. It can be understood that the method can also be applied to a server, and can also be applied to a system including a terminal and a server, and is implemented through the interaction between the terminal and the server. In this embodiment, the method includes the following steps:

[0049] Step 102, obtaining the code to be detected corresponding to the memory operation.

[0050] Specifically, in actual operation, the code to be detected is generally obtained from the data stream of the system to be detected, and a memory leak detection operation is performed on the code to be detected to determine whether the system to be detected has a risk of memory leak. This embodiment does not limit the specific type of the system to be detected. For example, it can be a smart grid, or other systems that perform memory control through code instructions. Among them, data flow refers to a group of ordered data sequences with a starting point and an end point. In the field of programming, it refers to the data that flows during the running of the program. Data flow analysis refers to the process of analyzing the data information that flows during the execution of the program to deduce the actual running process of the program. Data flow analysis is usually used to statically analyze the source code when the program is not running, in order to predict the process when the program is dynamically running.

[0051] The focus of this embodiment is to detect memory leaks, so it is necessary to obtain the code to be detected corresponding to the memory operation from the data stream. In actual operation, before the code to be detected is analyzed, the code to be detected is generally further preprocessed. Preprocessing is the basis for static analysis of the code to be detected. By preprocessing the code to be detected, the information required for this analysis is extracted and stored in a suitable data structure, which can reduce the difficulty of subsequent analysis while also greatly improving the efficiency of the analysis. It should also be noted that this embodiment does not limit the specific type of code instructions, for example, it can be a code instruction of the C++ programming language.

[0052] Step 104: Generate a control flow graph corresponding to the code to be detected, and determine the node path according to the control flow graph.

[0053] Specifically, the control flow graph (CFG) is also called a control flow graph. The control flow graph is one of the important data structures for program analysis and source code structure analysis. It is an abstract representation of a process or program. It is an abstract data structure used in the compiler and maintained internally by the compiler. It represents all the paths that will be traversed during the execution of a program. It uses a graph to represent the possible flow of execution of all basic blocks in a process, and can also reflect the real-time execution process of a process; that is, the data flow flows along the execution path of the program in the control flow graph.

[0054] In actual operation, after obtaining the code to be detected, the code to be detected is converted into a corresponding control flow graph. It is understandable that since there may be selection structures, loop structures, and multi-branch structures in the code to be detected, the control flow graph may include multiple paths, that is, there are multiple different paths from the starting node to the end node, so it is necessary to determine each different node path.

[0055] Step 106, according to the correspondence between the code instruction and the hash table operation, execute the corresponding hash table operation on the code instruction of each node in the node path in turn, and update the hash table state;

[0056] Step 108: Determine whether there is a memory leak in the code to be detected according to the hash table state corresponding to the node path that has been executed.

[0057] Specifically, the correspondence between code instructions and hash table operations is pre-set. When it is necessary to execute memory operations corresponding to the node path, for each node path, the nodes in the node path are obtained in turn, and then based on the correspondence between code instructions and hash table operations, the operations of the code instructions in the node on the memory are simulated through hash table operations, such as allocating and releasing heap memory, etc. The hash table state is updated according to the hash table operation, so the hash table state simulation corresponds to the memory state.

[0058] It is understandable that after the memory operations of all nodes corresponding to a node path are completed, the hash table state at this time indicates the memory state when the memory operations corresponding to this node path are completed; therefore, by detecting the hash table state, it is possible to determine whether there is a memory leak in the code to be detected. Specifically, if there is memory without a pointer in the final hash table state, it means that the memory has not been released after use, so it can be determined that there is a memory leak problem in the code to be detected.

[0059] In the above-mentioned memory leak detection method, based on the correspondence between preset code instructions and hash table operations, the memory operations of the node paths in the control flow graph corresponding to the code to be detected are simulated through hash table operations, and the hash table state is updated. According to the hash table state corresponding to the executed node path, it is determined whether there is a memory leak in the code to be detected; therefore, this method can detect whether there is a memory leak in the code to be detected without running the code to be detected, thereby saving operating costs.

[0060] Based on the above embodiment, this embodiment further illustrates and optimizes the technical solution. Specifically, in this embodiment, according to the correspondence between the code instruction and the hash table operation, the corresponding hash table operation is performed on the code instruction of each node in the node path in turn, and the hash table state is updated, including:

[0061] Input each node path in the control flow graph into the intermediate model;

[0062] Use the intermediate model to parse each node, and transfer each node to the intermediate model in the form of a control flow graph;

[0063] Each node in the node path is called in sequence through the intermediate model. According to the correspondence between the code instruction and the hash table operation, the corresponding hash table operation is performed on the code instruction of each node, and the hash table state is updated.

[0064] It should be noted that after the corresponding control flow graph is generated according to the code to be detected, the information of the control flow graph cannot be used directly. This embodiment stores the control flow graph through an intermediate model to facilitate the subsequent processing of the control flow graph. Specifically, in this embodiment, after the node path is determined according to the control flow graph, the control flow graph is input into the intermediate model in the form of a node path; then, each node is parsed using the intermediate model, and each node is transferred to the intermediate model in the form of a control flow graph.

[0065] As a preferred implementation, this embodiment chooses to use XML (eXtensible Markup Language) parsing model as the intermediate model, and uses the TinyXML open source library in the XML parsing model to parse each node in the node path. The main reason is that the basis of the TinyXML open source library is DOM (Document Object Model). This tree structure model is easy to understand and provides users with an object-oriented interface for access. As a DOM-based parsing tool, the principle of the TinyXML open source library is: load the XML file once, use the TiXmlDocument class to load the XML file to be parsed into the memory for subsequent operations; obtain the node, parse the node content, start from the parsing declaration, store each node as an object in the tree structure, and maintain the relationship between the nodes; through the provided object-oriented interface, users can easily access and edit the data in the XML file by calling the interface. By storing each node path in the intermediate model, each node in the node path can be called in turn through the intermediate model, and then according to the corresponding relationship between the code instruction and the hash table operation, the corresponding hash table operation is performed on the code instruction of each node, and the hash table state is updated.

[0066] It can be seen that the intermediate model can not only fully store the information of each node, but also reflect the relationship between nodes, that is, it can store the nodes in the form of node paths. At the same time, it can conveniently perform corresponding operations on the nodes through interface calls, which is convenient for parsing and reading, and can enhance the scalability and adaptability of the memory leak detection method.

[0067] Based on the above embodiment, this embodiment further illustrates and optimizes the technical solution. Specifically, in this embodiment, a control flow graph corresponding to the code to be detected is generated, and a node path is determined according to the control flow graph, including:

[0068] Determine nodes and control flow lines according to the process relationship in the code to be detected, and determine a control flow graph according to the nodes and control flow lines;

[0069] The node path is determined by using the nodes and control flow lines between the start node and the end node in the control flow graph.

[0070] Specifically, after obtaining the code to be detected, the code to be detected is first semantically identified, each word in the code to be detected is identified, and invalid characters such as blanks, comments, incomplete brackets, etc. are deleted, and when a grammatical error is detected, the grammatical error is corrected. In actual operation, this process can be achieved by generating an abstract syntax tree corresponding to the code to be detected. The abstract syntax tree (AST) is a tree representation of the abstract syntax structure of the source code, and each node on the tree represents a grammatical structure in the source code; the node can be an operator or a custom variable, but the node representing the grammatical structure is abstract, that is, it is not completely the same as the source code, it will hide some details, such as the brackets of the nested structure, which will not appear in the abstract syntax tree, that is, the abstract syntax tree has "abstractness". As an interface between syntax analysis and the back end, the abstract syntax tree converts the code to be detected into an abstract syntax tree, determines invalid characters such as blanks, comments, incomplete brackets, etc. in the code to be detected, and detects grammatical errors in the code to be detected, and obtains the updated code to be detected by deleting invalid characters and correcting grammatical errors. The updated code to be detected is then continued to execute operations, which can further improve the accuracy of detection.

[0071] In actual operation, after obtaining the updated code to be tested, the nodes and control flow lines can be determined according to the process relationship in the code to be tested, and the control flow graph can be determined according to the nodes and control flow lines. Among them, the nodes and control flow lines are determined according to the structure of the code instructions in the code to be tested, and the structure includes sequential structure, if branch structure such as if, if...else..., if...else if...else..., etc.; loop structure such as for loop, while loop, do...while loop, until loop, etc., and Case multi-branch structure; among them, the node is represented by a circle with a number, which represents the basic program block, which can be a single statement or multiple sequentially executed statement blocks; the control flow line is represented by an arc with an arrow, connecting two related nodes, and each control flow line can be labeled with a name (or not), but must end at a certain node.

[0072] In actual operation, the corresponding program flow chart can be determined according to the process relationship of the code to be detected, and then the control flow chart can be determined according to the program flow chart. Figure 2a FIG. 1 is a schematic diagram of a code to be detected provided by an embodiment of the present invention. Figure 2b Based on Figure 2a The program flow chart determined by the code to be detected is simplified to obtain a control flow chart that highlights the control flow structure, such as Figure 2c More specifically, combined with Figure 2b and Figure 2cThe difference between program flow chart and control flow graph can be more clearly distinguished: program flow chart uses unified symbols to describe the program execution process, usually composed of start box, end box, processing box, judgment box and flow line, reflecting the specific steps of program operation, focusing on the logic of the program, and clear structure. Control flow graph usually represents the directed graph of the program's logical control flow, consisting of a set of nodes and a set of directed edges, denoted as CFG(V, E); where V is the set of nodes of the control flow graph, V = {V s , V1, V 2. .....V n}, which indicates a simple statement, a judgment statement, or a control flow intersection. s and V n are the starting node and the ending node of the control flow graph respectively; E is the set of directed edges of the control flow graph, E={E1,E2......E n}, which actually represents the relationship between nodes, E = <V m ,V m+1 >, V m , is the predecessor node, V m+1 It is the successor node. Compared with the program flow chart, the control flow graph is more concise and only requires nodes and control flow lines to represent it.

[0073] It is understandable that there may be multiple ways of connecting nodes according to the various types of structures of code instructions, so the node path is determined by using the nodes and control flow lines between the start node and the end node in the control flow graph. In other words, the control flow graph records all possible execution paths of the program, and the execution path when the program is actually running will be different depending on the assignment. For example, Figure 2c There are three node paths, the first node path is "5-8-12-14-15-16-21-5", the second node path is "5-8-12-14-15-18-21-5", and the third node path is "5-8-24".

[0074] As a preferred implementation, the information in each node in the control flow graph includes the node number, the line number of the code instruction corresponding to the node, the content of the code instruction corresponding to the node, and the node number of the successor node of the node.

[0075] Specifically, in this embodiment, each node of the control flow graph is defined as a formalized four-tuple<id,line,cont,nextNode> ; Among them, id represents the node number of the node in the control flow graph, that is, the node number is used to uniquely identify each node; line represents the line number of the code instruction of the memory operation corresponding to the node in the entire code instruction, which is used to accurately report the location of the memory leak in the code to be detected; cont(content) represents the content of the code instruction corresponding to the node, the instruction code related to the memory operation corresponding to the node, ignoring the statement information that is not related to the memory leak, that is, not related to the memory operation; nextNode represents the node number (id) of the successor node corresponding to the node, which is used to represent the connection of the nodes in the control flow graph and facilitate the subsequent storage of the control flow graph in the intermediate model.

[0076] This embodiment sets the information of each node according to a unified format specification to facilitate the management of each node and to store the nodes in an intermediate model, thereby improving the detection efficiency and reducing the difficulty of implementing the memory leak detection method.

[0077] Correspondingly, when storing the control flow graph in the intermediate model, the label is set as the node number (id) in the intermediate model, and line, cont and nextNode are the three attributes under each label. Storing the control flow graph in the intermediate model in this way can not only completely save the information of each node, but also maintain the structure of the control flow graph for easy reading.

[0078] Based on the above embodiment, this embodiment further illustrates and optimizes the technical solution. Specifically, in this embodiment, each node in the node path is called in sequence through the intermediate model, and the corresponding hash table operation is performed on the code instruction of each node according to the corresponding relationship between the code instruction and the hash table operation, and the hash table state is updated, including:

[0079] Call each node in the node path in sequence through the intermediate model;

[0080] According to the correspondence between the code instruction and the hash table operation, determine the hash table operation corresponding to the code instruction of the node;

[0081] Update the hash table status based on the hash table operation and the mapping relationship between pointers and memory blocks.

[0082] Specifically, the hash table stores key-value pairs.<key,value> , the key-value pair defines a mapping relationship, namely the Hash function. The Hash function can be used to quickly find the stored record according to the keyword key, that is, the corresponding value can be found only according to the keyword and the Hash function. This embodiment is illustrated by the code instructions of the C++ programming language. As an efficient and flexible programming language, the C++ programming language is closer to the bottom layer of the operating system and can be easily deployed to the operating system, graphics processing, search engines and databases, with good compatibility; and the C++ programming language retains the characteristics of pointers and dynamic memory allocation. The memory location can be directly found through the pointer, so as to operate the data at the location without addressing through the variable name, which greatly saves time overhead; it allows users to directly control system resources through code instructions, and dynamically allocate memory through pointers, so as to achieve application and use, such as allocating and releasing memory; for example, memory can be dynamically allocated through the new statement, and after the allocated memory is used up, the memory can be released through the delete statement.

[0083] In the code instructions, memory operations are performed through pointers. The pointer variable is defined as pointer. The address stored in the pointer variable points to the memory block M. In the C++ programming language, multiple pointers are allowed to store the same memory address, that is, multiple pointers point to the same memory block. The set of pointers pointing to the same memory block Mi is defined as P. That is, there are two mapping relationships between pointers and memory blocks:

[0084] The mapping between a pointer and the memory block it points to: the pointer pointer and the memory block M are many-to-one mappings;

[0085] Mapping between a set of pointers pointing to the same memory block and a memory block: Pointers belonging to the same pointer set point to the same memory block, so the mapping between the pointer set and the memory block is a one-to-one mapping.

[0086] In actual operation, in order to use hash table operations to simulate the memory operations of code instructions in the code to be detected, such as memory allocation, memory release, etc., according to the storage characteristics of the hash table and the operation characteristics of the pointer, as well as the mapping relationship between the key-value pairs, the corresponding relationship between the code instructions and the hash table operations is determined, that is, the hash table is used to simulate the memory operations of the code instructions in the code to be detected, such as memory allocation, memory release, etc.<key,value> Represents data unit<pointer,M> As shown in Table 1, it is a correspondence table of memory operations, code instructions and Hash functions provided by an embodiment of the present invention;

[0087] Table 1 Correspondence between memory operations, code instructions and hash functions

[0088] Memory Operations C++ code instructions Hash Functions Allocating Memory int*P=newint[5] Add(pointer,M) Freeing up memory delete[]P Delete(pointer,M) Query Memory inta=P[1] Get(pointer) Modify pointer int*Q=P Modify(pointer,M)

[0089] It should be noted that common memory operations in the C++ programming language include Add, Delete, Get, and Modify, which can also correspond to operations in hash tables. Hash table operations are used to simulate memory operations in C++ programs. There are four operation definitions for hash tables:

[0090] 1. Add: Add(pointer,M) is a pointer pointer to allocate a dynamic memory M. The corresponding hash table operation is to add the key-value pair<pointer,M> Add to the hash table;

[0091] 2. Delete: Delete(pointer, M) is to release the memory block M allocated by pointer pointer in the dynamic memory area. The corresponding hash table operation is to find the key-value pair<pointer,M> , and delete the key-value pair;

[0092] 3. Get: Get(pointer) returns the first address of the memory block pointed to by pointer in the C++ program. The corresponding hash table operation is to find M with the key value of pointer in the hash table;

[0093] 4. Modify: Modify(pointer, Mi) is to modify the address pointed to by the pointer pointer from the original M to Mi. The corresponding hash table operation is to find the key-value pair with the key value of pointer and change its value to Mi.

[0094] After storing the node path in the intermediate model, each node in the node path is called in sequence through the intermediate model; for each called node, the hash table operation corresponding to the code instruction of the node is determined based on the correspondence between the code instruction and the hash table operation; the hash table state is updated according to the hash table operation and the mapping relationship between the pointer and the memory block.

[0095] For example, suppose a node path includes four nodes, and each node is represented as follows:

[0096] Node1:<1,37,“p=malloc(sizeof(int));”,2>

[0097] Node2:<2,38,“q=malloc(sizeof(int));”,3>

[0098] Node3:<3,39,“p=q;”,4>

[0099] Node4:<4,40,“free(p);”,null>

[0100] According to the correspondence between code instructions and hash table operations, determine the hash table operation corresponding to the code instruction of the node; for example, the hash table operation corresponding to the code instruction "p = malloc (sizeof (int));" is "Add (p, M1)", and the hash table operation corresponding to the code instruction "p = q;" is "Modify (p, M2)"; then update the hash table state according to the hash table operation and the mapping relationship between the pointer and the memory block. The processing process of the hash table operation is shown in Table 2:

[0101] Table 2 Processing of hash table operations

[0102] Instruction code Hash table operations Hash table status p = malloc(sizeof(int)); Add(p,M1) (<p,M1>) q = malloc(sizeof(int)); Add(q,M2) (<p,M1>,<q,M2>) p = q; Modify(p,M2) (<NULL,M1>,<p,M2>,<q,M2>) free(p) Delete(p,M2) (<NULL,M1>)

[0103] After all code instructions of the nodes corresponding to the node path are executed, the hash table state is determined, and whether there is a memory leak risk in the code to be detected corresponding to the node path is determined according to the processed hash table state.

[0104] Specifically, after the hash table is executed according to the instruction code, if the hash table state has the following<null,M1> If the mapping relationship between the pointer and the memory block is not found, it indicates that there is a risk of memory leak in the code to be detected.

[0105] It can be seen that this embodiment updates the hash table state according to the hash table operation and the mapping relationship between the pointer and the memory block. Therefore, it is possible to determine whether there is a memory leak in the code to be detected according to the hash table state. The detection process is convenient and the detection result is intuitive. In addition, this embodiment can directly find the memory location through the pointer, so as to operate the data at the location without addressing through the variable name, which greatly saves time overhead.

[0106] On the basis of the above embodiments, this embodiment further illustrates and optimizes the technical solution. Specifically, in this embodiment, the method further includes:

[0107] Determine an abnormal operation statement according to abnormal data of the hash table state and the hash table operation;

[0108] The code instruction location where the memory leak exists in the code to be detected is determined based on the abnormal operation statement.

[0109] In this embodiment, after determining that there is a memory leak risk in the code to be detected according to the hash table state, abnormal data in the hash table state is obtained, as shown in Table 2 above, according to the abnormal data in the hash table state "(<NULL,M1> )" to determine the risk of memory leak in the code to be detected, and based on the abnormal data "(<NULL,M1> )” to determine the corresponding instruction code, and it can be determined that the node corresponding to the instruction code is “Node4”, and then according to the information “the line number of the code instruction corresponding to the node” contained in the node “Node4”, the line number of the code instruction with the memory leak is determined.

[0110] It should be noted that in other embodiments, after determining that there is a memory leak risk in the code to be detected according to the hash table state, the code to be detected can be detected using third-party software to determine the line number of the code instruction where the memory leak exists in the code to be detected. The third-party software can be an open source tool such as cppcheck, and this embodiment does not limit the specific type of the third-party software.

[0111] It can be seen that, based on the ability to detect the risk of memory leakage, this embodiment can further determine the line number of the code where the risk of memory leakage is located in the code to be detected, that is, determine the code instruction position where the memory leak exists. Therefore, it can further facilitate the user to modify the code to be detected according to the determined code instruction position where the memory leak exists.

[0112] In order to enable those skilled in the art to better understand the technical solutions in this application, the technical solutions in the embodiments of this application are described in detail below in combination with actual application scenarios. Figure 3 As shown, it is a flow chart of a memory leak detection method provided by this embodiment; this application embodiment takes memory leak detection of data to be detected in a smart grid as an example, and the specific steps are as follows:

[0113] Step 302: Obtain the code to be detected corresponding to the memory operation from the data stream of the smart grid;

[0114] Among them, smart grid is the intelligence of power grid, also known as "Grid 2.0", which is built on the basis of integrated, high-speed two-way communication network. Through the application of advanced sensing and measurement technology, advanced equipment technology, advanced control methods and advanced decision support system technology, it realizes the goals of reliable, safe, economical, efficient, environmentally friendly and safe use of power grid. Its main features include self-healing, incentive and protection of users, defense against attacks, provision of power quality that meets user needs, allowing access to various forms of power generation, launching power market and optimizing and efficient operation of assets.

[0115] Step 304: perform lexical and grammatical analysis on the code to be detected to obtain an abstract syntax tree, delete invalid characters in the code to be detected, correct grammatical errors, and update the code to be detected;

[0116] Step 306: determining nodes and control flow lines according to the process relationship in the updated code to be detected, and determining a control flow graph according to the nodes and control flow lines;

[0117] Step 308: Determine a node path using the nodes and control flow lines between the start node and the end node in the control flow graph;

[0118] Step 310: Input each node path in the control flow graph into the XML parsing model;

[0119] Step 312: parse each node using the TinyXML open source library in the XML parsing model, and transfer each node to the XML parsing model in the form of a control flow graph;

[0120] Step 314: calling each node in the node path in sequence through the intermediate model;

[0121] Step 316: Determine the hash table operation corresponding to the code instruction of the node according to the correspondence between the code instruction and the hash table operation;

[0122] Step 318: Update the hash table state according to the hash table operation and the mapping relationship between the pointer and the memory block;

[0123] Step 320: Determine whether there is a memory leak in the code to be detected according to the hash table state corresponding to the node path that has been executed;

[0124] Step 322: determining an abnormal operation statement according to the abnormal data of the hash table state and the hash table operation;

[0125] Step 324: Determine the code instruction location where the memory leak exists in the code to be detected according to the abnormal operation statement.

[0126] It should be noted that the specific explanation of each step in this embodiment can refer to the explanation of each step in the above embodiment, which will not be repeated here.

[0127] The memory leak detection method provided in the present embodiment simulates the memory operation of the node path in the control flow graph corresponding to the code to be detected through hash table operations according to the correspondence between preset code instructions and hash table operations, and updates the hash table state, and determines whether there is a memory leak in the code to be detected according to the hash table state corresponding to the executed node path; therefore, the present method can detect whether there is a memory leak in the code to be detected without running the code to be detected, thereby saving operating costs; and can further determine the line number of the code to be detected where there is a risk of memory leak, that is, determine the code instruction position where there is a memory leak, so it can further facilitate the user to modify the code to be detected according to the determined code instruction position where there is a memory leak.

[0128] It should be understood that, although the steps in the flowcharts involved in the above embodiments are displayed in sequence according to the indication of the arrows, these steps are not necessarily executed in sequence according to the order indicated by the arrows. Unless there is a clear explanation in this article, the execution of these steps is not strictly limited in order, and these steps can be executed in other orders. Moreover, at least a part of the steps in the flowcharts involved in the above embodiments may include multiple steps or multiple stages, and these steps or stages are not necessarily executed at the same time, but can be executed at different times, and the execution order of these steps or stages is not necessarily carried out in sequence, but can be executed in turn or alternately with other steps or at least a part of the steps or stages in other steps.

[0129] Based on the same inventive concept, the embodiment of the present application also provides a memory leak detection device for implementing the memory leak detection method involved above. The implementation scheme for solving the problem provided by the device is similar to the implementation scheme recorded in the above method, so the specific limitations in one or more memory leak detection device embodiments provided below can refer to the limitations of the memory leak detection method above, and will not be repeated here.

[0130] In one embodiment, Figure 4 As shown, a memory leak detection device is provided, including: an acquisition module 402, a determination module 404, an operation module 406 and a detection module 408, wherein:

[0131] An acquisition module 402 is used to acquire a code to be detected corresponding to a memory operation;

[0132] The determination module 404 is used to generate a control flow graph corresponding to the code to be detected, and determine a node path according to the control flow graph;

[0133] An operation module 406 is used to perform corresponding hash table operations on the code instructions of each node in the node path in turn according to the correspondence between the code instructions and the hash table operations, and update the hash table state;

[0134] The detection module 408 is used to determine whether there is a memory leak in the code to be detected according to the hash table state corresponding to the node path that has been executed.

[0135] A memory leak detection device provided by an embodiment of the present invention has the same beneficial effects as the above-mentioned memory leak detection method.

[0136] In one embodiment, the operation module includes:

[0137] An input submodule, used to input each node path in the control flow graph into the intermediate model;

[0138] The storage submodule is used to parse each node using the intermediate model and transfer each node to the intermediate model in the form of a control flow graph;

[0139] The update submodule is used to call each node in the node path in sequence through the intermediate model, execute the corresponding hash table operation on the code instruction of each node according to the correspondence between the code instruction and the hash table operation, and update the hash table state.

[0140] In one embodiment, the updating submodule includes:

[0141] A calling unit, used for calling each node in the node path in sequence through the intermediate model;

[0142] A determination unit, configured to determine the hash table operation corresponding to the code instruction of the node according to the correspondence between the code instruction and the hash table operation;

[0143] The update unit is used to update the hash table state according to the hash table operation and the mapping relationship between the pointer and the memory block.

[0144] In one embodiment, the determining module includes:

[0145] A first determination submodule is used to determine nodes and control flow lines according to the process relationship in the code to be detected, and determine a control flow graph according to the nodes and control flow lines;

[0146] The second determination submodule is used to determine the node path by using the nodes and control flow lines between the start node and the end node in the control flow graph.

[0147] In one of the embodiments, the memory leak detection device further includes:

[0148] An abnormal statement determination module is used to determine an abnormal operation statement according to abnormal data of the hash table state and the hash table operation;

[0149] The abnormal position determination module is used to determine the code instruction position where the memory leak exists in the code to be detected according to the abnormal operation statement.

[0150] Each module in the above memory leak detection device can be implemented in whole or in part by software, hardware or a combination thereof. Each module can be embedded in or independent of a processor in a computer device in the form of hardware, or can be stored in a memory in a computer device in the form of software, so that the processor can call and execute operations corresponding to each module.

[0151] In one embodiment, a computer device is provided. The computer device may be a terminal, and its internal structure diagram may be as follows: Figure 5 As shown. The computer device includes a processor, a memory, a communication interface, a display screen and an input device connected through a system bus. Among them, the processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The communication interface of the computer device is used to communicate with an external terminal in a wired or wireless manner, and the wireless manner can be achieved through WIFI, a mobile cellular network, NFC (near field communication) or other technologies. When the computer program is executed by the processor, a memory leak detection method is implemented. The display screen of the computer device can be a liquid crystal display screen or an electronic ink display screen, and the input device of the computer device can be a touch layer covered on the display screen, or a key, trackball or touchpad set on the computer device housing, or an external keyboard, touchpad or mouse, etc.

[0152] Those skilled in the art will understand that Figure 5 The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.

[0153] In one embodiment, a computer device is provided, including a memory and a processor, wherein a computer program is stored in the memory, and when the processor executes the computer program, the following steps are implemented:

[0154] Get the code to be detected corresponding to the memory operation;

[0155] Generate a control flow graph corresponding to the code to be detected, and determine the node path according to the control flow graph;

[0156] According to the correspondence between the code instructions and the hash table operations, the corresponding hash table operations are executed on the code instructions of each node in the node path in turn, and the hash table state is updated;

[0157] According to the hash table status corresponding to the node path that has been executed, determine whether there is a memory leak in the code to be detected.

[0158] A computer device provided by an embodiment of the present invention has the same beneficial effects as the above-mentioned memory leak detection method.

[0159] In one embodiment, a computer readable storage medium is provided, on which a computer program is stored, and when the computer program is executed by a processor, the following steps are implemented:

[0160] Get the code to be detected corresponding to the memory operation;

[0161] Generate a control flow graph corresponding to the code to be detected, and determine the node path according to the control flow graph;

[0162] According to the correspondence between the code instructions and the hash table operations, the corresponding hash table operations are executed on the code instructions of each node in the node path in turn, and the hash table state is updated;

[0163] According to the hash table status corresponding to the node path that has been executed, determine whether there is a memory leak in the code to be detected.

[0164] A computer-readable storage medium provided by an embodiment of the present invention has the same beneficial effects as the above-mentioned memory leak detection method.

[0165] In one embodiment, a computer program product is provided, comprising a computer program, which, when executed by a processor, implements the following steps:

[0166] Get the code to be detected corresponding to the memory operation;

[0167] Generate a control flow graph corresponding to the code to be detected, and determine the node path according to the control flow graph;

[0168] According to the correspondence between the code instructions and the hash table operations, the corresponding hash table operations are executed on the code instructions of each node in the node path in turn, and the hash table state is updated;

[0169] According to the hash table status corresponding to the node path that has been executed, determine whether there is a memory leak in the code to be detected.

[0170] A computer program product provided by an embodiment of the present invention has the same beneficial effects as the above-mentioned memory leak detection method.

[0171] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties.

[0172] Those of ordinary skill in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to the memory, database or other medium used in the embodiments provided in the present application can include at least one of non-volatile and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetic random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. As an illustration and not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM). The database involved in each embodiment provided in this application may include at least one of a relational database and a non-relational database. Non-relational databases may include distributed databases based on blockchains, etc., but are not limited thereto. The processors involved in each embodiment provided in this application may be general-purpose processors, central processing units, graphics processors, digital signal processors, programmable logic devices, data processing logic devices based on quantum computing, etc., but are not limited thereto.

[0173] The technical features of the above embodiments may be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.

[0174] The above-described embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be construed as limiting the scope of the present application. It should be noted that, for a person of ordinary skill in the art, several modifications and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the attached claims.

Claims

1. A memory leak detection method, characterized in that: The method comprises: Get the code to be detected corresponding to the memory operation; Determine nodes and control flow lines according to the process relationship in the code to be detected, and determine a control flow graph according to the nodes and the control flow lines, and determine a node path by using the nodes and control flow lines between the start node and the end node in the control flow graph, wherein the information in each node in the control flow graph includes the node number, the line number of the code instruction corresponding to the node, the content of the code instruction corresponding to the node, and the node number of the successor node of the node; Input each of the node paths in the control flow graph into an intermediate model, parse each of the nodes using the intermediate model, and transfer each of the nodes to the intermediate model in the form of the control flow graph, sequentially call each of the nodes in the node path through the intermediate model, determine the hash table operation corresponding to the code instruction of the node according to the correspondence between the code instruction and the hash table operation, and update the hash table state according to the hash table operation and the mapping relationship between the pointer and the memory block; An abnormal operation statement is determined according to the abnormal data of the hash table state and the hash table operation, and a code instruction position where a memory leak exists in the code to be detected is determined according to the abnormal operation statement.

2. The method according to claim 1, characterized in that The intermediate model includes an XML parsing model.

3. The method according to claim 2, characterized in that The using the intermediate model to parse each of the nodes comprises: The TinyXML open source library in the XML parsing model is used to parse each node in the node path.

4. A memory leak detection device, characterized in that: The device comprises: An acquisition module, used for acquiring the code to be detected corresponding to the memory operation; A determination module, used to generate a control flow graph corresponding to the code to be detected, and determine a node path according to the control flow graph; An operation module, configured to perform corresponding hash table operations on the code instructions of each node in the node path in sequence according to the correspondence between the code instructions and the hash table operations, and update the hash table state; A detection module, used to determine whether there is a memory leak in the code to be detected according to the hash table state corresponding to the node path that has been executed; An abnormal statement determination module is used to determine an abnormal operation statement according to abnormal data of the hash table state and the hash table operation; The abnormal position determination module is used to determine the code instruction position where the memory leak exists in the code to be detected according to the abnormal operation statement; The information in each of the nodes in the control flow graph includes the node number, the line number of the code instruction corresponding to the node, the content of the code instruction corresponding to the node, and the node number of the successor node of the node; The operation module includes: An input submodule, used to input each node path in the control flow graph into the intermediate model; The storage submodule is used to parse each node using the intermediate model and transfer each node to the intermediate model in the form of a control flow graph; The update submodule includes: a calling unit, which is used to call each node in the node path in sequence through the intermediate model; a determining unit, which is used to determine the hash table operation corresponding to the code instruction of the node according to the corresponding relationship between the code instruction and the hash table operation; an updating unit, which is used to update the hash table state according to the hash table operation and the mapping relationship between the pointer and the memory block; The determining module comprises: A first determination submodule is used to determine nodes and control flow lines according to the process relationship in the code to be detected, and determine a control flow graph according to the nodes and control flow lines; The second determination submodule is used to determine the node path by using the nodes and control flow lines between the start node and the end node in the control flow graph.

5. The device according to claim 4, characterized in that The intermediate model includes an XML parsing model.

6. The device according to claim 5, characterized in that The device is also used to parse each node in the node path using the TinyXML open source library in the XML parsing model.

7. A computer device comprising a memory and a processor, wherein the memory stores a computer program, characterized in that: When the processor executes the computer program, the following steps are implemented: Get the code to be detected corresponding to the memory operation; Determine nodes and control flow lines according to the process relationship in the code to be detected, and determine a control flow graph according to the nodes and the control flow lines, and determine a node path by using the nodes and control flow lines between the start node and the end node in the control flow graph, wherein the information in each node in the control flow graph includes the node number, the line number of the code instruction corresponding to the node, the content of the code instruction corresponding to the node, and the node number of the successor node of the node; Input each of the node paths in the control flow graph into an intermediate model, parse each of the nodes using the intermediate model, and transfer each of the nodes to the intermediate model in the form of the control flow graph, sequentially call each of the nodes in the node path through the intermediate model, determine the hash table operation corresponding to the code instruction of the node according to the correspondence between the code instruction and the hash table operation, and update the hash table state according to the hash table operation and the mapping relationship between the pointer and the memory block; An abnormal operation statement is determined according to the abnormal data of the hash table state and the hash table operation, and a code instruction position where a memory leak exists in the code to be detected is determined according to the abnormal operation statement.

8. The computer device according to claim 7, characterized in that The intermediate model includes an XML parsing model; when the processor executes the computer program, the processor also implements the following steps: The TinyXML open source library in the XML parsing model is used to parse each node in the node path.

9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 3 are implemented.

10. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 3 are implemented.