Are there any functions like finalize/dealloc in K...
# kotlin-native
u
Are there any functions like finalize/dealloc in K/N Object?I have a CInterop delegate class like that.And When it's gone,I need to free the delegate pointer.
Copy code
class AssetExp {
    val delegate: CPointer<AssetModel>?

    constructor(){
        delegate = makeAssetModel()
    }

    //when Kotlin Object gone, call this functions
    fun finalize(){
        releaseVideoAssetModel(delegate)
    }
}
🚫 1
n
Is the CPointer provided by a C library?
Kotlin Native uses a GC (for all Kotlin objects) as part of its current memory model (to be replaced with a newer one in the future which is also GC based), which means finalizers aren't supported. C Pointers can be manually freed. With the Linux targets this is done with the
free
function ( https://man7.org/linux/man-pages/man3/free.3p.html ).
Do note that many C libraries (including the GObject related libraries like GTK for example) provide their own functions for properly freeing objects (via C Pointers) supplied by the library itself.
u
@napperley The CPointer provided by my code that is Struct(Actually it is CPP Struct).
//.cpp file
*struct* VideoAssetModelWrap{
  
std::shared_ptr<kuaiying::videoEditor::nopb::VideoAssetModel> nativePtr;
};
VideoAssetModelWrap* makeVideoAssetModel(){
  
VideoAssetModelWrap* videoAssetModel = *new* VideoAssetModelWrap;
  
videoAssetModel->nativePtr = std::make_shared<kuaiying::videoEditor::nopb::VideoAssetModel>();
  
*return* videoAssetModel;
}
//.hpp file
*extern* "C" {
typedef
*struct* VideoAssetModelWrap VideoAssetModelWrap;
VideoAssetModelWrap* makeVideoAssetModel(*void*);
}
I want the life cycle of the CPointer to be the same as that of Kotlin Object.Could that be reached by manually free CPointer?
why k/n object didn't provide the finalize function?
c
@napperley why would GC languages not support finalizers? Java supports them.
n
Java used to support Finalizers, but that feature was deprecated in Java 9 ( https://stackoverflow.com/questions/56139760/why-is-the-finalize-method-deprecated-in-java-9 ). Kotlin has never had that feature. Even the Kotlin JVM platform doesn't have it.
Which side owns the C Pointer? I'm assuming the CPP side is responsible for managing the C Pointer (including its cleanup afterwards). What is the life cycle (scope) of the C Pointer?
u
As my code Cpp side is provide the Create function and the Realease function of the CPointer for outside(KN).I just need that I could call some functions when KN object is gone.
I find some topics about this problem. https://youtrack.jetbrains.com/issue/KT-44191
@napperley As you say, KN Object currently don't support any functions like finalize/dealloc.
n
Alternatively functions with receivers (like
use
for example: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/use.html ) can be used in some situations to solve the problem. Here is a link to the solution: https://stackoverflow.com/questions/44747862/does-kotlin-native-have-destructors
u
This way seems inappropriate in my case.Finally I choose objc to wrap C++.Because objc support ARC.Anyway thanks a lot for your help.😄