<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://jogamp.org/bugzilla/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.2"
          urlbase="https://jogamp.org/bugzilla/"
          
          maintainer="sgothel@jausoft.com"
>

    <bug>
          <bug_id>1312</bug_id>
          
          <creation_ts>2016-06-09 09:13:35 +0200</creation_ts>
          <short_desc>GLContextShareSet memory leak when repeatedly creating and removing GLDrawables</short_desc>
          <delta_ts>2019-12-31 05:50:44 +0100</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>3</classification_id>
          <classification>JogAmp</classification>
          <product>Jogl</product>
          <component>opengl</component>
          <version>2.4.0</version>
          <rep_platform>All</rep_platform>
          <op_sys>all</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P4</priority>
          <bug_severity>normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Tom Nuydens">tom.nuydens</reporter>
          <assigned_to name="Sven Gothel">sgothel</assigned_to>
          <cc>gouessej</cc>
    
    <cc>sgothel</cc>
          
          <cf_type>DEFECT</cf_type>
          <cf_scm_refs>gluegen 5979d66fb0c619be32ebcf9fbe644cf6d757345f
gluegen c6fabb0ac94000afe29156f170c63080a37c034b
gluegen 178c7b9d40e06a04790542241912ca21d2c7b92f
gluegen add34060fde735ea932ca49fbe79e473c5a4f40b
jogl 658e25429aa150fad45a7c81a5a08f9ca35c4479
jogl 79833c9e4741bec9d1f56ea8b322679756b16f70</cf_scm_refs>
          <cf_workaround>---</cf_workaround>

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>5641</commentid>
    <comment_count>0</comment_count>
    <who name="Tom Nuydens">tom.nuydens</who>
    <bug_when>2016-06-09 09:13:35 +0200</bug_when>
    <thetext>My use case is an application that has one &quot;main&quot; GLDrawable which is always visible, and one or more &quot;secondary&quot; GLDrawables that can be opened and closed any number of times (e.g. in a separate window). I want all these drawables to do GL context sharing.

As long as the main drawable exists, however, GLContextShareSet seems to keep strong references to all the secondary ones that I have ever created. Hence, even though they have been destroyed, the secondary ones cannot be GC&apos;ed. I couldn&apos;t see a good reason for this, and was able to work around it by replacing the maps in GLContextShareSet with &quot;weak key&quot; maps.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>5648</commentid>
    <comment_count>1</comment_count>
    <who name="Tom Nuydens">tom.nuydens</who>
    <bug_when>2016-06-13 11:36:40 +0200</bug_when>
    <thetext>I submitted my workaround in a pull request:
https://github.com/sgothel/jogl/pull/101</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>5653</commentid>
    <comment_count>2</comment_count>
    <who name="Julien Gouesse">gouessej</who>
    <bug_when>2016-07-06 09:01:46 +0200</bug_when>
    <thetext>Hi

I agree with your main statement, GLContextShareSet shouldn&apos;t keep strong references on objects that could become useless, it shouldn&apos;t prevent their garbage collection. I just wonder if there is a more simple solution to fix this bug. I have looked at your pull request.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>6070</commentid>
    <comment_count>3</comment_count>
    <who name="Sven Gothel">sgothel</who>
    <bug_when>2018-01-16 07:14:25 +0100</bug_when>
    <thetext>Hi Tom, 

1st of all I would like to see a specific unit test for this,
we should have some already available - maybe we can fine tune them.

The actual bug you are hinting would be (using our terminology):

&quot;GLContextShareSet seems to keep strong references to all the shared ones 
that I have ever created. 
Hence, even though they have been destroyed, the shared ones cannot be GC&apos;ed&quot;

&apos;GLContextShareSet.unregisterSharing(this)&apos; never gets called?

Which points us to the logic in GLContextImpl.destroy() #498:

+++

if( GLContextShareSet.contextDestroyed(this) &amp;&amp;
    !GLContextShareSet.hasCreatedSharedLeft(this) ) {
  GLContextShareSet.unregisterSharing(this);
}

+++

I will investigate this one now.

The &apos;hard lifecycle&apos; with strong references is intended to enforce
a proper user lifecycle call hierarchy, e.g. calling &apos;destroy()&apos;.

Let&apos;s see what we can do here ..</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>6485</commentid>
    <comment_count>4</comment_count>
    <who name="Sven Gothel">sgothel</who>
    <bug_when>2019-12-31 05:50:44 +0100</bug_when>
    <thetext>Introducing WeakIdentityHashMap:
gluegen 5979d66fb0c619be32ebcf9fbe644cf6d757345f
gluegen c6fabb0ac94000afe29156f170c63080a37c034b
gluegen 178c7b9d40e06a04790542241912ca21d2c7b92f
gluegen add34060fde735ea932ca49fbe79e473c5a4f40b

+++

jogl 658e25429aa150fad45a7c81a5a08f9ca35c4479
jogl 79833c9e4741bec9d1f56ea8b322679756b16f70

Bug 1312: GLContextShareSet: Utilize WeakIdentityHashMap for shareMap and its destroyedShares
    
Picking up Tom Nuydens suggestion to utilize a WeakIdentityHashMap instead of a IdentityHashMap,
allowing destroyed GLContext to be removed from the GLContextShareSet through the GC.
    
TestSharedContextVBOES2NEWT5 demonstrates the use-case, having one master context
and several slaves being spawn off, killed and new sets to be spawn off.
Here the GLContextShareSet shall not hard-reference the destroyed and user-unreferenced context,
but allowing the system to GC &apos;em.

+++

Thank you Tom.

I kept the createdShares as hard-references on purpose.
TestSharedContextVBOES2NEWT5 passes the test.</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>