• Post Reply Bookmark Topic Watch Topic
  • New Topic

this = null; ??  RSS feed

 
Paul Kater
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,
At work I ran into this very strange bit of code.
A class does all kinds of things. Then I find this stuff in the class:

protected void closeActionPerformed()
{
super.closeActionPerformed();
this.freeResources();
}

private void freeResources()
{
this = null;
System.gc();
System.runFinalization();
}

Does this make sense? Running this code seems strange, since the freeResources would kill the actual object that the code is running in.
Could someone explain to me why this works?

Thanks,
Paul
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I run into strange bits of code where I work.
The strange bit is why the person who wrote it was ever employed.
It looks like you have similar situation.
Just have a chuckle and move on.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Kater:
Hi folks,
At work I ran into this very strange bit of code.
A class does all kinds of things. Then I find this stuff in the class:

protected void closeActionPerformed()
{
super.closeActionPerformed();
this.freeResources();
}

private void freeResources()
{
this = null;
System.gc();
System.runFinalization();
}

Does this make sense? Running this code seems strange, since the freeResources would kill the actual object that the code is running in.
Could someone explain to me why this works?

Thanks,
Paul


thisis only a reference to the object, so setting it to null does not kill the object. It will still exist on the heap. And System.gc() only requests the garbage collector to run - there's no guarantee that it will run. Even if it did run, I would imagine that the JVM maintains a reference to the currently executing object as well so it may be that garbage collection will ignore the object until the freeResources method has returned.
Just guesswork on my part. Maybe reading the JLS will give you a definitive answer.
 
Paul Kater
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, everyone, for your replies. I'll do some reading, and some more investigating when the time is available!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would guess that the person is trying to use an old-fashioned C++ idea of calling "free(this)" to delete the current object (even in C++ this is really problematic, because objects can be on the stack, where calling free() will corrupt the heap and crash your program.)

But one thing no one has pointed out here is that assignment to "this" is forbidden in Java -- this code should not compile!
 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:

But one thing no one has pointed out here is that assignment to "this" is forbidden in Java -- this code should not compile!


Information: 1 error
Information: 0 warnings
Information: Compilation completed with 1 errors and 0 warnings
C:\Java\test.java
Error: line (11) cannot assign a value to final variable this
 
Saeed Amer
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just wanted to thank Ernest and George for bringing forward an important piece of information --> "this" cannot be assigned a value (it is final). I never tried this and did not know.

Thanks again!

Saeed
[ August 11, 2005: Message edited by: Saeed Amer ]
 
Paul Kater
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On "this code should not compile":
This is why I stumbled over it. The original project was set up with Eclips 2.1.2. and that does not complain. I pulled the project into 3.0.1., and that does complain. I am trying to figure out what this developer did to the 2.1.2. to have that accept this error.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting! Eclipse has their own Java compiler, so it sounds like there was a bug in earlier versions. There's a perfectly reasonable implementation for "this = null" -- i.e., perfectly valid bytecode that could be emitted, if it weren't illegal, because "this" is just local variable #0 -- so that certainly seems possible.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it was a bug in the Eclipse compiler. Feels like years ago that I stumbled over it...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!