Skip to content

Commit 7cd5649

Browse files
authored
Merge pull request #541 from vizzuhq/immediate-detach
Detach immediately frees wasm allocated memory.
2 parents 9d75dcc + a88005b commit 7cd5649

File tree

7 files changed

+35
-17
lines changed

7 files changed

+35
-17
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
### Fixed
66

77
- Only dimension on size (+ color) wrongly displayed as treemap, not tablechart.
8+
- Allocated memory is immediately freed after calling detach(), CAnimation and Snapshot
9+
also can be released using their new `free()` method.
810

911
## [0.11.1] - 2024-05-31
1012

src/apps/weblib/ts-api/chart.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ export class Chart implements ChartInterface {
7777

7878
detach(): void {
7979
this._module.unregisterChart(this._cChart)
80+
this._cCanvas.free()
81+
this._cChart.free()
8082
}
8183

8284
start(): void {

src/apps/weblib/ts-api/module/canimctrl.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { CObject } from './cenv.js'
1+
import { CObject, CManagedObject } from './cenv.js'
22

33
/** Stored Animation object. */
4-
export class CAnimation extends CObject {}
4+
export class CAnimation extends CManagedObject {}
55

66
export class CAnimControl extends CObject {
77
setParam(path: string, value = ''): void {

src/apps/weblib/ts-api/module/ccanvas.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { type CEnv, CObject } from './cenv.js'
1+
import { type CEnv, CManagedObject } from './cenv.js'
22
import { type CPointerClosure } from './objregistry.js'
33
import { CColorGradient } from './ccolorgradient.js'
44
import type { CString, CColorGradientPtr } from '../cvizzu.types'
55

6-
export class CCanvas extends CObject {
6+
export class CCanvas extends CManagedObject {
77
constructor(env: CEnv, getId: CPointerClosure) {
88
super(getId, env)
99
}

src/apps/weblib/ts-api/module/cchart.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as Config from '../types/config.js'
55
import * as Styles from '../types/styles.js'
66
import * as Data from '../types/data.js'
77

8-
import { CObject, CEnv } from './cenv.js'
8+
import { CManagedObject, CObject, CEnv } from './cenv.js'
99
import { CPointerClosure } from './objregistry.js'
1010
import { CProxy } from './cproxy.js'
1111
import { CCanvas } from './ccanvas.js'
@@ -14,7 +14,7 @@ import { CAnimation } from './canimctrl.js'
1414
import { isIterable } from '../utils.js'
1515

1616
/** Stored Chart object. */
17-
export class Snapshot extends CObject {}
17+
export class Snapshot extends CManagedObject {}
1818

1919
export class CEvent extends CObject {
2020
preventDefault(): void {
@@ -26,7 +26,7 @@ class CConfig extends CProxy<Config.Chart> {}
2626
class CStyle extends CProxy<Styles.Chart> {}
2727
class CAnimOptions extends CProxy<Anim.Options> {}
2828

29-
export class CChart extends CObject {
29+
export class CChart extends CManagedObject {
3030
config: CConfig
3131
style: CStyle
3232
computedStyle: CStyle

src/apps/weblib/ts-api/module/cenv.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { CPointer, CVizzu, CString } from '../cvizzu.types'
22

33
import { CError } from './cerror.js'
4-
import { ObjectRegistry, FnGetter, CPointerClosure } from './objregistry.js'
4+
import { ObjectRegistry, CPointerClosure } from './objregistry.js'
55

66
export class CEnv {
77
protected _wasm: CVizzu
@@ -23,8 +23,9 @@ export class CEnv {
2323
return this._wasm.UTF8ToString(str)
2424
}
2525

26-
protected _getStatic(getter: FnGetter): CPointerClosure {
27-
return this._objectRegistry.get(this._callStatic(getter))
26+
protected _getStatic(getter: () => CPointer): CPointerClosure {
27+
const cPointer = this._callStatic(getter)()
28+
return (): CPointer => cPointer
2829
}
2930

3031
protected _callStatic<T extends unknown[], R>(f: (...params: T) => R): (...params: T) => R {
@@ -55,7 +56,8 @@ export class CObject extends CEnv {
5556
}
5657

5758
protected _get(getter: (self: CPointer) => CPointer): CPointerClosure {
58-
return this._objectRegistry.get(this._call(getter))
59+
const cPointer = this._call(getter)()
60+
return (): CPointer => cPointer
5961
}
6062

6163
protected _call<T extends unknown[], R>(
@@ -64,3 +66,15 @@ export class CObject extends CEnv {
6466
return super._callStatic(f).bind(this, this.getId())
6567
}
6668
}
69+
70+
export class CManagedObject extends CObject {
71+
constructor(getId: CPointerClosure, cenv: CEnv) {
72+
super(getId, cenv)
73+
this._objectRegistry.register(this.getId)
74+
}
75+
76+
free(): void {
77+
this._objectRegistry.unregister(this.getId)
78+
this._wasm._object_free(this.getId())
79+
}
80+
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { CPointer } from '../cvizzu.types'
22

33
export type FnFree = (rawCPointer: CPointer) => void
4-
export type FnGetter = () => CPointer
54

65
export type CPointerClosure = () => CPointer
76

@@ -14,10 +13,11 @@ export class ObjectRegistry {
1413
})
1514
}
1615

17-
get(fnGetter: FnGetter): CPointerClosure {
18-
const cPointer = fnGetter()
19-
const object = (): CPointer => cPointer
20-
this._finalizationRegistry.register(object, cPointer)
21-
return object
16+
register(objectId: CPointerClosure): void {
17+
this._finalizationRegistry.register(objectId, objectId())
18+
}
19+
20+
unregister(objectId: CPointerClosure): void {
21+
this._finalizationRegistry.unregister(objectId)
2222
}
2323
}

0 commit comments

Comments
 (0)