comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Pass a serial port as user data in a GTK callback handler?
Date: Sun, 16 Feb 2014 19:02:59 +0100
Date: 2014-02-16T19:02:59+01:00	[thread overview]
Message-ID: <ijl5fdpdhdre$.put23hz2dqy1$.dlg@40tude.net> (raw)
In-Reply-To: bmc5foF76avU1@mid.individual.net

On Sun, 16 Feb 2014 13:57:42 -0300, hreba wrote:

>> Specifically to GTK, you should better use smart pointers rather than raw
>> access types when dealing with callbacks and pass them around. The point
>> that UI and I/O will run in independent tasks. It is difficult to prevent
>> dangling pointers otherwise. [I don't consider global variables, of course]
> 
> So I will be back when I have to fight with task programming in Ada, as 
> soon as I begin with the real communication part.
> 
> I did a fast search without finding a data structure "smart pointer" in 
> GObject, GLib or GTK (devhelp), only as part of the C++ language (which 
> I don't know).

GObject and GValue in the case of GTK.

The former is the base type of all GTK widgets. One possible design is that
your custom widget contains the communication object it deals with. The
handlers of the signals pass the access to the widget as the user
parameter. There are means in GTK to ensure that signals from other widgets
(when not children) that pass the widget's pointer around are disconnected
when the widget disappears before the signal emitting widgets do. That
prevents dangling pointers.

A problem with this design is that the widget type in GtkAda is not
limited. You cannot put a limited member into a non-limited widget object.
So you might need a smart pointer again.

In simpler cases you could just allocate the limited object in Initialize
(called from Gtk_New) with new-allocator and keep the returned pointer it
the widget object. In the handler of the widget's Destroy signal you would
do Unchecked_Deallocate on the pointer.

> But I am using Ada. Could you give a hint/reference/link 
> to that?

You use a controlled object derived from Ada.Finalization.Controlled. The
implementation of Finalize does Unchecked_Deallocate on the pointer if the
reference count drops to 0. There are many implementations of this schema
available. But it is not really difficult to do just from scratch.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de


  reply	other threads:[~2014-02-16 18:02 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-15 23:32 Pass a serial port as user data in a GTK callback handler? hreba
2014-02-16  0:35 ` Jeffrey Carter
2014-02-16 16:18   ` hreba
2014-02-16  7:45 ` Niklas Holsti
2014-02-16 16:14   ` hreba
2014-02-16  9:20 ` Dmitry A. Kazakov
2014-02-16 16:57   ` hreba
2014-02-16 18:02     ` Dmitry A. Kazakov [this message]
2014-02-17 16:13 ` adambeneschan
2014-02-17 16:20   ` adambeneschan
2014-02-17 21:31     ` Robert A Duff
2014-02-19  0:53       ` Randy Brukardt
2014-02-19 22:22         ` Robert A Duff
2014-02-20  0:34           ` Randy Brukardt
2014-02-20 11:07             ` Simon Wright
2014-02-20 16:11             ` Robert A Duff
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox