Resource & Memory leak is a common issue in low-level programming languages like C++ where developers manually manage memory. In large-scale projects, these leaks can lead to performance degradation, crashes, or unpredictable behavior. This project aims to provide a C++ Resource Leak Detection System that helps developers track and identify application resource leaks.
- Track object allocations and deallocations
- Monitor resource usage (e.g., file handles, database connections)
- Detect memory leaks by identifying unfreed objects
- Automatically clean up and release resources
The main motivation for this project is to offer a simple, reusable solution for automatically detecting memory and resource leaks during development and testing, improving the reliability of C++ applications.
Note
This project is developed in C++ and utilizes Google Test for unit testing to ensure robustness and reliability.
- Object Tracking: Tracks memory allocations and deallocations, ensuring that every object created is properly freed.
- Memory Leak Detection: Identifies memory leaks by checking unfreed objects after program termination.
- Resource Management: Monitors resources such as file handles, database connections, and network sockets, ensuring proper cleanup after use.
- Unit Testing with Google Test: Implements Google Test framework for unit testing to verify the functionality of the memory leak detection and resource management features.
- Cross-Platform Compatibility: This library is designed to work across different platforms, such as Linux, macOS, and Windows.
Setup and Build:
To build and run this project, follow these steps:
$ git clone https://github.com/sbidwaibing/CPP-Resource-Leak-Detection-System.git
$ cd MLD_Library_Structures
Note: if the build directory is already present please continue with the **command else use the below one ->
Create a build directory:
$ mkdir build
change directory to build:
** $cd build
Run CMake to configure the project:
$ cmake ..
Build the project using Make:
$ make
Run the tests:
$ ./mld_library
- cmake ..: Configures the project for building.
- make: Compiles the project using the Make build system.
- ./mld_library: Runs the executable, which triggers unit tests and outputs results to the terminal.
Google Test is used in this project to perform unit testing on key components of the library, such as Object Tracking, Memory Leak Detection, and Resource Management.
Cloning Google Test: The Googletest repository is cloned directly into the project, allowing easy integration without relying on external package managers like vcpkg.
- Writing Test Cases: Test cases for the ReferenceTracker, LeakDetector, and ResourceManager classes are written using Google Test.
- Running Tests: The tests are compiled and run as part of the project’s build process. The mld_library executable is built, which automatically runs the tests during execution.
The tests include various checks:
- Object tracking: Verifies that objects are properly tracked during allocation and deallocation.
- Memory leak detection: Ensures that unfreed objects are flagged correctly.
- Resource management: Confirms that resources like database connections, file handles, and network connections are managed and released correctly.
OUTPUT:
[ RUN ] ReferenceTrackerTest.TestTrackObject
Tracked object of type 'int' at 0x159607740
Released object at 0x159607740
[ OK ] ReferenceTrackerTest.TestTrackObject
[ RUN ] LeakDetectorTest.TestMemoryLeaks
Detecting memory leaks...
Object of type 'int' at address 0x159607740
[ OK ] LeakDetectorTest.TestMemoryLeaks
[ RUN ] ResourceManagerTest.TestTrackResource
Tracking resource: Memory at 0x1597041a0
Released resource at 0x1597041a0
[ OK ] ResourceManagerTest.TestTrackResource
These results confirm that the tests were successful and that the memory leak detection library works as expected.
This project provides a lightweight, easy-to-use solution for tracking and identifying memory leaks and resource mismanagement in C++ applications. With its integration with Google Test, the library ensures that its features are thoroughly tested and reliable. The project helps developers keep their C++ code base efficient, stable, and free from memory-related bugs.
Tip
Feel free to contribute, report bugs, or suggest improvements by opening an issue or creating a pull request on GitHub.
Important
For questions or inquiries, you can reach me at Gmail.
Caution
This project is licensed under the MIT License - see the LICENSE file for details.