Bonjour,

J'esseye de faire un memoryManager basé sur ce tutorial : http://loulou.developpez.com/tutorie...artie1/#L2.2.1

J'ai donc un fihcier test.cpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;
 
#include "debugNew.h"
 
int main(void)
{
	int *p = new int;
	delete p;
	return 0;
}
Un fichier debugNew.h
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef DEBUGNEW_H
#define DEBUGNEW_H
 
#include "CMemoryManager.h"
 
inline void* operator new(std::size_t size, const char *file, int line)
{
	cout<<"alloc"<<endl;
	return CMemoryManager::instance().allocate(size, file, line, false);
}
 
inline void operator delete(void *ptr) throw()
{
	cout<<"desalloc"<<endl;
	CMemoryManager::instance().release(ptr, false);
}
 
#endif
 
#ifndef new
    #define new new(__FILE__, __LINE__)
#endif
Le CMemoryManager.cpp:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "CMemoryManager.h"
using namespace std;
 
CMemoryManager &CMemoryManager::instance(void)
{
    static CMemoryManager inst;
    return inst;
}
 
void *CMemoryManager::allocate(size_t size, const char *file, int line, bool array)
{
	void* ptr = malloc(size);
	return ptr;
}
 
void CMemoryManager::release(void* ptr, bool array)
{
	free(ptr);
}
Et pour finir le CMemoryManager.h
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#ifndef CMEMORYMANAGER_H
#define CMEMORYMANAGER_H
 
#include <iostream>
#include <map>
#include <fstream>
#include <stack>
#include <stdlib.h> //for malloc and free
 
class CMemoryManager
{
	public:
		static CMemoryManager &instance(void);
 
		void *allocate(std::size_t, const char *, int, bool);
		void release(void *, bool);
	private:
		CMemoryManager(void){};
		~CMemoryManager(void){};
 
		struct TBlock
		{
			std::size_t size;
			const char *file;
			int line;
			bool array;
		};
		std::map<void*, TBlock> mBlocks;
		std::stack<TBlock> mDeleteStack;
};
 
#endif
A la compilation : tout ce passe bien.
Mais voici le résultat de l'éxécution:
alloc
desalloc
terminate called after throwing an instance of '__gnu_cxx::recursive_init'
what(): N9__gnu_cxx14recursive_initE
Abandon
Et si je met cette ligne en commentaire : "CMemoryManager::instance().release(ptr, false);", voici le resultat:
alloc
desalloc
desalloc
desalloc
desalloc
desalloc
Pourquoi ai-je cette erreur ?

Merci d'avance...