473,416 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Heap Manager and new/delete overloading

Hello,
I'm trying to create a basic Heap manager and i have some question
about new/delete overloading.
The following code give me this output :
$./heap
registered : 0x804d098
0x804d008 _Delete unknown block
registered : 0x804d138
0x804d008 _Delete unknown block
0x804d098 _Delete ok
0x804d0a8 _Delete unknown block
0x804d138 _Delete ok
0x804d148 _Delete unknown block


Why is the HeapManager::_Delete called to delete objects that weren't
created using HeapManager::_New ?
The only reason I see is that these calls come from my BlocksMap
internals. Is it possible to differenciate delete used by the libstdc++
and delete used by the parts of my programs which are after the
overloading ?
Thanks for any advice :-)
Silver

------------------------------------------------
#include <map>
#include <vector>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <iostream>

using namespace std;

class HeapManager
{
public:
struct BlockDescriptor {
size_t size;
char filename[100];
int line;
};
static HeapManager* pSelf;
~HeapManager ();
void* _New (size_t size, const string& filename, int line);
void _Delete (void* addr);
private:
typedef map<void*, BlockDescriptor*> t_blocksMap;
t_blocksMap BlocksMap;
};

HeapManager::~HeapManager ()
{
vector<t_blocksMap::iterator> toErase;
t_blocksMap::iterator i;
for(i=BlocksMap.begin(); i!=BlocksMap.end(); i++) {
cout << "leak : " << i->second->filename << ":" << i->second->line
<<endl;
free(i->second);
free(i->first);
toErase.push_back(i);
}
for(unsigned int j =0; j<toErase.size(); j++) {
BlocksMap.erase(toErase[j]);
}
}

void* HeapManager::_New(size_t size, const string& filename, int line)
{
if(size <= 0)
return 0;

BlockDescriptor* pDesc =
(BlockDescriptor*)malloc(sizeof(BlockDescriptor));
pDesc->size = size;
strncpy(pDesc->filename,filename.data(),99);
pDesc->filename[99] = '\0';
pDesc->line = line;
void* block = malloc(size);
BlocksMap.insert(make_pair(block,pDesc));
cout << "registered : " << block << endl;
return block;
}

void HeapManager::_Delete(void* addr)
{
if(addr == 0)
return;
cout << addr << " ";
t_blocksMap::iterator i = BlocksMap.find(addr);
if (i != BlocksMap.end()) {
cout << "_Delete ok " << endl;
free(i->second);
BlocksMap.erase(i);
} else {
cout << "_Delete unknown block" << endl;
}
free(addr);
}

void* operator new(size_t size, const string& filename, int line) {
return HeapManager::pSelf->_New(size, filename, line);
}

void* operator new[](size_t size, const string& filename, int line) {
return HeapManager::pSelf->_New(size, filename, line);
}

void operator delete (void* addr) {
HeapManager::pSelf->_Delete(addr);
}

void operator delete [] (void* addr) {
HeapManager::pSelf->_Delete(addr);
}

#define new new(__FILE__,__LINE__)

HeapManager* HeapManager::pSelf = NULL;
HeapManager theHeap;

class Foo
{
public:
int bar;
};

int main()
{
HeapManager::pSelf = &theHeap;
Foo* ptr = new Foo;
Foo* ptr2 = new Foo;
delete ptr;
delete ptr2;
return 0;

}

Dec 20 '05 #1
3 4623
si*******@gmail.com wrote:
Hello,
I'm trying to create a basic Heap manager and i have some question
about new/delete overloading.
The following code give me this output :
$./heap
registered : 0x804d098
0x804d008 _Delete unknown block
registered : 0x804d138
0x804d008 _Delete unknown block
0x804d098 _Delete ok
0x804d0a8 _Delete unknown block
0x804d138 _Delete ok
0x804d148 _Delete unknown block


Why is the HeapManager::_Delete called to delete objects that weren't
created using HeapManager::_New ?
The only reason I see is that these calls come from my BlocksMap
internals. Is it possible to differenciate delete used by the libstdc++
and delete used by the parts of my programs which are after the
overloading ?
Thanks for any advice :-)
Silver

------------------------------------------------
#include <map>
#include <vector>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <iostream>

using namespace std;

class HeapManager
{
public:
struct BlockDescriptor {
size_t size;
char filename[100];
int line;
};
static HeapManager* pSelf;
~HeapManager ();
void* _New (size_t size, const string& filename, int line);
void _Delete (void* addr);
private:
typedef map<void*, BlockDescriptor*> t_blocksMap;
t_blocksMap BlocksMap;
};

HeapManager::~HeapManager ()
{
vector<t_blocksMap::iterator> toErase;
t_blocksMap::iterator i;
for(i=BlocksMap.begin(); i!=BlocksMap.end(); i++) {
cout << "leak : " << i->second->filename << ":" << i->second->line
<<endl;
free(i->second);
free(i->first);
toErase.push_back(i);
}
for(unsigned int j =0; j<toErase.size(); j++) {
BlocksMap.erase(toErase[j]);
}
}

void* HeapManager::_New(size_t size, const string& filename, int line)
{
if(size <= 0)
return 0;

BlockDescriptor* pDesc =
(BlockDescriptor*)malloc(sizeof(BlockDescriptor));
pDesc->size = size;
strncpy(pDesc->filename,filename.data(),99);
pDesc->filename[99] = '\0';
pDesc->line = line;
void* block = malloc(size);
BlocksMap.insert(make_pair(block,pDesc));
cout << "registered : " << block << endl;
return block;
}

void HeapManager::_Delete(void* addr)
{
if(addr == 0)
return;
cout << addr << " ";
t_blocksMap::iterator i = BlocksMap.find(addr);
if (i != BlocksMap.end()) {
cout << "_Delete ok " << endl;
free(i->second);
BlocksMap.erase(i);
} else {
cout << "_Delete unknown block" << endl;
}
free(addr);
}

void* operator new(size_t size, const string& filename, int line) {
return HeapManager::pSelf->_New(size, filename, line);
}

void* operator new[](size_t size, const string& filename, int line) {
return HeapManager::pSelf->_New(size, filename, line);
}

void operator delete (void* addr) {
HeapManager::pSelf->_Delete(addr);
}

void operator delete [] (void* addr) {
HeapManager::pSelf->_Delete(addr);
}

#define new new(__FILE__,__LINE__)

HeapManager* HeapManager::pSelf = NULL;
HeapManager theHeap;

class Foo
{
public:
int bar;
};

int main()
{
HeapManager::pSelf = &theHeap;
Foo* ptr = new Foo;
Foo* ptr2 = new Foo;
delete ptr;
delete ptr2;
return 0;

}


First of all, symbols such as _New and _Delete are illegitimate. All
symbols beginning with an underscore and captial letter are reserved
for use by the implementation. Fix that, try it again to make sure it's
not an implementation conflict, and then we can talk.

Second, you might be interested in this FAQ that discusses overloading
new and delete:

http://www.parashift.com/c++-faq-lit...html#faq-11.14

Third, you might consider using the singleton pattern for your heap
manager rather than that pointer to self trick. See _Modern C++ Design_
chapter 6 for more than you ever wanted to know about C++ singletons.

Cheers! --M

Dec 20 '05 #2
Thanks for your answers.
From what i learnt from the FAQ, if i want that only the objects

created through my new(size_t, const string&, int) call my overloaded
delete, i must redefine delete(void*, const string& , int ).

I rewrote my code with Singleton and correct symbols, but if i redefine
delete(void*, const string&, int), the program crash in dbgheap.c:1252
(_CrtIsValidHeapPointer). If i comment my 'new' delete an keep the old
delete(void*), i get the same output than in the first message.

Silver

----------------
// heap.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <map>
#include <vector>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <iostream>

using namespace std;

class HeapManager
{
public:
struct BlockDescriptor {
size_t size;
char filename[100];
int line;
};
static HeapManager* Instance();

void* myNew (size_t size, const string& filename, int line);
void myDelete (void* addr);
private:
typedef map<void*, BlockDescriptor*> t_blocksMap;
t_blocksMap BlocksMap;
HeapManager();
HeapManager(const HeapManager&);
HeapManager& operator= (const HeapManager&);
~HeapManager ();
};

HeapManager::HeapManager()
{

}

HeapManager::HeapManager(const HeapManager&)
{

}

HeapManager& HeapManager::operator = (const HeapManager&)
{
return *Instance();
}

HeapManager* HeapManager::Instance()
{
static HeapManager inst;
return &inst;
}

HeapManager::~HeapManager ()
{
vector<t_blocksMap::iterator> toErase;
t_blocksMap::iterator i;
for(i=BlocksMap.begin(); i!=BlocksMap.end(); i++) {
cout << "leak : " << i->second->filename << ":" << i->second->line
<<endl;
free(i->second);
free(i->first);
toErase.push_back(i);
}
for(unsigned int j =0; j<toErase.size(); j++) {
BlocksMap.erase(toErase[j]);
}
}

void* HeapManager::myNew(size_t size, const string& filename, int line)
{
if(size <= 0)
return 0;

BlockDescriptor* pDesc =
(BlockDescriptor*)malloc(sizeof(BlockDescriptor));
pDesc->size = size;
strncpy(pDesc->filename,filename.data(),99);
pDesc->filename[99] = '\0';
pDesc->line = line;
void* block = malloc(size);
BlocksMap.insert(make_pair(block,pDesc));
cout << "registered : " << block << endl;
return block;
}

void HeapManager::myDelete(void* addr)
{
if(addr == 0)
return;
cout << addr << " ";
t_blocksMap::iterator i = BlocksMap.find(addr);
if (i != BlocksMap.end()) {
cout << "_Delete ok " << endl;
free(i->second);
BlocksMap.erase(i);
} else {
cout << "_Delete unregistered" << endl;
}
free(addr);
}

void* operator new(size_t size, const string& filename, int line) {
return HeapManager::Instance()->myNew(size, filename, line);
}

void* operator new[](size_t size, const string& filename, int line) {
return HeapManager::Instance()->myNew(size, filename, line);
}

void operator delete(void* addr, const string& filename, int line) {
HeapManager::Instance()->myDelete(addr);
}

void operator delete[](void* addr, const string& filename, int line){
HeapManager::Instance()->myDelete(addr);
}
/*
void operator delete (void* addr) {
HeapManager::Instance()->myDelete(addr);
}

void operator delete [] (void* addr) {
HeapManager::Instance()->myDelete(addr);
}*/

#define new new(__FILE__,__LINE__)

class Foo
{
public:
int bar;
};

int main()
{
Foo* ptr = new Foo;
Foo* ptr2 = new Foo;
delete ptr;
delete ptr2;
return 0;

}

Dec 21 '05 #3
si*******@gmail.com wrote:
Thanks for your answers.
From what i learnt from the FAQ, if i want that only the objects created through my new(size_t, const string&, int) call my overloaded
delete, i must redefine delete(void*, const string& , int ).


Yes, you must do this for exception safety. See the FAQ.

I rewrote my code with Singleton and correct symbols, but if i redefine
delete(void*, const string&, int), the program crash in dbgheap.c:1252
(_CrtIsValidHeapPointer). If i comment my 'new' delete an keep the old
delete(void*), i get the same output than in the first message.
You must not do this. See below.

Silver

----------------
// heap.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <map>
#include <vector>
#include <stdlib.h>
Prefer <cstdlib>
#include <string>
#include <string.h>
Prefer <cstring>
#include <iostream>

using namespace std;

class HeapManager
{
public:
struct BlockDescriptor {
size_t size;
char filename[100];
int line;
};
static HeapManager* Instance();

void* myNew (size_t size, const string& filename, int line);
void myDelete (void* addr);
private:
typedef map<void*, BlockDescriptor*> t_blocksMap;
t_blocksMap BlocksMap;
HeapManager();
HeapManager(const HeapManager&);
HeapManager& operator= (const HeapManager&);
~HeapManager ();
};

HeapManager::HeapManager()
{

}

HeapManager::HeapManager(const HeapManager&)
{

}

HeapManager& HeapManager::operator = (const HeapManager&)
{
return *Instance();
}

HeapManager* HeapManager::Instance()
{
static HeapManager inst;
return &inst;
}

HeapManager::~HeapManager ()
{
vector<t_blocksMap::iterator> toErase;
t_blocksMap::iterator i;
for(i=BlocksMap.begin(); i!=BlocksMap.end(); i++) {
cout << "leak : " << i->second->filename << ":" << i->second->line
<<endl;
free(i->second);
free(i->first);
toErase.push_back(i);
}
for(unsigned int j =0; j<toErase.size(); j++) {
BlocksMap.erase(toErase[j]);
}
}

void* HeapManager::myNew(size_t size, const string& filename, int line)
{
if(size <= 0)
return 0;

BlockDescriptor* pDesc =
(BlockDescriptor*)malloc(sizeof(BlockDescriptor));
pDesc->size = size;
strncpy(pDesc->filename,filename.data(),99);
pDesc->filename[99] = '\0';
pDesc->line = line;
void* block = malloc(size);
BlocksMap.insert(make_pair(block,pDesc));
cout << "registered : " << block << endl;
return block;
}

void HeapManager::myDelete(void* addr)
{
if(addr == 0)
return;
cout << addr << " ";
t_blocksMap::iterator i = BlocksMap.find(addr);
if (i != BlocksMap.end()) {
cout << "_Delete ok " << endl;
free(i->second);
BlocksMap.erase(i);
} else {
cout << "_Delete unregistered" << endl;
}
free(addr);
}

void* operator new(size_t size, const string& filename, int line) {
return HeapManager::Instance()->myNew(size, filename, line);
}

void* operator new[](size_t size, const string& filename, int line) {
return HeapManager::Instance()->myNew(size, filename, line);
}

void operator delete(void* addr, const string& filename, int line) {
HeapManager::Instance()->myDelete(addr);
}

void operator delete[](void* addr, const string& filename, int line){
HeapManager::Instance()->myDelete(addr);
}
/*
void operator delete (void* addr) {
HeapManager::Instance()->myDelete(addr);
}

void operator delete [] (void* addr) {
HeapManager::Instance()->myDelete(addr);
}*/

#define new new(__FILE__,__LINE__)
Get rid of this macro. See below.

class Foo
{
public:
int bar;
};

int main()
{
Foo* ptr = new Foo;
Foo* ptr2 = new Foo;
delete ptr;
delete ptr2;
return 0;

}


First, regarding the singleton, I'd suggest something along these
lines, though perhaps you need something even more sophisticated in
terms of lifetime management (for which, see _Modern C++ Design_):

template<class T>
class Singleton
{
public:
static T& Instance();
private:
// Disabled functions
Singleton();
Singleton( const Singleton& );
Singleton& operator=( const Singleton& );
Singleton* operator&();
~Singleton();
};

template<class T>
T& Singleton<T>::Instance()
{
static T myObject;
return myObject;
}
class HeapManager
{
// Private ctor/dtor accessible only to friends
friend class Singleton<HeapManager>;
HeapManager() { /* ... */ }
~HeapManager() { /* ... */ }

// Disabled functions for singleton usage
HeapManager( const HeapManager& );
HeapManager& operator=( const HeapManager& );
HeapManager* operator&();

// ...

public:
// ...
};

typedef Singleton<HeapManager> theHeapManager;

Second, regarding the questions from your previous post:
registered : 0x804d098
0x804d008 _Delete unknown block
registered : 0x804d138
0x804d008 _Delete unknown block
0x804d098 _Delete ok
0x804d0a8 _Delete unknown block
0x804d138 _Delete ok
0x804d148 _Delete unknown block


Why is the HeapManager::_Delete called to delete objects that weren't
created using HeapManager::_New ?


Because in your original code you overload the *global* new and delete
operators. Consequently, your delete operator is being called from
std::string, but your new operator wasn't called from std::string
because the function signatures didn't match and your "#define new"
wasn't in effect yet.

In your updated code, you (rightly) provide a matching delete operator
for your new operator, but you incorrectly invoke the global (i.e., not
your own) delete operator in main(). Also, your updated code won't work
since the file and line numbers must be identical to match your map's
key. With your current operators, you'd need to do something like this
(assuming you get rid of the macro):

Foo* ptr = new("Bob",1) Foo;
Foo* ptr2 = new("Joe",2) Foo;
operator delete(ptr, "Bob",1); // same parameters as above!
operator delete(ptr2, "Joe",2); // same parameters as above!

You'll probably want to change to some other key type than a string/int
pair since the filename/line number thing won't work. In order to get
rid of the ugly syntax in this code, I'll refer you to that same FAQ
again. It describes in detail how to create a custom new/delete pair
and then how to overload the gloabal new/delete operators to use them.

http://www.parashift.com/c++-faq-lit...html#faq-11.14

If you don't understand the FAQ, ask some specific questions. I'm sure
someone here can help.

Cheers! --M

Dec 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
17
by: Jonas Rundberg | last post by:
Hi I just started with c++ and I'm a little bit confused where stuff go... Assume we have a class: class test { private: int arr; };
9
by: swengtoo | last post by:
My understanding is that an auto_ptr can never own an object that was created on the stack. This is because when auto_ptr goes out of scope, it calls 'delete' for the object it points to. Is my...
37
by: yogpjosh | last post by:
Hello All, I was asked a question in an interview.. Its related to dynamically allocated and deallocated memory. eg. //start char * p = new char; ...
3
by: toton | last post by:
Operator overloading has a sort syntax rather than member function call for stack based memory allocation. like complex<int> c1,c2,c3; c3= c1+c2; How the same can be applied to heap based...
13
by: Karthik | last post by:
In a recent techincal interview on C++, I came across this strange question.... How to ensure that your class object is always created in heap (by a new operator)? The compiler should throw...
16
by: sarathy | last post by:
Hi all, I need a few clarifications regarding memory allocaion in C++. I apologize for the lengthy explanation. 1. In C++, Objects are allocated in heap. What does heap refer to? Is it an area...
4
by: Justin | last post by:
Recieved the message: SQL0954C Not enough storage is available in the application heap to process the statement. SQLSTATE=57011 After viewing other posts, they recommended changing the...
5
by: kumarmdb2 | last post by:
Hi guys, For last few days we are getting out of private memory error. We have a development environment. We tried to figure out the problem but we believe that it might be related to the OS...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.