ExampleAppPrefsPrivate = ptr ExampleAppPrefsPrivateObj
ExampleAppPrefsPrivateObj = object
settings: gio.GSettings
font: gtk3.Widget
transition: gtk3.Widget
proc clear_object*(object_ptr: ptr GObject) {.importc: "g_clear_object", libgobj.}
proc varclear_object*(object_ptr: var GObject) {.importc: "g_clear_object", libgobj.}
# clear_object(cast[ptr GObject]( addr priv.settings))
varclear_object(priv.settings)
assert(priv.settings == nil)
My assumption was, that the variant with var parameter would work the same as that one with the explicit pointer. But for the proc with var parameter assert fails, so library function manages not to set it to nil. I guess the problem is that I have not a plain parameter, but a field in a Nim object. Is that not allowed?
Maybe I should mention that the parameter is defined with volatile in C lib, I do not know if the makes a difference:
void g_clear_object (volatile GObject **object_ptr);
[EDIT]
For the ptr version I have this C code:
Dl_143734(((Gobjectobj143085**) (&(*priv).settings))); nimln(45, "exampleappprefs.nim"); { if (!!(((*priv).settings == NIM_NIL))) goto LA4; failedassertimpl_88591(((NimStringDesc*) &TMP609)); }
And for var version:
Gobjectobj143085** LOC2; LOC2 = 0; LOC2 = &(*priv).settings->Sup; Dl_143741((&LOC2)); nimln(46, "exampleappprefs.nim"); { if (!!(((*priv).settings == NIM_NIL))) goto LA5; failedassertimpl_88591(((NimStringDesc*) &TMP609)); }
GObject* = ptr GObjectObj GObjectPtr* = ptr GObjectObj GObjectObj* = object of GTypeInstanceObj
So you think that a var parameter should work in this notation? Then I think I should make a github bugtracker report? Unfortunately this example compiles only with latest wrapper set for GTK 3.18, which is not fully finished. I will see if I can make an example without GTK.
type
Base = ptr BaseObj
BaseObj = object of Rootobj
b: int
Dia = ptr DiaObj
DiaObj = object of BaseObj
i: int
GSettings = ptr GSettingsObj
GSettingsObj = object of BaseObj
j: int
ExampleAppPrefs* = ptr ExampleAppPrefsObj
ExampleAppPrefsObj = object of DiaObj
ExampleAppPrefsClass = ptr ExampleAppPrefsClassObj
ExampleAppPrefsClassObj = object of DiaObj
p: ExampleAppPrefsPrivate
ExampleAppPrefsPrivate = ptr ExampleAppPrefsPrivateObj
ExampleAppPrefsPrivateObj = object
settings: GSettings
var
eapco: ExampleAppPrefsClassObj
dobj: DiaObj
gso: GSettingsObj
eappo: ExampleAppPrefsPrivateObj
eapc: ExampleAppPrefsClass
gs: GSettings
eapp: ExampleAppPrefsPrivate
eapc = addr eapco
eapc.p = addr eappo
eappo.settings = addr gso
proc xvarclear_object(object_ptr: var Base) =
object_ptr = nil
proc example_app_prefs_dispose(obj: ExampleAppPrefsClass) {.cdecl.} =
var priv: ExampleAppPrefsPrivate
priv = ExampleAppPrefsClass(obj).p
echo "yyyyyyyyyyyyyyyyyy"
xvarclear_object(priv.settings)
assert(priv.settings == nil)
echo "xxxxxxxxxxxxxx"
example_app_prefs_dispose(eapc)
nim c b.nim $ ./b yyyyyyyyyyyyyyyyyy Traceback (most recent call last) b.nim(50) b b.nim(47) example_app_prefs_dispose system.nim(3224) failedAssertImpl system.nim(3216) raiseAssert system.nim(2425) sysFatal Error: unhandled exception: priv.settings == nil [AssertionError] stefan@AMD64X2 ~/nab $ nim -v Nim Compiler Version 0.11.3 (2015-08-11) [Linux: amd64]
This is my third attempt, of course it is very ugly, but from my current understanding it shows that problem. The first two attemps, written from scratch worked fine. So maybe something is special for this example, or maybe I am doing something really wrong.