A Funny Java Flavoured Look at the World

Wednesday, July 05, 2006

Object Referencing gotcha's

the title of this blog entry sounds like a pussy cat dolls song (mmmmm pussy cat dolls).

I have bumped into a few cheeky gotcha's recently.

Firstly we had a problem with the wrong data appearing for different users when using our web app that served maps. It was a subtle problem that rarely occurred so it took us a while to actually catch it and then work out what was going on. I talk about it a bit in this blog entry

http://hoskinator.blogspot.com/2006/06/action-class-design-guidelines.html

I wasn't aware that Struts only creates on Action class and then uses it again and again. We had written some code with out reading design guidelines and used some instance variables with user information in. This lead to problems because this information was always being refreshed or a Struts action might be using an instance variable created by another users. The result was a confusing mix up of variables. We solved this by removing all the instance variables and passing in the objects that were needed.

Another problem I had last week was using a third party jar file. I was retrieving an array of objects, looping through them and assigning object into my new array of objects which I was going to change and then save back. To save on memory (for their code) they only used one object. The result of this was instead of copying a new instance each time in my loop, my array of loops was only pointing to instance repeatedly. So when I went on to the next feature in array retrieved from 3rd party jar it used the same instance but with a new value. I ended up with an array all pointing to the same instance and all having the same value which in this case was the last instance retrieved.

loop {
array [i] = thirdpartyArray[i];
}

What I need to do was to create a new object in the loop, something like this

loop {
array[i] = new object(thirdpartyArray[i]);
}

I wasn't sure whether there method of using the same instance whilst returning an array of objects was standard practise. The documentation said it was done this way for efficient memory usage. I don't know whether this is true either because I had to create new objects, someone has to, so maybe they saved on memory but all they did was pass it on to me. This was a fairly simple gotcha to find although it did take me about half an hour to pinpoint where my code was going wrong.

CheckStyle also complained that I shouldn't create new objects in a loop. I suppose I can see it's point because creating objects in a loop can go drastically wrong if the loop goes wrong.

my final gotcha of the day was using the final keyword. I am not totally sure what happened as it happened to a work buddy but it was something to do with the final keyword on a File class. I was opening a file and reading it in into a String and basically process it. The final keyword resulted in the File object not being garbage collected. This is a bit of flaky one to add because I am not entirely sure what happened but I think it was something to do with a File being Final and then it not being garbage collected and we couldn't then move the actual text flie because we still had it open inside our program.

The first two are probably fairly straight forward and most the people reading this are probably aghast at my buggling and the third one is something which I am not quite sure what the hell was going on. If anyone has any Object Referencing gotcha's please add a comment.

0 Comments:

Post a Comment

<< Home