"Fortunate are those who take the first steps.” ― Paulo Coelho

Learning Part 1-Memory/Lock tips.

  1. DispatcherTimer issue:
  2. Memory leaked caused by event handler
  3. Lock usage(based on common misuses found in smart code) From MSDN:
  4. lock calls Enter at the beginning of the block and Exit at the end of the block.In general, avoid locking on a public type, or instances beyond your code’s control. The common constructs lock (this), lock (typeof (MyType)), and lock ("myLock") violate this guideline:
  • lock (this) is a problem if the instance can be accessed publicly. (A few places in smart still does this.)
  • lock (typeof (MyType)) is a problem if MyType is publicly accessible.
  • lock(myLock) is a problem since any other code in the process using the same string, will share the same lock.

Best practice is to define a private object to lock on, or a private static object variable to protect data common to all instances.

  1. Don’t use lock if only one thread is involved.

a)         Don’t lock on code run only on one UI thread. (use a Boolean flag to prevent re-entry if that is the intention)

  1. Know what you are try to lock, in other word, keep the lock scope as tight as possible.

a)         Lock a large code block (or a method that does a lot is a problem, even worse, if the method uses pushframe that waits for messages)

b)         Don’t try to use lock to synchronize action/method, use thread safe producer-consumer queue.(a live example is Dispatcher in the UI thread).

(Few learnings from my team, I’m not the original author.. credit goes to my fellow teammate. thkx)


July 11, 2011 - Posted by | Uncategorized

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: