Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

shutting down virtual marchine and Shutdown Hooks

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In my app I've registerded a shut down hook to perform some clean up and housework when the programs exits because of System.exit or CTRL + C. The firs thing that I've noticed is that the logger (java.util.logging) is turned off, but what annoys me more is that I receive a lot of NullPointerException's in places where this shouldn't happen.

I know that all the shut down hooks are executed concurrently. Perhaps a system hook is removing my objects at the same time I'm trying to access them? If so, what is your suggestion to detect any possible application shut down and perform some taks in response?

Cheers,

Iv�n
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are using Runtime#addShutdownHook(Thread), correct, not using finalizers? If so, the hooks should be run before objects start to get collected. If they are running out of order you will have to set up some system of priorities or synchronization to make sure they get run in the proper order.
 
Master Rancher
Posts: 3851
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
receive a lot of NullPointerException's in places where this shouldn't happen.
Can you post any details on this and explain why the pointer should NOT be null?
 
Iván Párraga
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


As you can see, I use an enhanced loop to iterate over a set of clients, well, when trying to access to "currentObserver" I get a NullPointerException... And the previous log message shows: null...

How may this happen? It is supposed that the enhanced loop will iterate over existing objects, isn't it? The only thing I can think of is that the GC is removing the objects concurrently, but I thought this happens only after all the shut down hooks have been executed...

Cheers,

Iv�n
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What type of data structure does the getClients() return. Lists in general don't dis-allow null elements, so iterating over it does not prevent a null from being reached.

Assuming you are using a List, it indicates that somewhere you added a null element to the collection, not that the element was GCed while you iterated over it.

Finalizers, and the final GC, will not occur until after the shutdown hooks. Since you would have a reference to the client inside the list and a reference to the list inside your singleton, then the client would not be eligible for GC until after you either remove it from the list, or finish your shutdowns and let the finalizers run.

Do you have another shutdown hook that empties the list?
[ September 28, 2008: Message edited by: Steve Luke ]
 
Iván Párraga
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

It is a HashSet and I know that is not empty.

Cheers,

Iv�n
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I didn't mean to edit my last post while you were posting.
 
Iván Párraga
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

No problem. I don't have any other finalizer.

Iv�n
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like HashSet is allowed to have a single null element in it as well. So it still seems to me like somewhere you are adding a null element to the list of clients.
 
Iván Párraga
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

You were right. I was inserting null values. My fault ;-)

Cheers,

Iv�n
 
She'll be back. I'm just gonna wait here. With this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic