472,337 Members | 1,215 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,337 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 4522
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...
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...
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 =...
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;...
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...
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...
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...
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...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

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.