Am Mittwoch, den 14.02.2007, 20:32 +0100 schrieb Christian Lohmaier:
> On Wed, Feb 14, 2007 at 07:14:37PM +0100, Steffen Grund wrote:
> 
> thank you for having a look!
>  
> > perhaps I am getting this wrong, because you shortened your code to post 
> > it here, but: you add a listener to a MailMerge object and then throw 
> > the object away?
> 
> Hmm - I don't mean to throw it away. I'm a novice, and I don't see where
> I throw it away. If you say it is thrown away, then this is very likely
> my problem, although with a terminate listener that way works.
> 

>From your other mail:

>     // org.openoffice.cloph.postnet.XPostnetjava:
>     public void addMMEListener()
>     {
>         MailmergeEventListener MMListener = new MailmergeEventListener();
> 
>         XMultiComponentFactory xMCF = m_xContext.getServiceManager();
> 
>         Object mailMerge = null;

          ^ here you define an object only visible in this method

>         try {
>                 mailMerge = xMCF.createInstanceWithContext(
>                                 "com.sun.star.text.MailMerge", m_xContext);
>         } catch (Exception e) {
>                 e.printStackTrace();
>         }

          now the empty object reference has a value

>         XMailMergeBroadcaster xMMB = null;
>         xMMB = (XMailMergeBroadcaster) UnoRuntime.queryInterface(
>                         XMailMergeBroadcaster.class, mailMerge);

          here the value get's used

>         xMMB.addMailMergeEventListener(MMListener);
>         System.out.println("added listener");
>     }

... and if this exit point is reached the object is lost because there
is no further reference to it. I don't know if the object is really
needed, but if you want to store it you have to hold a reference in a
variable, otherwise the java garbage collector will dispose it at it's
next run.

You could simply do:

public final class PostNetJavaImpl extends WeakBase
   implements com.sun.star.lang.XServiceInfo,
              org.openoffice.cloph.postnet.XPostnetjava
{
    private final XComponentContext m_xContext;
 
    private static final String m_implementationName = 
PostNetJavaImpl.class.getName();
    private static final String[] m_serviceNames = {
        "org.openoffice.cloph.postnet.Postnetjava" };

--> private static final(?) Object mailMerge = null;

and only assign the retrieved object in the method "addMMEListener()" to
it. This way the object stays alive until it's getting disposed manually
or by disposing the surrounding "PostNetJavaImpl" object.

That's the way java works. C++ is similar. It's one reason for inventing
reference counting on object instances.

HTH,
Marc



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to