Archive

Archive for the ‘Native Code’ Category

Về Memory leak trong MFC/C++ trên Windows

<Bài này viết lâu rồi, trên Live Spaces, nay move qua đây>

Những ai lập trình C có kinh nghiệm chắc hẳn đều thừa nhận rằng việc C/C++ giao hẳn trách nhiệm quản lí bộ nhớ cho người lập trình thực sự là một ưu điểm mạnh mẽ, và cũng chính là khuyết điểm lớn của ngôn ngữ này. Với tư tưởng đó, lập trình viên C++ luôn phải làm một công việc khó chịu và mất nhiều thời gian là phát hiện và loại trừ memory leak trong chương trình. Entry này không trình bày cặn kẽ và chi tiết về memory leak, mà chỉ là ghi lại một số kinh nghiệm của người viết. Hi vọng bài viết sẽ có ích với những người (và chỉ những người) đã có kinh nghiệm làm việc với C++, đã có những hiểu biết cơ bản về memory leak như: tại sao lại có memory leak, tại sao phải tránh memory leak v.v…
Read more…

Differences between Control.Invalidate(), Control.Refresh and Control.Update

(Source: http://blogs.msdn.com/subhagpo/archive/2005/02/22/378098.aspx)

Before discussing each one of the above functions, let’s look at how winforms controls paint.

Windows controls paint is response to WM_PAINT messages. This message is sent when UpdateWindow or RedrawWindow is called, or by the DispatchMessage function when the application gets a WM_PAINT through the message queue. On getting the WM_PAINT message, the control paints its background and then the foreground if necessary. Double-buffering and transparency is honored while painting and then the OnPaint event is fired to give the user a chance to perform his custom painting.

With this background, let’s look at the above mentioned three functions in more detail,

Control.Invalidate( ) / Control.Invalidate(bool) / Control.Invalidate(Rectangle) / Control.Invalidate(Rectangle, bool) / Control.Invalidate(Region) / Control.Invalidate(Region, bool)

The bool parameter denotes whether the user wants to invalidate the child controls of the control on which he is calling Invalidate. The Rectangle parameter are the bounds to invalidate and the region parameter is the region to invalidate. All the overloads essentially end up calling one of the RedrawWindow, InvaliateRect or InvalidateRgn functions. If RedrawWindow is called then this may result in a WM_PAINT message being posted to the application message queue (to invalidate the child controls).

The important thing to note here is that these functions only “invalidate” or “dirty” the client area by adding it to the current update region of the window of the control. This invalidated region, along with all other areas in the update region, is marked for painting when the next WM_PAINT message is received. As a result you may not see your control refreshing (and showing the invalidation) immediately (or synchronously).

Control.Update()

Update function calls the UpdateWindow function which updates the client area of the control by sending WM_PAINT message to the window (of the control) if the window’s update region is not empty. This function sends a WM_PAINT directly to WNDPROC() bypassing the application message queue.

Thus, if the window update region is previously “invalidated” then calling “update” would immediately “update” (and cause repaint) the invalidation.

Control.Refresh()

By now, you might have guessed what Refresh( ) would be doing. Yes, it calls Invalidate(true) to invalidate the control and its children and then calls Update( ) to force paint the control so that the invalidation is synchronous.

Windows Touch in C++ fashion

A great article and presentation on how to multitouch in C++. It’s really worth watching and trying…

Ideas

I am currently having some fancy ideas about various topics that I can dive in. The bad news is I have too little time for them right now, so although I intended to start very soon, but I’m not sure when I can really dig into these messes.

These ideas are very straightforward:

1. Security in Cloud computing, data mining/querying on encrypted databases and practical facts.

This idea came to me in the RIVF conference last month. The general consideration is how can we trust the Cloud service provider (i.e. Windows Azure, Amazon Web Service…) that they will not steal our data, how much our data is secured? When we performing queries, how can we know if the provider is faithful enough?

One of possible solutions is encrypting the data before sending it to the provider. The problem hence raise, since how could we do data mining, query “our” encrypted data which are stored on provider’s infrastructure?

This is really a hard (and interesting!) problem, and it draws many attentions from academic scholars. It is pretty useful to apply the academic idea on commercial service like Windows Azure or AWS…

2. The second idea comes from my experiences on GPU computing with CUDA.

DirectCompute has been announced in PDC 2009, and it seems promising in cross-platform GPU computing. Nonetheless, the documentation of DirectCompute seems to be lacking (even on MSDN DirectX). I want to dig into DirectCompute, perform some benchmarks to check it performance, and then comparing with CUDA or OpenCL. I believe this will point out some interesting conclusions.

So if one of the readers of this blog would like to investigate to these problems, please contact me. I’m not sure this will lead to anywhere, but at least it can help you improving some skills 😉

Undefined behavior in C and C++

19/08/2010 3 comments

http://blog.regehr.org/archives/213

Another great article diving into the mess of standard C/C++ and describing the basic concepts which are often ignored by programmers.

Nonetheless, I have just read part 1. Have no time for this right now. Place it here as a bookmark, read later…

RTF to HTML converter in C++

14/09/2009 1 comment

Download source code (9KB)

Nearly, I used RichEditCtrl in a MFC project, and I want to take formatted RTF text in that control to display on a HTML control. I searched through CodeGuru and found this one. It’s a nice job, but it leaks 2 things:

  • Doesn’t work well with unicode. In fact, it can’t recognize unicode characters.
  • A minor error when calculating font size.

As you maybe known, in RTF format, an unicode character is encoded in two ways:

  • \u6884: 6884 is character code (in decimal) of this letter (it’s “Ấ“) .
  • \’1EA4: 1EA4 is character code (in hexadecimal) of this letter (it’s “Ấ“) .

So I slightly modified Daniel Beutler’s code in order to work around these problems. You can found my code in the following functions: CRTF_HTMLConverter::R2H_CreateHTMLElements and Util::StringToLong.

Serialize and deserialize BITMAP object in MFC/Win32

09/07/2009 5 comments

Download the source code for various platforms: WinCE/WinMobile, Win32 API

Update Feb 10, 2011: The source code I uploaded is for WinMobile platform. Today I have modified and upload the Win32 version. All the source code inside this post is also changed to Win32 version.

Lately, in one of my project, I have to save an BITMAP object into an XML document. The problem is I can only save an CString into the XML document. So I have to find a way to convert BITMAP data (in a HBITMAP) into CString.

It’s pretty easy if I serialize the BITMAP to an byte[] array, and then convert the result byte[] array into CString format. So I will consequently discuss on these 2 problems.

Read more…