Bug Vanquisher

28 February 2007

Another way of making your iterators safe!

Filed under: C++ — Tanveer Badar @ 12:01 PM

We all know what a problem a runaway pointer can be. Most of the time, things seem to work properly as memory in C/C++ is never garbage collected. As soon as you add memory pressure and that page is marked for some other process, hell unleashes itself. Memory access violation, buffer overruns, security problems, out-of-bound access to array members, dangling references to stale data. The list goes on
and on.

Most iterator implementations face these problems, because according to Stroustrup, an iterator is a pure abstraction and anything that acts like an iterator is an iterator. Pointers fit this pattern, so by transitivity of this principle, iterators also are problematic.

Here is how a typical vector iterator is implemented:

[Update: I had code here previously, but apparently, wordpress doesn’t like rich formatting and kept messing up the colorful syntax highlighting I was using. To make things readable again, I have moved the code to another place. Get the code here]

Now, No. 1 is the only choice when you want all the speed and don’t care about memory access violations. iterator and const_iterator for this case are simply typedefs for value_type* and const value_type*.

No. 2 is the style of nerds. It does not achieve a single goal about safety but adds tons of overhead to a simple pointer. Compiler may be smart enough to optimize out most of the dirt, but you cannot rely on that always. Therefore, don’t fall into the trap of writing a simple iterator class when it just wraps a pointer and does nothing else.

No. 3 is also an iterator class, but this time it has additional functionality that is added conditionally during compilation. also one additional pointer is added. This dialect of iterators is useful during all sorts of debugging and when you want safety over all concerns of performance. When  _SECURE_STL is not defined, this class degenerates into No. 2 with an additional member, a performance nightmare for nothing. Therefore, it is recommended
that you choose a variation of preprocessor directives that allows pointer declaration of iterators when it is not defined. Otherwise, you pay the costs for nothing.

However, when it is defined, the class is a wonderful piece of ingenuity. Every dereferencing and increment/decrement operation is checked to verify that it is still pointing at a valid position into the container. Also, this iterator will not suffer from dangling references problem, where an iterator is left to pointing at the old location after the container re-allocated its storage. The container is directly dereferenced when required. Through this extra layer of indirection, we avoid storing the pointer and access it through container itself.

However, this class is not without its costs. An additional member is defined, doubling storage requirements. Every member function executes additional code that checked for proper access and mostly lies in the critical path. Also, if the containers [ ] operator depends on its iterator implementation, infinite recursion occurs, when not all compilers may point-out at compile time.

25 February 2007

A day (mostly) wasted!

Filed under: Personal — Tanveer Badar @ 1:02 PM

Yesterday, I decided to have a visit to my university. First one since I graduated from there. I planned to meet some of my teachers to discuss life as a professional, have their views about how to go about your job, higher studies, applying for a faculty position at the university and so on. Also, there was the matter of looming convocation, ever getting closer and tons of official documents I had to acquire and some clearance fees to pay.

What I expected was a warm response from them, pleased to see that one of their students still remembered them. However, I couldn’t have been more wrong on this account. They turned a cold shoulder, not at all the kind of response I was expecting.

One teacher was, with whom I am on very good terms, one of the best students his career and having decided a time to meet had another student from 1999 batch and kept asking me if there was something else I wanted to discuss and kept hinting if I didn’t mind I should rather leave right away. Besides, this whole thing happened after he had asked me if I could come 30 minutes later.

Another one had much more important things on his mind, so I had to come up with a stupid reason for why I was there. Even there, I was one of the twelve students who choose his subject as an elective.

I didn’t dare risk my ego by trying to visit other teachers and found some friends who were there and went to a fast-food resturant to evaporate my feeling of let-down and insult.

22 February 2007

News Box

Filed under: Personal — Tanveer Badar @ 7:18 AM

So, it is true that Starget SG-1 is back on TV Land. A whole new season! Wow! Watched “Enemy Mine” last night.

The new posts I promised some time earlier are yet to come as I am feeling a bit too much lazy myself. This post itself is getting longer and longer, cutting into my laziness and making me do something. Therefore, see you latter :).

14 February 2007

More coming up in a couple of days

Filed under: Personal — Tanveer Badar @ 8:42 PM

I am really busy these days. Due to the incorrect assignment of workload, I am either waiting for something to do, or doing overtime in the office. Hopefully, this situation will resolve in a couple of days as we near product release date before I, myself, am finished.

Now, to the real topic of this post. Many things are cooking up in my mind during those free periods and I will share most of these with the readers. Things like how to write secure iterators, the blogs I frequently read (a mighty >86 item list) which you will find very informative, a deep drive into the Visual system and milcore.dll of WPF, the new APIs of user32.dll concerning off-screen surfaces used by DWM. that sort of stuff.

Don’t blame me if your head has already started aching.

It’s a must have!

Filed under: Tips — Tanveer Badar @ 8:29 PM

It’s a must have!

To die for!

I have been waiting all my life for someone like you!

What more can I say for such a wonderful product, go and see yourself. An assortment of visual tools, one of which allows you to edit .proj, .csproj and .vbproj files in a visual editor. Or you can directly go to the download.

7 February 2007

Make your life a bit easier around COM

Filed under: Tips — Tanveer Badar @ 11:33 AM

Just read a really useful debugging tip!

random debugger tip- catching COM exceptions

The problem with Thumbnails in Windows Vista

Filed under: Windows Vista — Tanveer Badar @ 10:08 AM

[Update: 7th April 2007. When viewing blog statistics, I noted that this particular post was getting many hits in relation to some problem with thumbnail images in Windows Vista. If there is some problem with them, let me know. This article is about Thumbnail API provided by DWM.] 

If you remember my past post of “Let’s mourn Windows Vista”, in the last paragraph I wrote about thumbnail freezing to the last image when a window is minimized. I got a link to really nice blog of a DWM guy (Greg Schechter), very informative about all the how’s and why’s of DWM.

Now, I have an official response to that problem and, ahem…, it is has as usual “by design” explanation! No offense to Greg.

The message I wrote yesterday follows:


“I have read your blog back-to-front over the last couple of days. You seemed quite involved with DWM so I thought I should bring this issue into your notice.
The thumbnails which are displayed when mouse pauses over a tab in taskbar are dynamic while the window is not minimized. As soon as the window is minimized, the last image is displayed even if the window itself is updated many times a second.
A very good example is the Performance tab in Task Manager. When it is not minimized, processor usage and memory consumption graphs advance in the thumbnail in sync with the actual window. But when you minimize it, they are frozen to the last image they would have displayed.
The version I am using in Windows Vista Enterprise Edition. These details may help further restricting the scope. My system is dual core Pentium D, 2.8 GHz, 1GB DDR2 RAM and D945GZIS motherboard (AERO capable).
Time won’t permit it to be fixed as I only realised it two days ago and Windows Vista has RTMed already. I haven’t check the thumbnail displayed with ALT+TAB combination but as you said they are same thing at different locations, I expect them to exhibit same behavior.”


And the expected reply:


Yes, you’re correct, and that’s By Design. This is because applications, when minimized, actually do have the region they paint to minimized as well and receive Window messages telling them that they’re minimized and they stop drawing. The DWM retains the last good bitmap, and displays this.


6 February 2007

What’s wrong with my System Protection?

Filed under: Windows Vista — Tanveer Badar @ 12:41 PM

The word is: “You are multibooting Windows Vista with a previous version of Windows and unable to restore Windows Vista to some previous restore point.”

The problem:

  • All restore points are deleted from the disk.
  • All previous versions of files are deleted from the disk.
  • All but the most recent CompletePC Backup are deleted from the disk. The remaining backup made by CompletePC Backup is full and complete and can be used to restore your entire computer.

The reason:

Windows XP automounts every disk it detects, including external or removable hard disks. As part of the automounting process, NTFS writes to the disk, and these writes are detected by the volsnap.sys driver in Windows XP. Because this version of volsnap.sys does not recognize the persistent shadow copies (also known as restore points) made by the volsnap.sys driver in Windows Vista, Windows XP cannot maintain the integrity of the shadow copy storage area and deletes the shadow copies to avoid corrupting them. Note that dual-booting Windows Vista with Windows Server 2003 or Windows XP Professional x64 Edition will also result in the shadow copies being deleted.

The only workaround:

To avoid losing Windows Vista shadow copies on a disk that you don’t need to access from Windows XP, disconnect or power off the disk prior to booting to Windows XP.

For complete details, follow the link: How restore points and other recovery features in Windows Vista are affected when you dual-boot

1 February 2007

Update to my mourning

Filed under: Windows Vista — Tanveer Badar @ 3:28 PM

I managed to find an explanation to the behavior I was seeing in internet explorer while loading a page. The problem lies entirely with internet explorer and has nothing to do with DWM.

Read further in: http://blogs.msdn.com/kamvedbrat/archive/2007/01/26/back-on-channel-9.aspx

Pay special attention to paragraphs eight and nine. According to that post, the application should redraw with the default, which in this case is the AppWorkspace. However, that drawing isn’t happening smoothly.

Blog at WordPress.com.