Message-Id: <9707071300.AA17833@sun4nl.NL.net> Date: Mon, 7 Jul 1997 12:22:00 +0200 From: Rob den Boer Subject: AW: Memory Leak patch Please. To: b2042026 Cc: djgpp Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="---- =_0_MIME_Boundary_278.33c0eae7.im4opx70.NTSPIJK" Precedence: bulk ------ =_0_MIME_Boundary_278.33c0eae7.im4opx70.NTSPIJK Hi all, The attached file MEMCHK.ZIP contains the memchk.c source. With this you can track down memory bugs, like not freeing pointers, using wrong pointers etc. It's adapted from an old piece of code published in DDJ back in the 90's, and made it work with DJGPP. You can use it as follows: 1. after your regular #include's add: #define MEM_DEBUG 1 #include "memchk.c" 2. first thing in your main: #ifdef MEM_DEBUG mem_check_init(); #endif 3. run your program! 4. check the file MEMCHK.LOG If all is well, the logfile will show something like this: Summary: Total allocated memory : 0 bytes Memory usage maximum : 11880 bytes Number of pointers used : 70 total Bitmaps : 861448 bytes, 85 x The 'total' allocated should be 0, otherwise you have a memory leak. You will see when things go wrong. Checks are done for: 1. malloc calls returning NULL pointers 2. multiple allocating of the same pointer 3. freeing a null pointer 4. freeing a pointer multiple times 5. bounds check (not 100% proof, see below) Use #define MEM_DEBUG_ALL to see all allocations/free's in your program. remove the MEM_DEBUG defines when your programs works great and you want to compile the final version, this leaves out all checking code. How does it work? The init function installs an atexit function, which does the end reporting. This code keeps an administration of all malloc/calloc/free calls, and keeps track of memory. To 'catch' the malloc/free calls in your program, memchk.c #defines malloc/calloc/free and replaces them with it's own custom versions. For each allocation there's a MEMHDR structure, which is linked into a list. Bounds checking is done by putting a magic value before and after the allocated memory area. Usually when you move outside the allocated memory area, you will touch the magic values, and this will be detected. It's not 100% proof, because you can skip them, but it's neat when it's detected. I quickly re-wrote this for my own use, because I had the same problems as you. Therefore it's not finished yet, like the strdup function doesn't work yet, and it doesn't work on multiple sourcefiles yet. I have to split it into a .c/.h for that. The BITMAP thing is for Allegro, it only counts how much was used for bitmaps. (it's no use tracking them down, because Allegro free's them on exit). regards Rob den Boer [[ MEMCHK.ZIP : 2101 in MEMCHK.ZIP ]] ------ =_0_MIME_Boundary_278.33c0eae7.im4opx70.NTSPIJK Content-Type: application/octet-stream; name="MEMCHK.ZIP" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="MEMCHK.ZIP" UEsDBBQAAAAIAMBV5yKhJ0pzKwkAAJUjAAAIAAAATWVtY2hrLmPtGmtv28jxswLkP8w58FWUK ce5D23PqoImF18TwAkOueRTWwgUubIIUySPD8tqkf/emdnZBynKsX1tgaJHwCI5uzvv13L9LM 3jrE0U/KlukrQ4Xb98+uSZD8vS5R6wSvOrHjDKMnVV6fUIT9QqzRV8+vj5AqYvHODHV5c/X8A ZzXk+gY3axOvrp08A6LGodoBoijhq0iKHRC3bqyukBHTxJP45++P07PfT7/8AH4slTsrhdaEq HlGbKM3OoSqWpwhfIvjPuWpO3/7ww2mePX0ywovnJWkdZ1G6UdU5v39apzXEBYqB97xoAFlN6 7VKTkHGVaV+R0Nw1UZVlDdKwXYdNXWhblQVQlHhkiZXdQ0rfI7yHZRR1aRxm0UVlG1VFrWaQV sjhQaiBnZFC8U2hyqtr0+ZxjvYplkGTXStiE6lalyTLtMsbXYWaVzktfqlVfgLjWV6EyG5qkj aWIUoW6XiBlFUkOb65ZS0PXlOv2SxFZoC3l+8X7y5eP35LwRrdqUiIBq2xbU49ubtR/gn84VY GjLOoomuZhoSr1GoSV3FqzRTAmvzOr3KVcLTM7R0XuHIiN7EpCohQJ3+Qy0aiOkuS7MCTbyJr tJ4sVRo732wypMO6bKp5L3L8aSs1E0Ik1zdNjjhC8Hfvvk4IxnrBr0qFghMUKIsrZv5h8+Xl7 gii+R5Zmdq+ii45X9+NjC6iW4H4cu02UTlHUNfWZkX+4NOR/MXg2OoqPn3crkJZAS0HszBW/X ju8sLmKxKVs5NkSZMO16r+HoczLSvWDCpahw8fcIuYVSolwKUc1Em22S7RqeAcQnfzIEUGpAf jdIVgqYvrSphPufcEIBcOAd/VyVmlg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