comp.lang.ada
 help / color / mirror / Atom feed
* Ada newbie  + Gtkada + Win2000 = ?
@ 2007-09-16 14:11 Brian Drummond
  2007-09-16 14:53 ` Dmitry A. Kazakov
  2007-09-16 18:12 ` michael bode
  0 siblings, 2 replies; 4+ messages in thread
From: Brian Drummond @ 2007-09-16 14:11 UTC (permalink / raw)


Just trying to learn Ada; (it's somewhat familiar after years on VHDL)
and interested in making simple cross-platform Win/Linux applications...

So I built the "testgtk" example (on Win2000 SP4) with Gnat2007 and
GtkAda (2006 as recommended by Adacore) and...
well it's unreliable; I've found quite a few ways of breaking it. (The
pre-built testgdk.exe is similarly unreliable so it's not just my
build).

This is good exercise; if I use the tools for my own application, I'll
certainly have to face - and fix - problems; the ease of uncovering and
fixing problems is part of what I want from Ada.

So I have fixed some; but am having trouble with others.
I am posting because I am interested in two questions:
(a) are these problems repeatable on other systems? (maybe some are
Windows-only; maybe only my particular machine)

(b) where do I start to fix the ones I'm having trouble with?
(Not necessarily a complete solution; but an approach. I'm a total
newbie with GTK for example)


Fixed:
(1) the "Misc/Cursors" demo has a spin control with a list box. Enter an
odd number in the list box and watch...
I can see no better fix than subtracting 1 if odd in Set_Cursor (and
setting the spinner value to match), without an extensive rewrite. (Took
the opportunity to replace some magic numbers with Gdk_Cursor_Type'first
and 'last too)

(2) Composite Widgets/plot_realtime draws "outside the box".
Clip_Data(Active_Plot, True) before Add_Plot and Show seems to fix.

(3) Without "test.xpm" in the executable's directory, "Containers
handle_box" and some other demos fall over (constraint error in
"getDrawingSize").
My fix: a new package supplies a new "Create_From_Xpm" function; this
checks the file exists and substitutes a default pixmap from a constant
using Gdk_Pixmap.Create_From_Xpm_D if necessary. 
The new package also wraps the Gdk_Pixmap type so all I do is With; Use;
it in place of GDK_Pixmap, in Create_Toolbar.adb and Create_Pixmap.adb
respectively.

Not fixed:
(1) Composite Widgets/plot_3d: pressing a rotate button obscures the
other buttons. I have no idea how to proceed with this. It may be
possible for the "rotate_x/y/z" functions to emit an "expose" signal to
the buttons but that looks like the wrong answer. 
I feel there ought to be a way to alert the canvas to draw the buttons;
I'd have hoped "put"ting the buttons there would do it but apparently
not...
Or is this a bug in GtkAda underneath?

(2) Memory leaks in all the "Plot" widgets.
(a) alternately selecting "Plot" and "Plot3D" I can get memory usage up
to about 45MB before allocation fails;
(b) "Plot_Realtime" lasts about a minute before falling over (at about
18MB memory usage)
(c) "Plot_3D" leaks about 320K per press of a "Rotate" button. On the
31st press (every time I've counted!) it falls over with:
gdkgc-win32.c:846 SaveDC failed: not enough memory available to process
this command" and an Exception_Access_Violation.
This message is typical of the failures in the other widgets too.

There are two mysteries here: - why the memory leaks occur; the GTK
documentation claims it manages memory for you, and why it falls over so
early (even when there is about 500MB of free memory) - if there is some
heap limit imposed on the program, I can't see it.

(3)  "plot" has a Print button. This writes a 71K Postscript file, then
raises an Exception_Access_Violation. I haven't worked on this one at
all.

The memory leaks are concerning; it doesn't look to me as if there is a
solution at the Ada level, and the point of the exercise is to escape
from C, if possible. If I need to get the full set of GTK sources, build
them and debug, I might, but it'll take a lot longer...

Thanks in advance for any help you can offer,

- Brian



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Ada newbie  + Gtkada + Win2000 = ?
  2007-09-16 14:11 Ada newbie + Gtkada + Win2000 = ? Brian Drummond
@ 2007-09-16 14:53 ` Dmitry A. Kazakov
  2007-09-16 23:54   ` Brian Drummond
  2007-09-16 18:12 ` michael bode
  1 sibling, 1 reply; 4+ messages in thread
From: Dmitry A. Kazakov @ 2007-09-16 14:53 UTC (permalink / raw)


On Sun, 16 Sep 2007 15:11:06 +0100, Brian Drummond wrote:

> There are two mysteries here: - why the memory leaks occur; the GTK
> documentation claims it manages memory for you, and why it falls over so
> early (even when there is about 500MB of free memory) - if there is some
> heap limit imposed on the program, I can't see it.

Well, GTK does not manage memory. Though, it has a reference counting model
for its objects, it does not provide handles to. The counts need to be
managed manually using Ref, Unref, Sink and Destroy. GtkAda is a thin
binding to GTK+, so it just follows this [IMO, flawed] design.

In effect, when you call Gtk_New the result of is a plain pointer. It is
your responsibility to pass it back to a container type or else explicitly
kill the object. Note also as if that was not enough complicated, GTK has
widgets and other objects. When created both have the reference count 1.
But then for objects you do:

1. put it into a GTK container (the count is now 2)
2. Unref (the count is 1 and the container holds the object)

for widgets

1. put into a container (the count is still 1, but the widget is no more
"floating," it is owned)
2. do nothing

It is very easy to mess with this. I guess that this is what caused leaking
in the example you are talking about.

> The memory leaks are concerning; it doesn't look to me as if there is a
> solution at the Ada level, and the point of the exercise is to escape
> from C, if possible. If I need to get the full set of GTK sources, build
> them and debug, I might, but it'll take a lot longer...

Sources are available at:

http://www.gtk.org/

You have to take somewhat outdated ones, because GtkAda lags behind.

For debugging reference counts you can use the Ref_Count function from:

http://www.dmitry-kazakov.de/ada/gtkada_contributions.htm#10.2

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



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Ada newbie  + Gtkada + Win2000 = ?
  2007-09-16 14:11 Ada newbie + Gtkada + Win2000 = ? Brian Drummond
  2007-09-16 14:53 ` Dmitry A. Kazakov
@ 2007-09-16 18:12 ` michael bode
  1 sibling, 0 replies; 4+ messages in thread
From: michael bode @ 2007-09-16 18:12 UTC (permalink / raw)


Brian Drummond schrieb:

> (2) Memory leaks in all the "Plot" widgets.
> (a) alternately selecting "Plot" and "Plot3D" I can get memory usage up
> to about 45MB before allocation fails;
> (b) "Plot_Realtime" lasts about a minute before falling over (at about
> 18MB memory usage)
> (c) "Plot_3D" leaks about 320K per press of a "Rotate" button. On the
> 31st press (every time I've counted!) it falls over with:
> gdkgc-win32.c:846 SaveDC failed: not enough memory available to process
> this command" and an Exception_Access_Violation.
> This message is typical of the failures in the other widgets too.

I've seen some of these memory leaks and crashes in GtkAda 2.4. I think 
GtkAda 2.4 shipped with a (even then) outdated version of GtkExtra and 
when using a newer GtkExtra at least the Plot Realtime worked. I'm not 
sure but I doubt GtkAda does much memory allocation itself, so I'd 
suspect memory leaks originate in Gtk and especially in GtkExtra.



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Ada newbie  + Gtkada + Win2000 = ?
  2007-09-16 14:53 ` Dmitry A. Kazakov
@ 2007-09-16 23:54   ` Brian Drummond
  0 siblings, 0 replies; 4+ messages in thread
From: Brian Drummond @ 2007-09-16 23:54 UTC (permalink / raw)


On Sun, 16 Sep 2007 16:53:54 +0200, "Dmitry A. Kazakov"
<mailbox@dmitry-kazakov.de> wrote:

>On Sun, 16 Sep 2007 15:11:06 +0100, Brian Drummond wrote:
>
>> There are two mysteries here: - why the memory leaks occur; the GTK
>> documentation claims it manages memory for you, and why it falls over so
>> early (even when there is about 500MB of free memory) - if there is some
>> heap limit imposed on the program, I can't see it.
>
>Well, GTK does not manage memory. Though, it has a reference counting model
>for its objects, it does not provide handles to. The counts need to be
>managed manually using Ref, Unref, Sink and Destroy. GtkAda is a thin
>binding to GTK+, so it just follows this [IMO, flawed] design.
>
>In effect, when you call Gtk_New the result of is a plain pointer. It is
>your responsibility to pass it back to a container type or else explicitly
>kill the object. Note also as if that was not enough complicated, GTK has
>widgets and other objects. When created both have the reference count 1.
>But then for objects you do:
>
>1. put it into a GTK container (the count is now 2)
>2. Unref (the count is 1 and the container holds the object)

Thanks, that gives me a few things to look at.
But so far, it looks like unref'ing anything that is GTK_New'ed from
user code (i.e. the "testgtk" sources) results in releasing important
objects; I believe I have to dig into GTK or GDK to find the problem.

As you say, the problem seems to be getting handles to the things you
need to free.

>Sources are available at:
I found some (presumably the correct vintage) on libre; haven't tried
them yet. Next weekend probably.

>For debugging reference counts you can use the Ref_Count function from:
>
>http://www.dmitry-kazakov.de/ada/gtkada_contributions.htm#10.2

Thanks!

- Brian



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2007-09-16 23:54 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-09-16 14:11 Ada newbie + Gtkada + Win2000 = ? Brian Drummond
2007-09-16 14:53 ` Dmitry A. Kazakov
2007-09-16 23:54   ` Brian Drummond
2007-09-16 18:12 ` michael bode

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