Saturday, September 06, 2008

Adding Icons to the Menus

This has been the worst struggle I've ever had to do with getting MFC to do what I want.

I had to add Icons to a Context menu and so far I have done it successfully for Vista. For XP its another ball-game altogether.

Tuesday, September 02, 2008

Of promising to give more than you have

For years that i have been programming i have taken pride in the fact that I check for all the newly allocated pointer against NULL and don't use it otherwise.

No imagine the following scenario

  1. You ask for a huge amount of memory and system returns you a pointer.
  2. You check it against NULL and are happy that its not NULL so you go ahead and try to use it.
  3. Out of the blue you get a Segmentation Fault error and boom... you app is dead ;(

Lets investigate as to what happened

  • There is a feature in Linux kernel called "Over Commit"
  • When you ask for memory it just returns you a pointer which actually is not yet allocated.
  • System assigns you pages as and when you use it (WTF)
  • So just in case when you decided it was time for you to use the memory you allocated and check the return value for NULL (just to be sure) the system might run out of all memory.

The result

Your app just "crashed and burned". Why? because although you checked for a NULL return from the allocating routines but you didn't use it and check for a "segmentation fault" following the access to the memory. a BIG WTF.

 

One solution I can think of is to follow this procedure

  1. Set up signal handler for "SIGSEGV"
  2. Allocate memory
  3. Set up a flag that you are going to use the newly allocated memory (the flag has to be thread specific of use a synchronization object for this thus making it thread safe)
  4. In the Signal handler for "SIGSEGV" see if signal was raised due to the memory access of the newly allocated memory.
  5. If yes set up another flag marking the memory bad and don't use it anywhere.
  6. Else use the memory and be merry.

Conclusion

  • A simple and efficient check against NULL now replaced with all the above crap.

My stand

I don't know who and why would think up this feature, let alone make it a system wide policy. if this would have been a process level settings wherein the developer would have to explicitly tell the compiler to enable "Over Commit" then i could have let that fancy dev do whatever he/she wanted, but making this default setting on every Linux box... WOW, no other words.

Yes i know there may be some advantages to this, but the risk far outweighs the advantages. Let the crazy ones make this choices of doing all the juggling for allocating valid memory, I just want the NULL back.

I found this on gnu.org

Memory overcommit is a Linux kernel feature that lets applications allocate more memory than is actually available. The idea behind this feature is that some applications allocate large amounts of memory "just in case", but never actually use it. Thus, memory overcommit allows you to run more applications than actually fit in your memory, provided the applications don't actually use the memory they've allocated. If they do, then the kernel terminates the application.

Wow, I got terminated because I asked for memory and then used the memory i got back. am I missing something here?