<div dir="ltr"><span style="font-size:12.8px">Hi,</span><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Given a simple @Stateless bean:</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">@Stateless</div><div style="font-size:12.8px">public class Foo {</div><div style="font-size:12.8px">    public void bar() {}</div><div style="font-size:12.8px">}</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Then requesting an instance of this via CDI as follows:</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Foo foo = CDI.current().select(Foo.class).get();</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Causes a lot of leaked proxy instances (at least on Weld). Now what I guess needs to be done is destroying the proxy, taking Antoine&#39;s answer here as a lead: <a href="http://stackoverflow.com/questions/28767536/scope-of-stateless-bean" target="_blank">http://stackoverflow.com/questions/28767536/scope-of-stateless-bean</a></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Only the following throws an UnsupportedOperationException (on Weld 2.3.2, haven&#39;t tested OWB yet)</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><div>Instance&lt;Foo&gt; fooInstance =<span style="white-space:pre-wrap">        </span>CDI.current().select(Foo.class);</div><div>Foo foo = fooInstance.get();</div><div>foo.bar();</div><div><span style="white-space:pre-wrap">foo</span>Instance.destroy(foo);</div></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">The question is, is this how it&#39;s supposed to be done via the spec?</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Implementation wise, what happens in Weld is that the CDI/EJB proxy (com.test.Foo$Proxy$_$$_Weld$EnterpriseProxy$) in the following code doesn&#39;t hit the check for a dependent instance (comments in capitals added by me):</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><div>    public void destroy(T instance) {</div><div>        Preconditions.checkNotNull(instance);</div><div><br></div><div>        // check if this is a proxy of a normal-scoped bean</div><div>        if (instance instanceof ProxyObject) {</div><div><br></div><div>            // THIS BRANCH IS TAKEN FOR CDI/EJB PROXY</div><div><br></div><div>            ProxyObject proxy = (ProxyObject) instance;</div><div>            if (proxy.getHandler() instanceof ProxyMethodHandler) {</div><div>                ProxyMethodHandler handler = (ProxyMethodHandler) proxy.getHandler();</div><div>                Bean&lt;?&gt; bean = handler.getBean();</div><div>                Context context = getBeanManager().getContext(bean.getScope());</div><div>                if (context instanceof AlterableContext) {</div><div>                    AlterableContext alterableContext = (AlterableContext) context;<br><br>                    // CONTEXT IS A DEPENDENTCONTEXTIMPL THAT THROWS</div><div>                    // UnsupportedOperationException FROM ITS DESTROY() METHOD</div><div>                    alterableContext.destroy(bean);</div><div>                    return;</div><div>                } else {</div><div>                    throw BeanLogger.LOG.destroyUnsupported(context);</div><div>                }</div><div>            }</div><div>        }</div><div><br></div><div>        // check if this is a dependent instance</div><div>        CreationalContext&lt;? super T&gt; ctx = getCreationalContext();</div><div>        if (ctx instanceof WeldCreationalContext&lt;?&gt;) {</div><div>            WeldCreationalContext&lt;? super T&gt; weldCtx = cast(ctx);</div><div><br></div><div>            // PROXY REFERENCES ARE KEPT HERE IN A </div><div>            // &quot;dependentInstances&quot; LIST, AND WOULD BE CLEARED HERE</div><div>            // BUT THIS CODE IS NEVER REACHED</div><div>            weldCtx.destroyDependentInstance(instance);</div><div>        }</div><div>    }</div></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Now I wonder, am I doing something wrong (according to the CDI spec), or could this be a bug in the Weld code?</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Kind regards,</div><div style="font-size:12.8px">Arjan Tijms</div></div>