memory

Discussion about everything. New games, 3d math, development tips...
Post Reply
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

memory

Post by sudi »

i was a little bored and played a little bit with the new and delete operators to see if i got some memory leaks. well the following happend....

Image


I guess i just did something wrong but i am not sure...anyway here is the code used.

EDIT: just compile it along with your code. should work....maybe thats the error ;)

Code:

Code: Select all

class MemoryManager
{
public:
    static MemoryManager& Instance(void);
    void print(void);
    int Allocated;
    int Dellocated;
    int New;
    int NewMore;
    int Delete;
    int DeleteMore;
private:
    MemoryManager(void);
    ~MemoryManager(void);
};

Code: Select all

#include <stdlib.h>
#include <stdio.h>
#include <new>

#include <core/ILog.h>
#include "memoryManagement.h"
MemoryManager& MemoryManager::Instance(void)
{
    static MemoryManager mem;
    return mem;
}

void MemoryManager::print(void)
{
    nlogl(1)<<"Memory usage:";
    nlogl(1)<<"new: "<<New<<" - "<<Delete;
    nlogl(1)<<"new []: "<<NewMore<<" - "<<DeleteMore;
    nlogl(1)<<"Allocated: "<<Allocated;
    nlogl(1)<<"Dellocated: "<<Dellocated<<nlendl;
}

MemoryManager::MemoryManager(void)
{
    Allocated = 0;
    Dellocated = 0;
    New = 0;
    NewMore = 0;
    Delete = 0;
    DeleteMore = 0;
}

MemoryManager::~MemoryManager(void)
{
    print();
}

void* operator new (size_t number_of_bytes) throw (std::bad_alloc)
{
    MemoryManager::Instance().Allocated+=number_of_bytes;
    MemoryManager::Instance().New++;
    return malloc(number_of_bytes);
};

void* operator new[] (size_t number_of_bytes) throw (std::bad_alloc)
{
    MemoryManager::Instance().Allocated+=number_of_bytes;
    MemoryManager::Instance().NewMore++;
    return malloc(number_of_bytes);
};

void  operator delete  (void* address) throw ()
{
    MemoryManager::Instance().Delete++;
    free(address);
}

void  operator delete[](void* address) throw ()
{
    MemoryManager::Instance().DeleteMore++;
    free(address);
}
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
Post Reply