My blog has moved!

You should be automatically redirected . If not, visit
http://www.atriagrawal.com
and update your bookmarks.

About Me

My photo
I am currently pursuing my Masters at University of Southern California in Computer Science.

Sunday, November 20, 2011

Rescue from Segmentation Fault

Segmentation fault.

As soon as I see these golden words glittering on my screen when I execute my program, I can feel the nerve chill going down my spine. Its the last of the errors that I want my program to show. Three months back when I would see this error, I would give up and start from scratch. There was no way I was going to debug it. But in these three months I did quite a lot of debugging and thanks to Professor Cheng's assignments, because of whom I have come out of the "FEAR" of Segmentation Fault. Now when I get any such error, I say, okay lets see.... gotcha, here is the problem. Earlier the reaction was something like "forget it!!"

Like my Proff. said, "I don't have a magic wand to remove Segmentation faults". I didn't find any foolproof method to get rid of the segmentation faults, but one of the following methods always works for me.

You should be firstly very clear with why segmentation faults occurs. It can occur due to memory leaks, insufficient memory and other memory related issues. This does not happen very often.The most common mistake because of which this error occurs is that we call method on a NULL value or we try to access some memory location which we haven't allocated, or we are deleting some object which doesn't exist.

Example:

SomeClass *temp=NULL;
temp->someFunction();

/*Even if you have initialized temp to a new object, if somewhere in your code the value of temp is getting set to NULL, it will give segmentation fault.*/

or

SomeClass *temp[10];
cout<<temp[15]->someFunction();

Some debugging techniques:

1) Our old friend GDB(GNU Debugger). This is one of the best tools to debug your code. To use GNU debugger make sure you compile your code by using the '-g' switch. To find where your code is causing a segmentation fault do the following. Start gdb by typing:

gdb {executablefile}
run {command line options(if any)}

Run the above commands on terminal without the braces, those are just a way of representation.

After doing this you will get segmentation fault. Now type "where" or "bt". This will take you to the exact line number where your code is causing the error. BT is used to back trace from where the error occurred and it will show the full stack.

2) Valgrind: This is a piece of software which is very helpful in debugging segmentation fault errors. As of now this is only available for Linux systems. To use Valgrind, after installing valgrind, just type:

./valgrind {executablefile} {command line options} ----->(without the braces)

The output of this would be a full stack trace and the exact line number where the problem is occurring.

3) Try writing printf/cout statements at several points and find where your code is breaking. This is not a very good method as it is very tedious and sometimes when Segmentation faults occur you would not get the output of any printf's. But it does work sometimes so it can be given a shot.

4) Increase the size of array: One trick to solve this can be increasing the size of the arrays(wherever we are using an array) that we are using in our code. In case there is a problem wherein we are trying to access an index number which is not allocated, then the problem will be resolved.


This error is similar to NullPointerException that we get in JAVA. Just that in java we can catch the exception and see the full stack trace, thus its easier to debug this problem in java.

Thursday, November 17, 2011

An Alternative to Notepad++ on Linux (Ubuntu)

I was searching an alternative of Notepad++ on linux and I stumbled upon this to log into the remote server and edit remote files:

If you have ever tried to unleash the power of Bundled "Gedit" application on Ubuntu, then you would never want to download anymore external stuff to use the features similar to that of Notepad++ .

In this post, I am going to illustrate only one important feature of Gedit that I found really helpful in logging on to remote server and automatically FTP the changes to reflect on the server.

Open the home folder in ubuntu, then click File->Connect to server. Fill in the details and login. Then you need to open Gedit. Click View->Side Panel.

Now you get to see the side panel having documents. Click on the server that you have logged in and you are good to go. Browse the file you want to edit and get going.

Saturday, November 12, 2011

Android error [2011-11-12 00:13:59 - HelloAndroid]emulator-5554 disconnected! Cancelling 'com.example.helloandroid.HelloAndroid activity launch'!

Finally, after a long long time I am back to post some stuff.

When I had started off with setting up the environment for making Android apps, I had never ever imagined that its going to take so much of time! Finally after lots of R & D on the problem, I have come up with a solution which is rather different than what I read on most of the blogs and forums. So I was excited to share it with the world.

First and foremost, as of now, Eclipse classic 3.7 is not supported by Android ADT plugin. So better not use it for now. I tried it for long and failed and finally found out that its not yet supported. Then I went for Eclipse Indigo for Java developers. I followed the steps given on the Android developers website to install the SDK and ADT plugin. Everything was working like a charm, up until I started with our very own "hello android" app.

When I launched the app on emulator(NOTE: Patience is the key, it takes a lot of time to load emulator itself) I got this weird error saying:

[2011-11-12 00:13:59 - HelloAndroid] emulator-5554 disconnected! Cancelling 'com.example.helloandroid.HelloAndroid activity launch'!

I searched a lot on different forums and tried all possible solutions but then, none of them seemed to be working for me. Then I tried something of my own which worked.

Solution:
1) In the Eclipse window, go to window->AVD Manager, a window "Android Virtual Device Manager" will open, select the virtual device and hit "start".

2) Let the Virtual device boot and load fully.

3) Right click on your project, select Run-as->Run Configuration. In the "TARGET" tab select "Deployment Target Selection Mode" as manual, and check the "Wipe User Data" as well.

4) Click Apply, and then click RUN. A new window will pop up "Android device chooser". In this select "choose a running android device" and select the already running virtual device which we had run in Step 1. Now we are all set to roll. Hit OK and boom your app is loaded and it runs smoothly.

For information on how to install Android SDK visit http://developer.android.com/sdk/index.html and follow the simple steps.