Skip to main content

ERROR_SUCCESS

ERROR_SUCCESS. This macro would be familiar to all those who have done some programming in WIN32. It is the output of the GetLastError() function to check the thread's last error state when no error has occurred. Weird, isn't it? I mean, if it is a success, then why is it marked as an error in the macro?

This is one example of a badly made API. APIs are considered bad when programming in them becomes non-intuitive. Software is said to be bad (or said to suck) when it seems counter-intuitive to the user. There is one very simple example of this.

Start notepad. Type in any text. Click on close. The message that you see is:

Notepad dialogue: Do you want to save changes to Untitled?

This makes no sense to me as a user. Of course, the programmer follows the approach that he creates a temporary file called Untitled, and in that file he allows the user to make all his changes. But how am I, as a user to understand that?

A similar disconnect occurs even between two different programmers. That is why it takes a whole lot of effort to make a programme using a different API than what one is used to. But by far, the most difficult and non-intuitive API that I have used is the WIN32 API.

CreateThread() looks pretty intuitive as a function to create a thread. But there is a deep catch. If the programmer uses any of the C++ stdlib functions, then CreateThread shall lead to memory leaks. Now this sure is not clear from the function description. Similarly, to end a thread in a clean fashion, the programmer must make the thread return a value. If the thread is forcefully terminated by means of the TerminateThread() function, then the destructors for the objects within the thread are not called, which causes the thread to waste memory. Further, there is no automatic garbage collection, as there is with Java or C#. Perhaps this shall tell why it does not make sense to forcefully terminate any programme via the task manager. If the programme does not have a good garbage collection mechanism, then the programme shall eat up memory and waste space.

Another annoying thing I found was about the way any object must be created/used. In WIN32, there may be a large number of errors that may occur when any object is used. The error catching codes go larger than my basic code. So it happens that my code for a simple multi-threaded application would be around 50 lines, around 100 lines get added in handling errors. Well, what happens if you miss out any particular error? In that case, you are a bad programmer.

Till we meet again. Keep coding. A toast for all your errors which are actually successes.

Comments

  1. I find ERROR_SUCCESS quite funny :)

    Automatic garbage collection is a feature missing in C and C++. You can't blame WinAPI for this.

    There is no DEERP_CATCH when using stdlib functions. Quote from http://msdn.microsoft.com/en-us/library/windows/desktop/ms682453(v=vs.85).aspx

    "A thread in an executable that calls the C run-time library (CRT) should use the _beginthreadex and _endthreadex functions for thread management rather than CreateThread and ExitThread; this requires the use of the multithreaded version of the CRT. If a thread created using CreateThread calls the CRT, the CRT may terminate the process in low-memory conditions."

    As for TerminateThread() this function is indeed a last resort for forced thread killing. So when you're simply telling the kernel to wipe out the thread what do you expect?

    ReplyDelete
    Replies
    1. It's been quite a long time since I wrote a Win32 program, so I've forgotten lot of the caveats.

      I guess this post was written after a lot of frustration and heartache when writing a multithreaded C++ program. Check my post on why I like the new C++ (C++11).

      Delete

Post a Comment

Popular posts from this blog

On Harry Potter and why I dislike the series

There could not be a better time for this post. There could not have been a worse time for this post. Now that the penultimate movie of the series is out, and my facebook wall filled with people who loved the movie. But this is something I really wanted to say, and I shall say it anyway. Harry Potter is pathetic literature. Now, you must be wondering why I say that. There are many reasons. Firstly, the storyline itself is flawed. When a writer sits down to write anything, he/she must set up some essential rules about what is happening. These rules must remain constant irrespective of how many times he/she changes his/her mind. This is so that the readers are allowed to have some sensibility in what they are reading. In the fourth book, Rowling goes ahead and kills Cedric. Then, at the end of the book, the horseless carriages are there again. Nothing special. We all knew that they are horseless. But then comes the fifth book, and BAM, the horses are actually winged beasts that only thos

On the Dvorak Simplified Keyboard

This is a post that I have been meaning to write from quite some time. Long hours spent typing code on my computer left my hands fatigued, and left me with a lot of pain in my wrists and fingers. That is when I decided to use the Dvorak. But I have got the same bad habit as Dr. Watson, to tell a story backwards. Of course, you must be wondering what the Dvorak is. The story of keyboards starts with the invention of the typewriter. Christopher Sholes, the inventor of the typewriter, tried with a two row piano style keyboard. But then, he got into many difficulties with the design. Then he finally settled for a four row design. This was similar to the QWERTY layout that most computers and typewriters today possess. The engineers at Remington, to whom Sholes had presented his design modified the layout a little further, and then the QWERTY was born. As typewriters became popular, people got used to the layout, and started practising touch typing, i.e. typing without looking at the keys

The paradox of government

I'm fascinated by the concept of government, and the paradoxes it presents. On one hand, governments grant us a certain set of rights or liberties. On the other hand, they work to strip us of the very liberties they promise. Now, I don't mean that all governments strip people of liberties, but there are liberal regimes, and there are sufficiently restrictive and dictatorial ones. Both models may have results to show, it does not mean that people in a restrictive regime are unhappy (refer to Dan Dennett's TED talk , where he states that ideas or memes can be dangerous when taken from one part of the world, where they are widespread, and, using the virus analogy, where people are immune to the memes; to a part of the world where they are foreign, where people may not be immune to the memes and where people may get infected). History has shown that people were sufficiently satisfied with autocratic governments with a benevolent dictator, and that people in other parts of the