java - Delete native peer with general PhantomReference class -


as hans boehm in google i/o '17 talk "how manage native c++ memory in android" suggests use phantomreferenceclass ensure native peers deleted properly.

in linked video @ 18 min 57 sec shows example implementation of object registering phantomreference class it's type. phantomreference class, shows @ 19 min 49 sec. copied approach example object. see below.

while approach works fine, not scale. need create quite amount of objects , haven't found way create base class (either objects or phantomreference base class) take objects , handle native deletion properly.

how can make generic base phantomreference class can call native static method on provided object?

i've tried transform phantomreference generic native static deletion method hinders implementation.

my workviewmodel

import android.databinding.*;  public class workviewmodel extends baseobservable {   private long _nativehandle;    public workviewmodel(database database, int workid)   {     _nativehandle = create(database.getnativehandle(), workid);     workviewmodelphantomreference.register(this, _nativehandle);   }    private static native long create(long databasehandle, int workid);   static native void delete(long nativehandle);    @bindable   public native int getworkid();   public native void setworkid(int workid); } 

my workviewmodelphantomreference

import java.lang.ref.*; import java.util.*;  public class workviewmodelphantomreference extends phantomreference<workviewmodel> {   private static set<workviewmodelphantomreference> phantomreferences = new hashset<workviewmodelphantomreference>();   private static referencequeue<workviewmodel> garbagecollectedobjectsqueue = new referencequeue<workviewmodel>();   private long _nativehandle;    private workviewmodelphantomreference(workviewmodel workviewmodel, long nativehandle)   {     super(workviewmodel, garbagecollectedobjectsqueue);     _nativehandle = nativehandle;   }    public static void register(workviewmodel workviewmodel, long nativehandle)   {     phantomreferences.add(new workviewmodelphantomreference(workviewmodel, nativehandle));   }    public static void deleteorphanednativepeerobjects()   {     workviewmodelphantomreference reference;      while((reference = (workviewmodelphantomreference)garbagecollectedobjectsqueue.poll()) != null)     {       workviewmodel.delete(reference._nativehandle);       phantomreferences.remove(reference);     }   } } 


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -