1 #include <iostream.h>
2 class Singleton
3 {
4 public:
5 ~Singleton(){cout<<"singleton deconstruct"<<endl;}
6 static Singleton* Instance()
7 {
8 if (_instance == NULL)
9 {
10 _instance = new Singleton();
11 static Cleaner cl; //延遲到這里
12 }
13 return _instance;
14 }
15 void Print(char* str)
16 {
17 cout<<"singleton print:"<<str<<endl;
18 }
19 private:
20 Singleton(){cout<<"singleton construct"<<endl;}
21 static Singleton* _instance;
22 class Cleaner
23 {
24 public:
25 Cleaner(){cout<<"cleaner construct"<<endl;}
26 ~Cleaner()
27 {
28 cout<<"cleaner deconstruct"<<endl;
29 if(Singleton::Instance())
30 delete Singleton::Instance();
31 }
32 };
33 };
34 Singleton* Singleton::_instance = NULL;
35 int main(int argc, char* argv[])
36 {
37 Singleton::Instance()->Print("print 1");
38 Singleton::Instance()->Print("print 2");
39 return 0;
40 }
1 1 //子項
2 2 class autoItem
3 3 {
4 4 public:
5 5 //處理
6 6 virtual bool proces() = 0;
7 7 //彈出
8 8 virtual bool ok() = 0;
9 9 //處理順序
10 10 virtual int Order(){return 0;}
11 11 };
12 12 enum sortType
13 13 {
14 14 ST_Input,
15 15 ST_Custom
16 16 };
17 17 //自處理list
18 18 class _autolist
19 19 {
20 20 public:
21 21 _autolist( sortType _type );
22 22 ~_autolist();
23 23
24 24 //繼承函數
25 25 public:
26 26 //加入處理序列
27 27 virtual bool push(autoItem *p);
28 28 //執行處理序列
29 29 virtual bool go();
30 30 //清理所有
31 31 virtual bool clear();
32 32
33 33 //
34 34 protected:
35 35 std::list<autoItem*> itemList;
36 36 sortType _sortType;
37 37 };
38 38
39 39
40 40 _autolist::_autolist( sortType _type )
41 41 {
42 42 _sortType = _type;
43 43 }
44 44
45 45 _autolist::~_autolist()
46 46 {
47 47 clear();
48 48 }
49 49
50 50 bool _autolist::push( autoItem *p )
51 51 {
52 53 if (p == NULL) return false;
53 54 switch (_sortType)
54 55 {
55 56 case ST_Input:
56 57 {
57 58 itemList.insert(itemList.end(),p);
58 59 return true;
59 60 }
60 61 case ST_Custom:
61 62 {
62 63 if (itemList.size() == 0)
63 64 {
64 65 itemList.push_back(p);
65 66 return true;
66 67 }
67 68 static std::list<autoItem*>::iterator it;
68 69 it= itemList.begin();
69 70 for (; it != itemList.end();it++)
70 71 {
71 72 if ((*it)->Order() < p->Order())continue;
72 73 itemList.insert(it,p);
73 74 break;
74 75 }
75 76 return true;
76 77 }
77 78 default:
78 80 return false;
79 82 }
80 83 return false;
81 84 }
82 85
83 86 bool _autolist::go()
84 87 {
85 88 static std::list<autoItem*>::iterator it;
86 89 int nsize = itemList.size();
87 90 it= itemList.begin();
88 91 for (; it != itemList.end();)
89 92 {
90 93 (*it)->proces();
91 94 if ( (*it)->ok())
92 95 it = itemList.erase(it);
93 96 else
94 97 it++;
95 98 }
96 99 return true;
97 100 }
98 101
99 102 bool _autolist::clear()
100 103 {
101 104 itemList.clear();
102 105 return true;
103 106 }
104 107
105
v1(x,y,z) ->v1(x,y,-z) Z軸方向不同 DX: z正方向屏幕向里 OpengGL: z正方向屏幕向外
面:
face1(v1,v2,v3) -> face(v1,v3,v2) 正面相反 DX: 面正方向是順時針 OpengGL: 面正方向逆時針
紋理坐標
tz(u,v) -> tz(u,1.0f-z) (0,0)點不同 DX: (0,0)左上角 OpengGL: (0,0)左下角
矩陣:
m(m00,...m33) ->轉置() () DX: 左手坐標系,行向量 OpenGL: 右手坐標系,列向量
VC++ IDE 的默認狀態(VC6)是沒有啟用內存泄漏檢測機制的,也就是說即使某段代碼有內存泄漏,調試會話的 Output 窗口的 Debug 頁不會輸出有關內存泄漏信息。你必須設定以啟用內存泄漏檢測機制。
按下面的方法使用調試堆函數
在XXXView.cpp中添加下面粗體行
你再看看輸出結果,是不是有很多的內存泄漏?
#define _CRTDBG_MAP_ALLOC
#include<stdlib.h>
#include<crtdbg.h>
CXXXView::~CXXXView()
{
}
文章出處:DIY部落(http://www.diybl.com/course/3_program/c++/cppjs/2007925/73624.html)
你也許還沒用過的vc++的調試的功能
From: http://www.cnitblog.com/Raistlin/archive/2005/12/14/5380.html
剛剛在IT博客網閑逛的時候看到了孤獨的夜的一片文章《如何調試MFC中的內存泄漏》,講道用設置{,,msvcrtd.dll}_crtBreakAlloc這個變量來調試內存泄露的問題。
在How to use _crtBreakAlloc to debug a memory allocation你可以找到英文的更完整的版本,靜態鏈接和動態連接到C運行庫的名稱是不一樣的
靜態:_crtBreakAlloc
動態:{,,msvcr40d.dll}*__p__crtBreakAlloc()
{,,msvcrtd.dll}__p__crtBreakAlloc()是個什么東西呢?
查看msdn索引“Advanced Breakpoint”and you will find out...
語法如下:
{[function],[source],[exe] } location
{[function],[source],[exe] } variable_name
{[function],[source],[exe] } expression_r_r
這個是我轉的時候的地址:http://blog.sina.com.cn/s/blog_630d564a0100gq5k.html
http://blog.vckbase.com/smileonce/articles/1184.html(這個鏈接->關于Debug和Release之本質區別 )
今天公司有同事問我ASSERT與VERIFY宏有什么區別,雖然平時常用這兩個宏對一些變量在Debug模式下作判斷,但一時還真答不上來二者之間的具體差別,看來只是知其然不知其所以然。后來查了一些資料,總算弄清楚了二者之間的區別,整理后與同事交流了一番,感覺收獲還是蠻大的。下面對我的理解進行了總結:
In the debug version of MFC, the VERIFY macro evaluates its argument. If the result is 0,
the macro prints a diagnostic message and halts the program. If the condition is nonzero,
it does nothing.
In the release version of MFC, VERIFY evaluates the expression but does not print or interrupt the program. For example, if the expression is a function call, the call will be made.
斷言類型 | 定義 |
ANSI C 斷言 | void assert(int expression ); |
C Runtime Lib 斷言 | _ASSERT( booleanExpression ); _ASSERTE( booleanExpression ); |
MFC 斷言 | ASSERT( booleanExpression ); VERIFY( booleanExpression ); ASSERT_VALID( pObject ); ASSERT_KINDOF( classname, pobject ); |
ATL 斷言 | ATLASSERT( booleanExpression ); |
所有這些斷言都只在 Debug版中才被編譯,而在 Release 版中被忽略。唯一的例外是 VERIFY() 。事實上,這些宏都是調用了 assert() 函數,只不過附加了一些與庫有關的調試代碼。如果你在這些宏中加入了任何程序代碼,而不只是布爾表達式(例如賦值、能改變變量值的函數調用 等),那么 Release 版都不會執行這些操作,從而造成錯誤。初學者很容易犯這類錯誤,查找的方法也很簡單,因為這些宏都已在上面列出,只要利用 VC++ 的 Find in Files 功能在工程所有文件中找到用這些宏的地方再一一檢查即可。另外,有些高手可能還會加入 #ifdef _DEBUG 之類的條件編譯,也要注意一下。
順便值得一提的是 VERIFY() 宏,這個宏允許你將程序代碼放在布爾表達式里。這個宏通常用來檢查 Windows API 的返回值。有些人可能為這個原因而濫用 VERIFY() ,事實上這是危險的,因為 VERIFY() 違反了斷言的思想,不能使程序代碼和調試代碼完全分離,最終可能會帶來很多麻煩。因此,專家們建議盡量少用這個宏。