Kha
                                
                                 Kha copied to clipboard
                                
                                    Kha copied to clipboard
                            
                            
                            
                        [native] memory leaks?
I'm investigating some potential memory leaks in our app, and noticed that kinc_a1_sound_destroy doesn't seem to be called anywhere. Is this supposed to happen, as the new fancy async loader does call kinc_a1_sound_create?
Hm, or maybe it's because finalize won't be registered
https://github.com/Kode/Kha/blob/bedcaaee04bd9de9d6db794239506c68c3330605/Backends/Kore/kha/arrays/Float32Array.hx#L31-L37
because the pointers are just assigned here:
https://github.com/Kode/Kha/blob/bedcaaee04bd9de9d6db794239506c68c3330605/Backends/Kore/kha/LoaderImpl.hx#L194-L198
So i changed the 3 array types to always register the callback
	public inline function new(elements: Int = 0) {
		self = Float32ArrayData.create();
		if (elements > 0) {
			self.alloc(elements);
		}
		Gc.setFinalizer(this, cpp.Function.fromStaticFunction(finalize));
	}
and added a constructor to the 3 cpp_XXXarray.h types in khacpp
(not sure if this is strictly necessary or if data/myLength will always be initialized properly, but deleting a nullptr is valid code and i guess better safe than sorry? :thinking: ).
struct float32array {
	float* data;
	int myLength;
	float32array() {
		data = NULL;
	}
...
}
And this indeed seems to make our memory consumption more stable now, whereas it was only increasing before.
It's still increasing a bit too much for my taste, but that might also be due to fragmentation, so i will investigate some more.
Hm, reading the LoaderImpl code a bit more, doesn't this also leak:
https://github.com/Kode/Kha/blob/bedcaaee04bd9de9d6db794239506c68c3330605/Backends/Kore/kha/LoaderImpl.hx#L176
https://github.com/Kode/Kha/blob/bedcaaee04bd9de9d6db794239506c68c3330605/Backends/Kore/kha/LoaderImpl.hx#L201
All the data seems to be copied into the new array object, but file.data.blob / file.data.sound.compressed_samples is never free'd it seems?
Our memory consumption seems relatively stable now with the 2 PR's, this is what top on linux shows me during the runtime (hyphen-id means unload all game assets and reload the lobby ones)
| id | VIRT | RES | 
|---|---|---|
| lobby | 1421 | 498 | 
| game1 | 2218 | 1282 | 
| - | 2358 | 1422 | 
| game2 | 2201 | 1265 | 
| - | 2205 | 1272 | 
| game3 | 2193 | 1260 | 
| - | 2149 | 1216 | 
| game4 | 2277 | 1349 | 
| - | 2081 | 1153 | 
| game5 | 2161 | 1233 | 
| - | 2081 | 1153 | 
| game6 | 2133 | 1210 | 
| - | 2213 | 1290 | 
| game7 | 2465 | 1507 | 
| - | 2465 | 1525 | 
| game8 | 2293 | 1353 | 
| - | 2373 | 1433 | 
| game9 | 2241 | 1315 | 
fromData does indeed copy - thought it didn't. Will look for some way to avoid that, meanwhile your fix for that is correct.
Ok, i'll leave it open as a reminder then.