[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [CPROG] Overloaded delete bliver ikke kaldt



Robert Larsen <sslug@sslug> writes:

> Hey kodere
> 
> Jeg har overloaded new, new[], delete og delete[] for at kunne logge
> memory leaks. Koden ser sådan her ud:
> 
> 
> #ifdef MEMORY_LOGGER
> #include "../util/MemoryLogger.h"
> 
> inline void * operator new(size_t size, char * file, int line)
> {
>     return MemoryLogger::getInstance()->allocateMemory(size, file, line);
> }
> 
> inline void * operator new[](size_t size, char * file, int line)
> {
>     return MemoryLogger::getInstance()->allocateMemory(size, file, line);
> }
> 
> inline void operator delete(void * ptr)
> {
>     MemoryLogger::getInstance()->deAllocateMemory(ptr);
> }
> 
> inline void operator delete[](void * ptr)
> {
>     MemoryLogger::getInstance()->deAllocateMemory(ptr);
> }
> 
> #define new new(__FILE__,__LINE__)
> #define delete (MemoryLogger::getInstance()->pushDeAllocater(
> __FILE__, __LINE__ ), false) ? 
> MemoryLogger::getInstance()->pushDeAllocater( "", 0 ) : delete
> 
> #endif
> 
> 
> Koden er næsten ren afskrift fra "Game Programming Gems 2" sektion 1.10.
> Jeg har testet koden med følgende program som bliver compilet med
> -DMEMORY_LOGGER:
> 
> 
> #include <iostream>
> #include <util/Types.h>
> 
> using namespace std;
> 
> int main(int argc, char ** argv)
> {
>     int * i = new int;
>     int * is = new int[20];
> 
>     delete i;
>     delete[] is;
> 
> #ifdef MEMORY_LOGGER
>     MemoryLogger::finalize();
> #endif
> 
>     return 0;
> }
> 
> 
> Efter preprocesseringen ser det således ud:
> 
> inline void * operator new(size_t size, char * file, int line)
> {
>         return MemoryLogger::getInstance()->allocateMemory(size, file,
> line);
> 
> }
> 
> inline void * operator new[](size_t size, char * file, int line)
> {
>         return MemoryLogger::getInstance()->allocateMemory(size, file,
> line);
> 
> }
> 
> inline void operator delete(void * ptr)
> {
>         MemoryLogger::getInstance()->deAllocateMemory(ptr);
> }
> 
> inline void operator delete[](void * ptr)
> {
>         MemoryLogger::getInstance()->deAllocateMemory(ptr);
> }
> # 3 "tester.cpp" 2
> 
> using namespace std;
> 
> int main(int argc, char ** argv)
> {
>         int * i = new("tester.cpp",8) int;
>         int * is = new("tester.cpp",9) int[20];
> 
>         (MemoryLogger::getInstance()->pushDeAllocater( "tester.cpp",
> 11 ), false) ? MemoryLogger::getInstance()->pushDeAllocater( "", 0 ) :
> delete i;
> 
>         (MemoryLogger::getInstance()->pushDeAllocater( "tester.cpp",
> 12 ), false) ? MemoryLogger::getInstance()->pushDeAllocater( "", 0 ) :
> delete[] is;
> 
> 
> 
>         MemoryLogger::finalize();
> 
> 
>         return 0;
> }
> 
> Når jeg stepper igennem koden med 'ddd' kan jeg se at mine new og
> new[] operatorer bliver kaldt og at
> MemoryLogger::getInstance()->pushDeAllocater() bliver kaldt med de
> rigtige parametre (de første kald) men at det er del_op.cc's versioner
> af delete og delete[] som kaldes.

Hvilke versioner skulle det ellers være?

> 
> Nogen som har et godt bud på hvorfor mine versioner ikke bruges ?

Fordi dine versioner tager 3 argumenter, og som du kan se i den
expanderede kode så får den kun et argument.

Er der noget jeg har overset?

Jarl


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:09 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *