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.

Tuesday, September 1, 2009

Fed up with writing codes in javascript for client side validation?

Since client side validation is the best method to validate forms in web page, application designers usually try to get the javascript code from somewhere or write those lengthy codes themselves. Yesterday I was going through Struts complete reference and then Rahul Singh(neta) bumped in with a cool concept he learnt in HPES. I would love to share this information will you guys so here goes the description.

I was amazed to know this fact that we dont have to do even a single line of coding for javascript validation of forms. There is this tag which dynamically generates javascipt functions for form validation.

For example:
If there is a login form then just make an entry in the validation.xml configuration file and also make an entry in the struts-config.xml. In the struts-config.xml file just make an extra parameter in the tag which is validate="true". In the validation.xml file make an entry for it.

Now finally insert the tag in the head section of the website in which the login page exist and your dynamic javascript code is ready to use.
Enjoyyyy!!

Saturday, August 29, 2009

Developing an application Vs successfully running it on LAN

I guarantee you that making a web application may not seem to be a very big deal but trust me deploying it on INTRANET (or internet) and successfully running it with many many simultaneous users is quite a deal !!

I created the quiz engine in around 3 days but to make it successfully run on my college's intranet, I had to modify the main part of my application around 4-5 times. This took me around 5 more days. But this indeed has taught me a lot of things which I would like to share.

First off, always use a standard method of coding. Never go for the so called "jugad" technology. In the end you will have to pay heavily(in terms of your time) for using "jugad" in your code if there has to be any modification at any point of time in your application. Some very important points to be kept in mind are:

* Always use one single connection for connecting to your database for the whole application for every user. Don't open and close connections every time you have to connect to the database.

* Keep your concept of STATIC variable very very clear in your mind. A static variable declared in any bean(java class) will always have the same constant value throughout the application scope for each and every user until and unless changed. Once the value is changed it will remain constant, again, for every user and for the scope of the whole application

* Try using AJAX as it makes the application very light in terms of the request and response from the user to the server and vice-versa.

* Keep a check on the security aspect of your applications (people tend to test your application with every possible way out!). Keep the application tightly constrained.

* Try configuring your server on LINUX only. It helps a lot and of course it is open source. Split your database server and application server so that the load is distributed.

Tuesday, August 25, 2009

splitting the database and the application server

Wanna split the database server and the application server???? Wanna get some more performance with your servers ?? follow these simple steps and feel the difference........

First off never make a server as your windows XP machine if you want to support more than 5 users simultaneously, it will really make a mess of your application. Always use linux(any distro) as your server. I ususally use fedora or ubuntu. So start with making the root user on the database server and then give the root user permissions so that when the application server tries to contact the database server it can easily get through.

Okay now, what you need next is configuring your application server now. If you have used connection pooling, just give the ip of the server you have made as ur database server and never forget to give the exception of the port used in the firewall settings otherwise u will keep trying and u wud never get a response.

So, configured you application server and gave permissions to the root user with the ip of application server. Then you are all set to feel the experience of load distribution.

some command to create user on the database server and giving permission: (MYSQL 5.1 specific commands)

1) update mysql.user set password=password('newpassword') where user='root';
2) create user 'root'@'ipaddress' identified by 'password';
3) grant all on database.table to 'root'@'ipaddress' identified by 'password';
4) configure your application server for your database server's ip address and you are all set to start your application having a database server on the remote system.

happy splitting!!!!

Sunday, August 23, 2009

Web application development in Struts

Well the project that I have discussed in the last post is made in Struts framework. This framework provides a very easy access of the values that the user provides in the UI components. The components are handled by FormBeans and ActionBeans.

for example:

make a login page having UI components as follows:
1) a text box for username
2) a text box for password
3) a login button

Now, just simply make a form bean having the setter and getter functions of all the component properties. Then make a LoginActionBean through which all the form values are accessed and are manipulated as per the requirements and then stored in the database or any other database interaction that the developer wants to have.

The entries of the beans both form and action beans are to be made in the struts-config.xml file of the configuration file of struts. This is the controller where every request comes.