How to detect whether the connection to SQL Server is broken or not

  For managed code, use SqlConnection’s StateChange event:           static void Main(string[] args)         {             SqlConnection conn = new SqlConnection();             conn.StateChange += ConnStateChangeEventHandler;             conn.ConnectionString = "server=yourServerName; uid=yourUserName; pwd=xxxx; database=northwind";             conn.Open();             try             {                 SqlCommand cmd = new SqlCommand();                 cmd.Connection = conn;                 cmd.CommandText = "select * from…

0

C++ small gotcha – 3

Have two class as below:   struct MyBase {   __int64 x;   __int64 y;   MyBase()   {           memset(this, 0, sizeof(MyBase));   } };   struct MyDerived: public MyBase {   int z;   MyDerived():MyBase(), z(0)   {} };   Then create two objects as below, and then compare these twoobjects with memcmp, do you think the c compare will be equal since…

1

C++ small gotcha – 2

What’s wrong with the below code: // a wrapper class which holds memory that’s allocated // by using new [] template<class T> class ArrayWrapper { public:     ArrayWrapper(T* pData = NULL)     {         m_pData = pData;     }       ~ArrayWrapper()     {         delete [] m_pData;     } private:     T* m_pData; };…

1

C++ small gotcha

Suppose str1 and str2 points to the same zero terminated string, will str1 and str2 be the same value after running below while loop? while(*str1) { str1++; }; while(*str2++); Answer: Str1 points to the terminating zero of the string. But str2 points to the memory address after the zero. See the assembly of “while(*str2++);” will…

0

Design pattern in .NET: iterator

The iterator pattern plays very important role in STL. In STL, iterators are the glue between algorithms and containers. STL algorithms and containers are independent of each other; each STL container implements its own iterator and the STL algorithms use iterators to access the elements of the containers. For example, there is a for_each algorithm…

0

How to get high resolution timing info

We know that we can use GetTickCount to get timing info but the resolution of this API is limited to about 10 milliseconds, see MSDN: http://msdn.microsoft.com/en-us/library/ms724408(VS.85).aspx The resolution of the GetTickCount function is limited to the resolution of the system timer, which is typically in the range of 10 milliseconds to 16 milliseconds. To get…

0

C++ Tips: Adjustor thunk: what is it, why and how it works

When you debug C++ applications, you may notice below unusual functions generated by the compile. Have you even wondered what the adjustor thunk function is. Why it is needed, and how it works? 00ce10a4 8b06            mov     eax,dword ptr [esi]00ce10a6 8b10            mov     edx,dword ptr [eax]……00ce10ab 8bce            mov     ecx,esi00ce10ad ffd2            call    edx {test![thunk]:Derived::Func1`adjustor{4}’ (00ce10c0)} When virtual function…

1

C++ tips: Why the pop method of STL stack does not return a value

Or why they don’t combine the functionality of pop/top method into one function? We know that the pop method pops the top element of the stack, change the stack’s internal state but returns nothing; and the top method returns a reference to the top element on the stack, but does not change the stack’s internal…

0

C++ tips: why you should not leak exception from a destructor

One of the reasons: we know the destructor of an auto object can be called in two situations:  1. When the object is out of scope normally.  2. During the stack unwind when handling an exception. For situation 2, if the destructor throws another exceptions when it is handling the first exception, C++ will call…

0

Design pattern in .NET: template method

GoF’s classical design pattern book has discussed template method pattern: define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure. We see this pattern in .NET programming also: 1. The dispose pattern Any type that wraps…

0