2011年11月11日 星期五

[VC] Use VLD for memory leak detection

Allocating and releasing memory is a big headache for many Visual C++ programmer.
Usually, the memory is allocated and forget to be released.
It will make the program unstable, can't run properly, or occupy more and more system resources, the worst case is make the system can't response anymore.
There are many tools helping programmer to detect Memory Leak, such as Rational Purify, BoundersChecker...etc, but they are not cheap.

Here is the introduction of a free Memory Leak detection tool: VLD (VisualLeak Detector).

I would rather take VLD as a library for programmer than a tool.
For programmers, it's easy to add memory leak detection to source code instead of understanding complicated operation of Purify or BounderChecker.
For developing a  medium or small program, I think it's quite enough. :-)

The following are the description of installing and using VLD.
VLD Version:  2.2
Official Website:  http://vld.codeplex.com/

Download link:  http://vld.codeplex.com/releases/view/70398

  • Click setup.exe of VLD to start installation. Here is the installation of Version 2.2


  • The default location of VLD will be installed to Program Files.

  • When installing, VLD will ask you to add the location of VLD dll files to your "Path" environment variable. Click Yes to continue.

  • Installation finished. It's very easy to install. :-)

  • Then, open your VC project. Add VLD include path in VC project property.

  • Add VLD library path to VC project property. I added Win32 since my OS is 32 Bit. If your OS is 64 Bit, please change VLD library path to Win64.

  • Include vld.h in the cpp file which wants to be detected for memory leak. We can include vld.h in Main dialog or App if we want to check whole program.


  • OK, let's test VLD.
    Allocate a memory and don't free it in purpose, run Debug to see how VLD report memory leak.

  • When the program ended, VLD reported that memory leak was detected in Output window. Double click at the line with line number of source code will jump to the location. It's very convenient.

  • The data in the memory not freed listed in Output window in detail. It's also helpful for debugging.

Afterword: VLD is really helpful for VC programmer. But I had the experience that my program can't be launched when using VLD. And then I changed to open VLD only when I want to detect memory leak. VLD is closed when memory leak detection is finished.

Reference:
Memory Leak Detection in C++
Memory leak tool for C++ under Windows

[VC] 使用VLD偵測Memory Leak

記憶體的宣告與釋放是許多Visual C++開發人員相當頭痛的部分
常常不小心宣告了記憶體忘記釋放
造成程式不穩定, 程式無法正確執行
或是系統資源越吃越多, 狀況嚴重時甚至整個系統會卡住!!
市面上有許多幫助開發人員偵測Memory Leak的工具
例如Rational Purify, BoundersChecker...等, 但是價格都不斐
這邊要介紹的是一個免費Memory Leak偵測工具VLD (VisualLeak Detector)
它與其說是個工具、反倒比較像是個給開發人員用的Library
對程式開發人員來說, 不用搞懂複雜的Purify或BoundersChecker操作
很容易便可以將記憶體偵測的功能加入程式碼中
以中小型程式開發而言, 用VLD已經綽綽有餘囉. :-)

以下是VLD的安裝與使用說明

VLD版本:  2.2版
官方網站:  http://vld.codeplex.com/

下載網址:  http://vld.codeplex.com/releases/view/70398

  • 點選VLD的Setup開始安裝, 這邊安裝的是2.2版


  • VLD預設會安裝到Program Files下面

  • 安裝中, VLD安裝包會詢問您是否將vld的dll路徑加入系統環境變數, 點選Yes讓它加入

  • 裝好了, 安裝很容易. :-)

  • 再來開啟您的專案, 在VC的專案屬性中加入VLD include的路徑

  • 加入library inlcude的路徑, 因為我的作業系統是32位元, 所以加入Win32的library路徑. 如果您的作業系統是64位元, 請將Library路徑改成Win64

  • 在想要偵測Memory Leak的cpp檔include vld.h。如果想偵測整支程式的話, 可以在主視窗或是APP的cpp檔中include vld.h


  • OK, 來測試一下。
    故意在OnInitDialog()裡面宣告一塊記憶體而不要釋放, 執行Debug看看VLD會怎麼回報Memory Leak

  • 執行完程式之後, VLD會在Output視窗輸出訊息, 說它發現Memory Leak了。在回報程式碼行數的那列點兩下, 會自動跳到記憶體宣告的地方, 相當方便喔。

  • Output視窗中還會列出沒有被釋放的記憶體裡面的詳細資料, 對於Debug也相當有幫助

後記: VLD對VC開發人員來說的確很有幫助, 但筆者使用VLD時, 曾發生程式無法叫起來的現象, 後來改成只有在要檢測Memory Leak時才include vld.h, 檢查完Memory Leak之後就關掉VLD了。

參考資料:
Memory Leak Detection in C++
Memory leak tool for C++ under Windows

Create table by MySql Workbench - The meaning of abbreviation for column properties

The following abbreviations are the column properties when creating column by MySQL Workbench:
  • PK: Belongs to primary key
  • NN: Not null
  • UQ: Unique index
  • BIN: Is binary column
  • UN: Unsigned data type
  • ZF: Fill up values for that column with 0's if it is numeric.
  • AI: Auto Incremental.
 

[MFC] Set default focus of control item in dialog

The story is like this, we had some small requirement with our program.
Default focus of control item in our program was asked to change to some edit box for data input instead of setting on OK button as default, so that users don't need to change the focus by moving cursor or clicking Tab button.

And it can avoid an extra Enter button clicking to run OK when our program is launched.
But this request wasn't altered in next version.
I discussed with our engineer, and he said SetFocus() method of CWnd can work properly.
Then  I found something tricky in OnInitDialog() function when I looking into this issue.
It's just the key point to resolve this problem.
Share with you here.

* Set the focus of control item in OnInitDialog(), but it doesn't work.












* The key point is here,
OnInitDialog() must return FALSE so that the focus of control item can be set properly.




[MFC] 設定對話窗中預設的控制項焦點

事情是這樣的,我們的程式有個小修正
請工程師將程式預設的控制項改到輸入資料的Edit Box上
控制項焦點不要放在預設的OK按鈕
如此一來使用者便不用再用滑鼠點Edit Box或用Tab鍵更改控制焦點
也避免程式開起之後Enter多按一次馬上按到OK鍵
但是又過了一個版本依然沒有修正這問題
工程師反應CWnd的SetFocus()函式無法正確地將控制焦點設定好
後來去幫忙看問題時發現OnInitDialog()裡面有個小小的"眉角"
正是這個問題的關鍵所在!
與大家分享~

* 在OnInitDialog()設定了控制項焦點, 但是無法動作












* 關鍵在這裡, 
OnInitDialog()必須回傳FALSE, 設定控制項焦點才能正常動作




2011年11月10日 星期四

Use DebugView for remote debugging

As many people know, DebugView is a very useful utility for debugging which can receive the string sent by Win32 OutputDeubgString or Kernel-mode DbgPrintf so that we can debug accordingly.

Besides debug in local machine, DebugView can debug remotely.
This article is to share the remote debugging feature.
DebugView can receive the message from remote computer.
When we have the issue which program under test makes operating system crashed or no response, we can use remote machine to debug.

Debug View Version: 4.76
Setup steps:
  • Open a command window, input "DbgView /c" to open DebugView as client mode. DebugView will popup message of waiting for connection at this moment.
  • Lanuch DebugView in another computer, and click Computer -> Connect.

  • Input the computer IP or computer name which the DebugView Client located, and then press OK to connect.

  • Both computers will display success message if the connect is established successfully.

使用DebugView做遠端除錯

大家都知道DebugView是一個相當好用的除錯工具
可以接收程式送出的訊息,進而加以偵錯

事實上DebugView除了在本機端偵錯
還有這邊介紹的遠端除錯的功能
DebugView可以接收遠端電腦所送出的訊息
如果遇到待測程式會造成電腦當機或卡住不動的時候
便可以利用這項遠端除錯的功能來進行偵錯 

設定步驟:
  • 在待測程式的電腦上開啟命令視窗, 輸入DbgView  /C的命令, 將DebugView開啟為Client模式, DebugView此時會顯示等待連結的訊息

  • 在另一部電腦上開啟DebugView, 點選Computer -> Connect命令
  • 輸入待測程式之電腦IP或電腦名稱

  • 如果可以連線, 則兩邊都會顯示連線成功的訊息
連線成功後, DebugView便可以接收到由待測電腦所發送出來的訊息囉!

DebugView版本:4.76
DebugView下載位址:

http://technet.microsoft.com/zh-tw/sysinternals/bb896647.aspx