CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2009
    Posts
    2

    Question new operator overload and singleton instantiation

    I've run into a problem today that didn't dawn on me till I took a look at my call stack in Visual Studios. I'm currently trying to build a class that will display pieces of allocated memory that isn't cleaned up before the end of execution. The basic idea for now was to increment a counter within a singleton class whenever the new operator was used and decrement the counter once delete was called. I understand the logic error I created and I have created a work around. I'm currently looking for some advice for a cleaner, single instance solution then the one I have now. Here was my original code:

    Code:
    #ifndef CNEW_H
    #define CNEW_H
    
    #include "CAllocReporter.h"
    
    // overload new
    void* operator new(size_t size)
    {
    	void *p;
    
    	p = malloc(size);
    
    	if(!p) 
    	{
    		// Out of memory
    		return NULL;
    		/*bad_alloc ba;
    		throw ba;*/
    	}
    	if(s_pAllocReporter)
    		AllocReporter()->Add();
    
    	return p;
    }
    
    #endif
    
    class CAllocReporter
    {
    public:
    	static CAllocReporter* Get()
    	{
    		if(m_pInstance)
    			return m_pInstance;
    		m_pInstance = new CAllocReporter();
    	}
    
    	void 
    
    	void Add(){ m_AllocTotal++; }
    	void Remove(){ m_AllocTotal--; }
    
    	int GetAllocTotal(){ return m_AllocTotal; }
    	
    private:
    	CAllocReporter(): m_AllocTotal(0)
    	{}
    	~CAllocReporter()
    	{}
    
    	static CAllocReporter* m_pInstance;
    
    	int m_AllocTotal;
    };
    
    CAllocReporter* CAllocReporter::m_pInstance = NULL;
    
    CAllocReporter* AllocReporter()
    {
    	return CAllocReporter::Get();
    }
    During the previous code why Singleton was never instantiated because the allocated memory was never returned from my new overload. In turn creating an infinite loop with the constant call to AllocReporter()->Add();

    My work around:

    Code:
    void* operator new(size_t size)
    {
    	void *p;
    
    	p = malloc(size);
    
    	if(!p) 
    	{
    		// Out of memory
    		return NULL;
    		/*bad_alloc ba;
    		throw ba;*/
    	}
    	if(s_pAllocReporter)
    		s_pAllocReporter->Add();
    
    	return p;
    }
    
    #ifndef CALLOC_H
    #define CALLOC_H
    
    #include <windows.h>
    #include <iostream>
    #include <cstdlib>
    
    // not very happy with the implementation of this class, Singleton design will not
    //		work with overloaded new because of the member call inside of it
    
    class CAllocReporter
    {
    public:
    	CAllocReporter(): m_AllocTotal(0)
    	{}
    	~CAllocReporter()
    	{}
    
    	void Add(){ m_AllocTotal++; }
    	void Remove(){ m_AllocTotal--; }
    
    	int GetAllocTotal(){ return m_AllocTotal; }
    	
    private:
    	//static bool m_bInit;
    	int			m_AllocTotal;
    };
    
    static CAllocReporter* s_pAllocReporter = NULL;
    
    void Report()
    {
    	// we want to output this to our console
    	if(s_pAllocReporter)
    	{
    		char msg[256];
    		sprintf(msg, "Total memory allocated, including leaked: %d", s_pAllocReporter->GetAllocTotal());
    		MessageBox( NULL, msg, "MsgBox", MB_OK | MB_ICONERROR );
    	}
    }
    
    #endif
    I'd like to find a better way to implement this idea ( single instance design mainly ).

  2. #2
    Join Date
    Sep 2005
    Location
    London
    Posts
    208

    Re: new operator overload and singleton instantiation

    Consider Loki's singleton:
    http://loki-lib.cvs.sourceforge.net/....h?view=markup

    The class template policies can generate many different types of singletons, allowing you to specify how the Singleton is created, destroyed and whether it is thread safe.


    Regards
    Doron

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured