[perl-Gtk2] Use the AV* typemap in a few places



commit bd227acbe39d006430da0a16dce5445300794b69
Author: Kevin Ryde <user42 zip com au>
Date:   Wed Nov 10 14:35:32 2010 +1100

    Use the AV* typemap in a few places
    
    Instead of PUSHs or SV* returns.  Concerns gtk_radio_action_get_group(),
    gtk_radio_menu_item_get_group(), gtk_radio_button_get_group(),
    gtk_radio_tool_button_get_group().
    
    https://bugzilla.gnome.org/show_bug.cgi?id=634708

 t/GtkRadioAction.t       |   20 +++++++++++++++++---
 t/GtkRadioButton.t       |   19 +++++++++++++++++--
 t/GtkRadioMenuItem.t     |   19 +++++++++++++++++--
 t/GtkRadioToolButton.t   |   19 +++++++++++++++++--
 xs/GtkRadioAction.xs     |   15 ++++++++-------
 xs/GtkRadioButton.xs     |   16 ++++++++--------
 xs/GtkRadioMenuItem.xs   |   17 +++++++++--------
 xs/GtkRadioToolButton.xs |   13 +++++++------
 8 files changed, 100 insertions(+), 38 deletions(-)
---
diff --git a/t/GtkRadioAction.t b/t/GtkRadioAction.t
index f84c7d3..ded6d6f 100644
--- a/t/GtkRadioAction.t
+++ b/t/GtkRadioAction.t
@@ -4,7 +4,7 @@
 
 use Gtk2::TestHelper
 	at_least_version => [2, 4, 0, "Action-based menus are new in 2.4"],
-	tests => 10, noinit => 1;
+	tests => 14, noinit => 1;
 
 
 my @actions = (Gtk2::RadioAction->new (name => 'one', value => 0));
@@ -20,7 +20,21 @@ my $group = $actions[0]->get_group;
 push @actions, Gtk2::RadioAction->new (name => 'six', value => 5);
 isa_ok ($actions[$#actions], 'Gtk2::RadioAction');
 $actions[$#actions]->set_group ($group);
-
+{
+  # get_group() no memory leaks in arrayref return and array items
+  my $x = Gtk2::RadioAction->new (name => 'x', value => 0);
+  my $y = Gtk2::RadioAction->new (name => 'y', value => 0);
+  $y->set_group($x);
+  my $aref = $x->get_group;
+  is_deeply($aref, [$x,$y]);
+  require Scalar::Util;
+  Scalar::Util::weaken ($aref);
+  is ($aref, undef, 'get_group() array destroyed by weakening');
+  Scalar::Util::weaken ($x);
+  is ($x, undef, 'get_group() item x destroyed by weakening');
+  Scalar::Util::weaken ($y);
+  is ($y, undef, 'get_group() item y destroyed by weakening');
+}
 
 is ($actions[0]->get_current_value, 0);
 
@@ -37,5 +51,5 @@ ok ($actions[3]->get_active);
 
 __END__
 
-Copyright (C) 2003-2006 by the gtk2-perl team (see the file AUTHORS for the
+Copyright (C) 2003-2006, 2010 by the gtk2-perl team (see the file AUTHORS for the
 full list).  See LICENSE for more information.
diff --git a/t/GtkRadioButton.t b/t/GtkRadioButton.t
index e647e3e..fe89fc9 100644
--- a/t/GtkRadioButton.t
+++ b/t/GtkRadioButton.t
@@ -7,7 +7,7 @@
 # 	- rm
 #########################
 
-use Gtk2::TestHelper tests => 9;
+use Gtk2::TestHelper tests => 13;
 
 ok( my $vbox = Gtk2::VBox->new(0, 5) );
 # $win->add($vbox);
@@ -32,6 +32,21 @@ ok( $rdobtn = Gtk2::RadioButton->new([ $rdobtn ], "bar2") );
 $vbox->pack_start($rdobtn, 0, 0, 0);
 
 ok( scalar(@{$rdobtn->get_group}) == 3 );
+{
+  # get_group() no memory leaks in arrayref return and array items
+  my $x = Gtk2::RadioButton->new;
+  my $y = Gtk2::RadioButton->new;
+  $y->set_group($x);
+  my $aref = $x->get_group;
+  is_deeply($aref, [$x,$y]);
+  require Scalar::Util;
+  Scalar::Util::weaken ($aref);
+  is ($aref, undef, 'get_group() array destroyed by weakening');
+  Scalar::Util::weaken ($x);
+  is ($x, undef, 'get_group() item x destroyed by weakening');
+  Scalar::Util::weaken ($y);
+  is ($y, undef, 'get_group() item y destroyed by weakening');
+}
 
 my $i;
 my @rdobtns;
@@ -47,5 +62,5 @@ ok( scalar(@{$rdobtns[0]->get_group}) == 5 );
 
 __END__
 
-Copyright (C) 2003 by the gtk2-perl team (see the file AUTHORS for the
+Copyright (C) 2003, 2010 by the gtk2-perl team (see the file AUTHORS for the
 full list).  See LICENSE for more information.
diff --git a/t/GtkRadioMenuItem.t b/t/GtkRadioMenuItem.t
index 61151d5..3d9d23a 100644
--- a/t/GtkRadioMenuItem.t
+++ b/t/GtkRadioMenuItem.t
@@ -1,6 +1,6 @@
 #!/usr/bin/perl -w
 use strict;
-use Gtk2::TestHelper tests => 9;
+use Gtk2::TestHelper tests => 13;
 
 # $Id$
 
@@ -19,6 +19,21 @@ isa_ok($item_four, "Gtk2::RadioMenuItem");
 $item_three -> set_group($item_one);
 
 is_deeply($item_one -> get_group(), [$item_one, $item_two, $item_three, $item_four]);
+{
+  # get_group() no memory leaks in arrayref return and array items
+  my $x = Gtk2::RadioMenuItem->new;
+  my $y = Gtk2::RadioMenuItem->new;
+  $y->set_group($x);
+  my $aref = $x->get_group;
+  is_deeply($aref, [$x,$y]);
+  require Scalar::Util;
+  Scalar::Util::weaken ($aref);
+  is ($aref, undef, 'get_group() array destroyed by weakening');
+  Scalar::Util::weaken ($x);
+  is ($x, undef, 'get_group() item x destroyed by weakening');
+  Scalar::Util::weaken ($y);
+  is ($y, undef, 'get_group() item y destroyed by weakening');
+}
 
 SKIP: {
   skip("the from_widget variants are new in 2.4", 4)
@@ -38,5 +53,5 @@ SKIP: {
 
 __END__
 
-Copyright (C) 2003 by the gtk2-perl team (see the file AUTHORS for the
+Copyright (C) 2003, 2010 by the gtk2-perl team (see the file AUTHORS for the
 full list).  See LICENSE for more information.
diff --git a/t/GtkRadioToolButton.t b/t/GtkRadioToolButton.t
index 1ffe3dd..855357b 100644
--- a/t/GtkRadioToolButton.t
+++ b/t/GtkRadioToolButton.t
@@ -2,7 +2,7 @@
 use strict;
 use Gtk2::TestHelper
   at_least_version => [2, 4, 0, "Action-based menus are new in 2.4"],
-  tests => 8;
+  tests => 12;
 
 # $Id$
 
@@ -31,8 +31,23 @@ $item = Gtk2::RadioToolButton -> new();
 
 $item -> set_group([$item_two, $item_three]);
 is_deeply($item -> get_group(), [$item_two, $item_three]);
+{
+  # get_group() no memory leaks in arrayref return and array items
+  my $x = Gtk2::RadioToolButton->new;
+  my $y = Gtk2::RadioToolButton->new;
+  $y->set_group($x);
+  my $aref = $x->get_group;
+  is_deeply($aref, [$x,$y]);
+  require Scalar::Util;
+  Scalar::Util::weaken ($aref);
+  is ($aref, undef, 'get_group() array destroyed by weakening');
+  Scalar::Util::weaken ($x);
+  is ($x, undef, 'get_group() item x destroyed by weakening');
+  Scalar::Util::weaken ($y);
+  is ($y, undef, 'get_group() item y destroyed by weakening');
+}
 
 __END__
 
-Copyright (C) 2003 by the gtk2-perl team (see the file AUTHORS for the
+Copyright (C) 2003, 2010 by the gtk2-perl team (see the file AUTHORS for the
 full list).  See LICENSE for more information.
diff --git a/xs/GtkRadioAction.xs b/xs/GtkRadioAction.xs
index b060ac6..1be2788 100644
--- a/xs/GtkRadioAction.xs
+++ b/xs/GtkRadioAction.xs
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003-2006 by the gtk2-perl team (see the file AUTHORS)
+ * Copyright (c) 2003-2006, 2010 by the gtk2-perl team (see the file AUTHORS)
  *
  * Licensed under the LGPL, see LICENSE file for more information.
  *
@@ -26,17 +26,18 @@ Glib::Object::new.
 =cut
 
 ## GSList  *gtk_radio_action_get_group (GtkRadioAction *action);
-void
+AV *
 gtk_radio_action_get_group (GtkRadioAction *action)
     PREINIT:
 	GSList * group, * i;
-	AV * av;
-    PPCODE:
+    CODE:
 	group = gtk_radio_action_get_group (action);
-	av = newAV ();
+	RETVAL = newAV();
+	sv_2mortal ((SV*) RETVAL);  /* typemap expects RETVAL mortalized */
 	for (i = group ; i != NULL ; i = i->next)
-		av_push (av, newSVGtkRadioAction (i->data));
-	PUSHs (sv_2mortal (newRV_noinc ((SV*)av)));
+		av_push (RETVAL, newSVGtkRadioAction (i->data));
+    OUTPUT:
+	RETVAL
 
 ## void gtk_radio_action_set_group (GtkRadioAction *action, GSList *group);
 void gtk_radio_action_set_group (GtkRadioAction *action, SV *member_or_listref);
diff --git a/xs/GtkRadioButton.xs b/xs/GtkRadioButton.xs
index 4b1d5c3..6cd0a21 100644
--- a/xs/GtkRadioButton.xs
+++ b/xs/GtkRadioButton.xs
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003 by the gtk2-perl team (see the file AUTHORS)
+ * Copyright (c) 2003, 2010 by the gtk2-perl team (see the file AUTHORS)
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
@@ -175,25 +175,25 @@ gtk_radio_button_set_group (radio_button, member_or_listref)
 
 # GSList * gtk_radio_button_get_group (GtkRadioButton *radio_button)
 =for apidoc
-=for signature $group = $radio_button->get_group
 Return a reference to the radio group to which I<$radio_button> belongs.
 The group is a reference to an array of widget references; the array is B<not>
 magical, that is, it will not be updated automatically if the group changes;
 call C<get_group> each time you want to use the group.
 =cut
-void
+AV *
 gtk_radio_button_get_group (radio_button)
 	GtkRadioButton * radio_button
     PREINIT:
 	GSList * group;
 	GSList * i;
-	AV     * av;
-    PPCODE:
+    CODE:
 	group = radio_button->group;
-	av = newAV();
+	RETVAL = newAV();
+	sv_2mortal ((SV*) RETVAL);  /* typemap expects RETVAL mortalized */
 	for( i = group; i ; i = i->next )
 	{
-		av_push(av, newSVGtkRadioButton(GTK_RADIO_BUTTON(i->data)));
+		av_push(RETVAL, newSVGtkRadioButton(GTK_RADIO_BUTTON(i->data)));
 	}
-	PUSHs(sv_2mortal(newRV_noinc((SV*)av)));
+    OUTPUT:
+	RETVAL
 
diff --git a/xs/GtkRadioMenuItem.xs b/xs/GtkRadioMenuItem.xs
index 7b2e026..3edb4ed 100644
--- a/xs/GtkRadioMenuItem.xs
+++ b/xs/GtkRadioMenuItem.xs
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003 by the gtk2-perl team (see the file AUTHORS)
+ * Copyright (c) 2003, 2010 by the gtk2-perl team (see the file AUTHORS)
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
@@ -85,23 +85,24 @@ gtk_radio_menu_item_new_from_widget (class, group, label=NULL)
 
 # GSList * gtk_radio_menu_item_get_group (GtkRadioMenuItem *radio_menu_item)
 =for apidoc
-Returns a list of Gtk2::RadioMenuItems, the group.
+Return a reference to an array of C<Gtk2::RadioMenuItem>s, the group members.
 =cut
-void
+AV *
 gtk_radio_menu_item_get_group (radio_menu_item)
 	GtkRadioMenuItem * radio_menu_item
     PREINIT:
 	GSList * group;
 	GSList * i;
-	AV     * av;
-    PPCODE:
+    CODE:
 	group = radio_menu_item->group;
-	av = newAV();
+	RETVAL = newAV();
+	sv_2mortal ((SV*) RETVAL);  /* typemap expects RETVAL mortalized */
 	for( i = group; i ; i = i->next )
 	{
-		av_push(av, newSVGtkRadioMenuItem(GTK_RADIO_MENU_ITEM(i->data)));
+		av_push(RETVAL, newSVGtkRadioMenuItem(GTK_RADIO_MENU_ITEM(i->data)));
 	}
-	PUSHs(sv_2mortal(newRV_noinc((SV*)av)));
+    OUTPUT:
+	RETVAL
 
 void
 gtk_radio_menu_item_set_group (radio_menu_item, member_or_listref)
diff --git a/xs/GtkRadioToolButton.xs b/xs/GtkRadioToolButton.xs
index 3299663..38a5c7e 100644
--- a/xs/GtkRadioToolButton.xs
+++ b/xs/GtkRadioToolButton.xs
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003 by the gtk2-perl team (see the file AUTHORS)
+ * Copyright (c) 2003, 2010 by the gtk2-perl team (see the file AUTHORS)
  *
  * Licensed under the LGPL, see LICENSE file for more information.
  *
@@ -27,14 +27,14 @@ group_from_sv (SV * member_or_listref)
 	return group;
 }
 
-static SV *
-sv_from_group (GSList * group)
+static AV *
+av_from_group (GSList * group)
 {
 	GSList * i;
 	AV * av = newAV ();
 	for (i = group ; i != NULL ; i = i->next)
 		av_push (av, newSVGtkRadioToolButton (i->data));
-	return newRV_noinc ((SV*)av);
+	return av;
 }
 
 MODULE = Gtk2::RadioToolButton PACKAGE = Gtk2::RadioToolButton PREFIX = gtk_radio_tool_button_
@@ -63,10 +63,11 @@ GtkToolItem *gtk_radio_tool_button_new_with_stock_from_widget (class, GtkWidget_
 	(GtkRadioToolButton*)group, stock_id
 
  ##GSList * gtk_radio_tool_button_get_group (GtkRadioToolButton *button);
-SV *
+AV *
 gtk_radio_tool_button_get_group (GtkRadioToolButton *button)
     CODE:
-	RETVAL = sv_from_group (gtk_radio_tool_button_get_group (button));
+	RETVAL = av_from_group (gtk_radio_tool_button_get_group (button));
+	sv_2mortal ((SV*) RETVAL);  /* typemap expects RETVAL mortalized */
     OUTPUT:
 	RETVAL
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]