MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Bug in JavaBlock (or in JavaBlock documentation)

  • To: mathgroup at
  • Subject: [mg56265] Re: [mg56211] Bug in JavaBlock (or in JavaBlock documentation)
  • From: Todd Gayley <tgayley at>
  • Date: Wed, 20 Apr 2005 05:32:15 -0400 (EDT)
  • References: <>
  • Sender: owner-wri-mathgroup at

At 03:55 AM 4/19/2005, J. McKenzie Alexander wrote:
>I think this is a bug in JavaBlock, but it could merely be a mistake in
>the documentation.
>The docs for JavaBlock state: "JavaBlock[expr] causes all new Java
>objects returned to Mathematica during
>the evaluation of expr to be released when expr finishes."  However, if
>you evaluate the following:
>    JavaNew["javax.swing.JProgressBar"]
>    ]
>You'll see that the list of objects returned by PeekObjects[] contains
>exactly one object - the JProgressBar created by the one call to
>JavaNew, in which case it's not true that "all new Java objects
>returned to Mathematica during the evaluation of expr [are] released
>when expr finishes".
>FWIW, I'd prefer JavaBlock behave the way it's described in the docs,
>since then one doesn't have to worry about the odd exception generated
>by having expr return a Java object instead of some other value.


As you have discovered, if a JavaBlock returns a single JavaObject, then 
this object will not be released by the JavaBlock. This feature was added 
to J/Link after the 1.0 release specifically because it is so convenient. A 
relatively common idiom in J/Link programming involves writing a block of 
code that will result in the production of one object you want to return 
from the block and a lot of temporary objects that should be discarded. 
This is similar to Module or Block in Mathematica, which return one useful 
expression and make assignments to temporary local variables along the way. 
JavaBlock is made more convenient by having this "escape hatch" for its 
return value.

In other words, yes it is an exception, but a useful one. It's not likely 
to trip up programmers unaware of the behavior, because why would you write 
a JavaBlock that deliberately returns a JavaObject if you didn't want the 
object to exist outside the block?

This feature is discussed in the section on JavaBlock in the J/Link User 
Guide. It was left out of the usage message in interest of keeping it short 
and as clear as possible. Based on this suggestion, however, I have added a 
sentence to the message.

Todd Gayley
Wolfram Research 

  • References:
    • Bug in UI?
      • From: "Matt" <>
  • Prev by Date: Lists of equations. Again
  • Next by Date: Reduce bug report
  • Previous by thread: Re: Bug in JavaBlock (or in JavaBlock documentation)
  • Next by thread: Re: Bug in UI?