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
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 #include <iostream> using namespace std; #include "debugNew.h" int main(void) { int *p = new int; delete p; return 0; }
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
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
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 #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); }
A la compilation : tout ce passe bien.
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
Mais voici le résultat de l'éxécution:
Et si je met cette ligne en commentaire : "CMemoryManager::instance().release(ptr, false);", voici le resultat:alloc
desalloc
terminate called after throwing an instance of '__gnu_cxx::recursive_init'
what(): N9__gnu_cxx14recursive_initE
Abandon
Pourquoi ai-je cette erreur ?alloc
desalloc
desalloc
desalloc
desalloc
desalloc
Merci d'avance...
Partager