fork download
  1. #include <iostream>
  2. #include <memory>
  3.  
  4.  
  5. template<typename T>
  6. struct Holder {
  7. Holder() {
  8. std::cout << "Holder()::Holder()" << std::endl;
  9. if (!Self) Self = new T();
  10. Self->Init();
  11. }
  12. ~Holder() {
  13. if (Self) {
  14. Self->Cleanup();
  15. delete Self;
  16. std::cout << "~Holder()::Holder()" << std::endl;
  17. }
  18. }
  19. T* Self = nullptr;
  20. };
  21.  
  22. class SomeClass
  23. {
  24. public:
  25. void Init(){}
  26. void Cleanup() {}
  27. int member;
  28. };
  29.  
  30. ////////////////////////////////////////////////////////////////////////////////////////
  31.  
  32. template <typename T>
  33. class Singleton {
  34. public:
  35. static T* Instance() {
  36. std::cout << "Singlton::Instance()" << std::endl;
  37. static Holder<T> Dummy;
  38. return Dummy.Self;
  39. }
  40. private:
  41. Singleton() = delete;
  42. Singleton(Singleton const&) = delete;
  43. Singleton& operator= (Singleton const&) = delete;
  44. Singleton(Singleton const&&) = delete;
  45. Singleton& operator= (Singleton const&&) = delete;
  46. };
  47.  
  48. int main()
  49. {
  50. {
  51. auto pInst = Singleton<SomeClass>::Instance();
  52. pInst->member = 10;
  53. std::cout << pInst->member << std::endl;
  54. std::unique_ptr<SomeClass> pObj(pInst);
  55. }
  56. auto pInst = Singleton<SomeClass>::Instance();
  57. pInst->member = 20;
  58.  
  59. return 0;
  60. }
  61.  
Runtime error #stdin #stdout #stderr 0s 80768KB
stdin
Standard input is empty
stdout
Singlton::Instance()
Holder()::Holder()
10
Singlton::Instance()
stderr
*** Error in `./prog': double free or corruption (fasttop): 0x0000557abbacbc30 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x70bcb)[0x2ba3cbcb8bcb]
/lib/x86_64-linux-gnu/libc.so.6(+0x76f96)[0x2ba3cbcbef96]
/lib/x86_64-linux-gnu/libc.so.6(+0x7778e)[0x2ba3cbcbf78e]
./prog(+0xe77)[0x557ab9b48e77]
/lib/x86_64-linux-gnu/libc.so.6(+0x35920)[0x2ba3cbc7d920]
/lib/x86_64-linux-gnu/libc.so.6(+0x3597a)[0x2ba3cbc7d97a]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf8)[0x2ba3cbc682b8]
./prog(+0xd4a)[0x557ab9b48d4a]
======= Memory map: ========
2ba3caf68000-2ba3caf8b000 r-xp 00000000 fd:00 2840974                    /lib/x86_64-linux-gnu/ld-2.24.so
2ba3caf8b000-2ba3caf8f000 rw-p 00000000 00:00 0 
2ba3caf98000-2ba3caf9d000 rw-p 00000000 00:00 0 
2ba3cb18b000-2ba3cb18c000 r--p 00023000 fd:00 2840974                    /lib/x86_64-linux-gnu/ld-2.24.so
2ba3cb18c000-2ba3cb18d000 rw-p 00024000 fd:00 2840974                    /lib/x86_64-linux-gnu/ld-2.24.so
2ba3cb18d000-2ba3cb18e000 rw-p 00000000 00:00 0 
2ba3cb18e000-2ba3cb300000 r-xp 00000000 fd:00 2967755                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22
2ba3cb300000-2ba3cb500000 ---p 00172000 fd:00 2967755                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22
2ba3cb500000-2ba3cb50a000 r--p 00172000 fd:00 2967755                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22
2ba3cb50a000-2ba3cb50c000 rw-p 0017c000 fd:00 2967755                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22
2ba3cb50c000-2ba3cb510000 rw-p 00000000 00:00 0 
2ba3cb510000-2ba3cb613000 r-xp 00000000 fd:00 2841003                    /lib/x86_64-linux-gnu/libm-2.24.so
2ba3cb613000-2ba3cb812000 ---p 00103000 fd:00 2841003                    /lib/x86_64-linux-gnu/libm-2.24.so
2ba3cb812000-2ba3cb813000 r--p 00102000 fd:00 2841003                    /lib/x86_64-linux-gnu/libm-2.24.so
2ba3cb813000-2ba3cb814000 rw-p 00103000 fd:00 2841003                    /lib/x86_64-linux-gnu/libm-2.24.so
2ba3cb814000-2ba3cb82a000 r-xp 00000000 fd:00 2840941                    /lib/x86_64-linux-gnu/libgcc_s.so.1
2ba3cb82a000-2ba3cba29000 ---p 00016000 fd:00 2840941                    /lib/x86_64-linux-gnu/libgcc_s.so.1
2ba3cba29000-2ba3cba2a000 r--p 00015000 fd:00 2840941                    /lib/x86_64-linux-gnu/libgcc_s.so.1
2ba3cba2a000-2ba3cba2b000 rw-p 00016000 fd:00 2840941                    /lib/x86_64-linux-gnu/libgcc_s.so.1
2ba3cba2b000-2ba3cba43000 r-xp 00000000 fd:00 2840960                    /lib/x86_64-linux-gnu/libpthread-2.24.so
2ba3cba43000-2ba3cbc42000 ---p 00018000 fd:00 2840960                    /lib/x86_64-linux-gnu/libpthread-2.24.so
2ba3cbc42000-2ba3cbc43000 r--p 00017000 fd:00 2840960                    /lib/x86_64-linux-gnu/libpthread-2.24.so
2ba3cbc43000-2ba3cbc44000 rw-p 00018000 fd:00 2840960                    /lib/x86_64-linux-gnu/libpthread-2.24.so
2ba3cbc44000-2ba3cbc48000 rw-p 00000000 00:00 0 
2ba3cbc48000-2ba3cbddd000 r-xp 00000000 fd:00 2841097                    /lib/x86_64-linux-gnu/libc-2.24.so
2ba3cbddd000-2ba3cbfdc000 ---p 00195000 fd:00 2841097                    /lib/x86_64-linux-gnu/libc-2.24.so
2ba3cbfdc000-2ba3cbfe0000 r--p 00194000 fd:00 2841097                    /lib/x86_64-linux-gnu/libc-2.24.so
2ba3cbfe0000-2ba3cbfe2000 rw-p 00198000 fd:00 2841097                    /lib/x86_64-linux-gnu/libc-2.24.so
2ba3cbfe2000-2ba3cbfe6000 rw-p 00000000 00:00 0 
2ba3cc000000-2ba3cc021000 rw-p 00000000 00:00 0 
2ba3cc021000-2ba3d0000000 ---p 00000000 00:00 0 
557ab9b48000-557ab9b4a000 r-xp 00000000 fd:00 23353346                   /home/g1NiFf/prog
557ab9d49000-557ab9d4a000 r--p 00001000 fd:00 23353346                   /home/g1NiFf/prog
557ab9d4a000-557ab9d4b000 rw-p 00002000 fd:00 23353346                   /home/g1NiFf/prog
557abbab9000-557abbaeb000 rw-p 00000000 00:00 0                          [heap]
7fff4aec3000-7fff4aee4000 rw-p 00000000 00:00 0                          [stack]
7fff4afee000-7fff4aff0000 r-xp 00000000 00:00 0                          [vdso]
7fff4aff0000-7fff4aff2000 r--p 00000000 00:00 0                          [vvar]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]