Sunday, April 28, 2013

Chasing stack overflows

Recently, one of my programs crashed with a segmentation fault error.

For the user of a program, bugs and crashes are bad.  For a programmer, bugs and crashes are ok. Almost everything I learn is via bugs and crashes.  If I knew a better way to implement something, I probably would have done it that way in the first place.  Watching your program crash and burn or compute at a snail's pace is great motivation to write better code.  And making a code change that fixes your buggy program is a powerful way to learn.  One might call this Crash driven development.

Here are a few things I learned:
1. Get comfortable using debuggers and examining core files.  It took me a bit of time and mental energy to set up the debugger that by the time I wanted to analyze things, I was sort of already mentally checked out.  I took a look at the stack, I dumped some local variables and I kind of gave up.  Using debuggers should be as easy as opening text files and reading what is going on.  

It turns out the reason for the crash could have been found if I spent a little more time with the debugger.  However, I gave up too soon.  The reason I gave up was because I don't have much experience working with debuggers.  I figured I'd be better off just reading my code, running tests and going from there.  Unfortunately, the crash wasn't really reproducible on my development machine, so using the debugger with the crash environment saved was the way to go.

2. Prove the bug, before implementing a fix.  After some time hopelessly trying to reproduce the crash a coworker told me that the machine where my program crashed was set up with a relatively small default stack size*.  He suggested that I raise the stack size.  This led me to a whirlwind of reading and code fixes, and tests that led me to conclusions like, if I overflow my stack, my program will crash.  While that statement is true, it didn't really prove that the cause of my crash was a stack overflow.  I should have spent more time definitively proving the suspected cause of my bug was indeed the cause or not.

3. Ask for help.  One colleague suggested that I had a stack size issue, which wasn't the case. Another suggested that I refactor my code a bit to make it a little more clear what was happening at the time of the crash.  This second discussion led me to re-evaluating a different section of my code and somehow seeing the problem.  Even though the second colleague didn't see the bug, just discussing the issues with him led me to solving the issue.  I have no idea how the brain actually works, but somehow just explaining my bug to another person who just asks questions is enough to re-wire my brain to see previously before hidden problems.

Just as a final note, the cause of my bug was an off-by-one error.  I was reading from a dynamically generated set of data records that could have variable number of fields and records.  I was using the wrong bounds, and sometimes (only on one type of machine and sporadically on some queries) the program would crash.  The fix turned out to be just a one line code change.

*Whenever I face a problem I'm not familiar with (in this case, stack sizes and stack overflows) I  typically turn to Google to help me research stack overflows.  How can I reproduce one, what signs are there that I might be doing something bad, are there messages that will help me identify what is going on, etc ... Unfortunately, searching the internet for information on stack overflows has been rendered virtually impossible by the ubiquitous programming Q&A site, stackoverflow.

Monday, April 22, 2013

Fargo - here I come

I'm a fan / reader of Dave Winer, and I like outlining.  Therefore, I wanted to give fargo a try.

Here are my notes on my first impressions.  By the way, I am using Chrome browser on a Macbook Air.

Connect to dropbox
First, I had to allow fargo to use my dropbox account.  After I gave permission, it looks like a new folder was created in my dropbox folder -- Dropbox/Apps/Fargo.  If you create a post, it is saved in this folder as an opml document.  After I made some changes with fargo, I see that the file in my dropbox folder on my local machine has updated.  That was simple and easy.

Offline access
As a test, I turned off my internet connection.  The fargo application in my browser gave me this
error:
Dropbox save error
Can't save to Dropbox because a network error occurred.
I ignored the error and made some edits to the document in the browser.  As expected, the changes did not appear on my local version of the file.  After turning my internet on again, fargo uploaded the changes to dropbox and updated the local version of the file.

I'm not sure how I am supposed to edit a fargo document when I am offline.  I edited the text with a text editor, and that works as expected.  But it would be nice if I knew how to use the browser application to make updates to the local version of my file.

Using fargo
You can begin using fargo right out of the box, but I think it will take me a little bit of time before using it will be second nature.  Here are some initial observations about the usability:
  • There are two modes.  Structure mode and text mode.  This reminds me of the vi text editor which has the command and input mode.  It would be cool if when I am in structure mode, I could navigate with the standard vi shortcuts.  For example, I could press w to go to the next word, 0 to go to the start of the line and so forth.  
  • Out of habit, I keep pressing <command> s, to save the document.  This causes the browser to prompt to save the document, which is not what I want at all.  It would be nice if that shortcut could trigger a sync with dropbox, but I'll probably just have to get out of the habit of pressing <command> s. 
  • Pressing enter causes the outliner to make a new item in the outline.  This makes sense if I am at the end of the line.  However, if I'm in the middle of the line, I'd rather want a new item in the outline that used the text from the middle of the line to the end of the line that I was on.  
  • There's a cribsheet which gives some shortcuts.  Press command + shift + ?.  It would be cool if I could leave the cribsheet open at the bottom of the page.  Also, it would be cool if I can add some more keyboard shortcuts.
Overall impression
I think it's a pretty cool app that I plan on using more and more. 

Paying a visit to the doctor

About two and a half years ago, my mom was diagnosed with Cancer.  After two surgeries and chemotherapy, the Cancer was brought down to undetectable levels.  Recently, a PET-scan and biopsies have revealed that it is back.

Today, I took my mom to Memorial Sloan Kettering to see another oncologist to get a second opinion on the diagnosis and future treatment plan.  All of the people we interacted with were very professional, courteous (friendly even), and competent.  My only complaint is that we met with the doctor about 2 hours after our scheduled appointment time.  As Seinfeld would said,
"See, you know how to take the reservation, you just don’t know how to *hold* the reservation and that’s really the most important part of the reservation, the holding. Anybody can just take them."

We did eventually meet with the doctor, so things weren't so bad, but I couldn't help but wonder, why was the doctor so late.  Considering that each person we spoke to, from the person at reception to the nurses and doctor, seemed really good at their jobs, it seems that there is something systematically built into the medical industry where people will always be waiting for doctors.  The same can be said for cable companies who ask their customers to be home anytime from 9 am to 4 pm.  I guess the thing in common is that almost every one needs both a doctor and cable t.v.

The problem is that no one cares that my appointment ran late.  I don't think doctors charge by the hour (except for psychiatrists).  If they did, maybe insurance companies would try to audit doctors and force them to move things along.  Also, I don't think punctuality is a must have trait for being a top rated doctor.  For oncologists, I would hope survival rates for patients is the number one thing.

Anyway, for the sake of posterity, I wanted to note the timeline:

  • 10:45 am - Entered the medical building ( got our parking validated ).
  • 10:50 am - Found the registration office.  We were given a form with our information already filled out that we had to verify.  Nice, they seem to know who we are.
  • 11:00 am - Met with someone in registration who confirmed our insurance info, privacy laws and other miscellaneous items.
  • 11:10 am - Went to another floor of the medical building, filled out some papers, and began the wait.
  • 11:30 am - Called in to see the nurse to do some measurements like height, weight, blood pressure.
  • 12:00 pm - This was our scheduled appointment time to see the doctor.  At this point, we've finished all of the preliminary work a while ago, so we expect everything to run smoothly.
  • 1:40 pm - Called in to go to the exam room.
  • 2:00 pm - Doctor walked into the exam room.  (two hours after our scheduled appointment).
  • 2:30 pm - At the end of the visit, we scheduled a follow up visit to see a surgeon.  This time we booked it for 8 am.  We were told, the earlier the visit, the less likely that it will run late.  
This day reminded me of my own work, where I estimate (often poorly) how long a project will take to complete.  


Thursday, April 18, 2013

Steps to make your own passport or visa picture

If you need to renew your passport or get a visa, then you will need a picture.
You can easily pay to get this picture taken, or you can pretty easily make one on your own.

Here are the steps I took:
  1. I had my picture taken with a digital camera.  
  2. Connected the camera to my Mac.
  3. Used iPhoto to import the image off of the camera and then to export the image to a jpg file.
  4. Used http://www.idphoto4you.com/?Target=DownloadPage to create four 2 inch x 2 inch images placed in a 4x6 inch area.
  5. Verified that the image looks ok and follows all the rules set in - http://travel.state.gov/_res/flash/cropper/FIG_cropper.html#.
  6. Downloaded the image.
  7. Uploaded the image to Walgreens photo site.  
  8. The final cost was less than 40 cents.

Wednesday, April 10, 2013

My first stackoverflow contribution

Stackoverflow is currently the go to Q&A website about programming.  Many if not all of my Google searches about technical things bring me to questions posed and answered on said site.

I have long been a reader of the site, but today, I have finally contributed something.  I added an alternative answer to an already answered question.  For my effort, I received
  • one 'up-vote',
  • my reputation was raised from 1 to 11,
  • and I got my first badge, "Teacher".  
That is a lot of positive feedback for a minimal amount of effort.

Maybe, it's just me, but the positive feedback feels great, and it certainly makes me want to browse the site and answer something else.  But as much as I like helping others and getting positive feedback,  I'm guessing I'll probably remain primarily a stackoverflow reader rather than a contributor.

Saturday, April 6, 2013

Hello World with MacBook Air in C++

I like having a bare-bones setup to write code.  All I need is a dream and:
  • a text editor ( vi, vim, gvim ) 
  • a compiler ( g++ )
  • a terminal
Given that Mac OS X is backed by Unix, I thought it would be simple to get up and running.  As always, it took me longer than expected.  Here's what I did.

Instead of gvim, I installed macvim.  As suggested I moved the MacVim icon to the applications folder.  I also made a copy of the mvim script to /usr/local/bin folder.
  sudo cp ~/Downloads/MacVim-snapshot-66/mvim /usr/local/bin

Now I can open macvim from any terminal directory with commands like:
  mvim
  mvim main.cpp

In order to get the compiler working I installed xcode.  You can write, compile and run code all from xcode, but the compiler isn't available on the command line.  To install the command line tools:
  open xcode -->
    open the xcode menu -->
      open preferences -->
        open the downloads and install the command line tools.

After it all installs, you should be able write a simple c++ file and compile it in the terminal with commands like:
  g++ main.cpp
  ./a.out

Wednesday, April 3, 2013

Day Two with MacBook Air

So, today is my second day with the MacBook Air.

One of the driving forces I had for switching from Windows to Ubuntu in the past and now from Ubuntu to Mac OS X  is to learn new things.  But of course, on my first days of using a new computer, I need to do some sanity checks and make sure that I have the same functionality that I had before.

One of my biggest difficulties with my old computer was that it was completely incapable of running an Android emulator.  To do so, rendered my computer pretty useless.  So, I wanted to see how this new computer stacked up.  To test, I wanted to run one of old Android projects with an emulator.  These are steps needed to get started:
  • Downloaded and installed Eclipse
  • Downloaded Java support
  • Installed the Eclipse Android Developer Tools (ADT) plugin
  • Downloaded the Android SDK and told Eclipse where to find it
  • Downloaded an emulator (virtual device)
  • Used Git Hub for Mac to pull my old code from my repository
  • Downloaded one library which I didn't include the git repository
  • Cleaned and built the project
And, after all that.  I got my Android app to work with an emulator.  It worked much faster than on my old laptop - which is expected and good.  

So today, on day 2, I spent a bunch of time re-creating my old setup, struggled against my muscle memory as I fumbled with new keyboard shortcuts, confirmed that my new laptop is much faster than my old one, and started to use one new program, GitHub for Mac, which is pretty cool.

And as a final checkpoint for the day, I just checked and I'm now down to 91 GB of free space. This is down from 97GB from the time I first opened this laptop.  

First Impressions of Macbook Air

My Macbook Air arrived today.  These are my first impressions.

Let's get physical 

Before I get to installing software and running stuff, the computer's physical attributes deserve some attention.  This laptop is light and hard.  The edges of the computer are pretty rough, and I can imagine my wrists feeling sore from them over time.  Hopefully, I get used to the hard-ness, but I hope I never take its lightness for granted.

Onto the softer side

The first time I turned on the computer, I was asked to do some upgrades.  I complied, and after a restart, I checked the memory.  Starting from a 128 GB hard drive I had about 97 GB free to use.

I then installed some programs.  I was a bit confused as to the process.  I click on the downloaded file.  It sometimes opens a file on the desktop, sometimes it has a file ready for copying to another location, ... I'm sure I'll get used to it, but I sure do not grok it just yet.
  • Citrix - I need this to remote connect to work, which is Windows machine.  Login worked great and it seemed to work more seamlessly than when I logged in from Ubuntu.  I will need to do some additional checking with emphasis that I can do everything with a Mac keyboard as I am logged into a Windows machine.
  • Eclipse - I downloaded this.  I wasn't sure where it is best to install this.  I created a folder called Development in my home directory for now, and left the whole directory structure there.
  • Android SDK - I want to do some Android development, so I need this Software development kit.  I'll start up an emulator and test it out later.  For me, using an emulator was a pretty taxing operation on my old computer.  We'll see how the new computer fares.
  • Git - I can use this to retrieve my previous projects.
  • MacVim - I usually use gvim, so this will hopefully be very natural for me.  
  • Xcode - I've never used this, but I do want to see what it is.  
Everything installed pretty easily, so no complaints for now.  I'll try to do some things with these programs in the coming days.

Updates

  • After several months of usage, I'd recommend this computer to anyone.
  • I just checked online (July 2014), and I saw that this same Macbook (a refurbished mid-2012) is going for $779. I bought the same machine, also refurbished, for $999.