[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Bug-guile-ncurses] [PATCH 1/2] Add reference count to ITEM structs
From: |
John Darrington |
Subject: |
Re: [Bug-guile-ncurses] [PATCH 1/2] Add reference count to ITEM structs contained by SCMs |
Date: |
Tue, 3 May 2016 13:27:54 +0200 |
User-agent: |
Mutt/1.5.23 (2014-03-12) |
This patch (partially) fixes a bug where a segmentation violation in the
guile library would occur after, for example the current-item had been called.
The problem was, the native C item struct is shared between many SCM smobs.
Thus, if the garbage collector is allowed to run on any one of them, it'll
free memory which will later be needed by another.
This patch fixes the problem by using the "userptr" member as a reference count.
On Tue, May 03, 2016 at 01:22:34PM +0200, John Darrington wrote:
* ncurses/menu_type.c (_scm_from_item, new_item, free_item): Add reference
counting.
---
ncurses/menu_type.c | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/ncurses/menu_type.c b/ncurses/menu_type.c
index 679e9c1..712cd35 100644
--- a/ncurses/menu_type.c
+++ b/ncurses/menu_type.c
@@ -88,6 +88,8 @@ gucu_new_item (SCM name, SCM description)
abort ();
}
+ /* Use the userptr member as a reference count */
+ set_item_userptr (c_item, 0);
SCM ret = _scm_from_item (c_item);
return ret;
@@ -117,6 +119,9 @@ _scm_from_item (ITEM * x)
assert (x == (ITEM *) SCM_SMOB_DATA (s_item));
+ intptr_t refcnt = (intptr_t) item_userptr (x);
+ set_item_userptr (x, (void *) (++refcnt));
+
if (0)
{
fprintf (stderr, "Making <#item> smob from ITEM * %p\n", (void *)
x);
@@ -157,7 +162,11 @@ gc_free_item (SCM item)
assert (m != NULL);
- free_item (m);
+ intptr_t refcnt = (intptr_t) item_userptr (m);
+ set_item_userptr (m, (void *) (--refcnt));
+
+ if (refcnt == 0)
+ free_item (m);
return 0;
}
--
2.1.4
--
Avoid eavesdropping. Send strong encryted email.
PGP Public key ID: 1024D/2DE827B3
fingerprint = 8797 A26D 0854 2EAB 0285 A290 8A67 719C 2DE8 27B3
See http://sks-keyservers.net or any PGP keyserver for public key.
signature.asc
Description: Digital signature
- [Bug-guile-ncurses] Segmentation violations in menus, John Darrington, 2016/05/03
- [Bug-guile-ncurses] [PATCH 1/2] Add reference count to ITEM structs contained by SCMs, John Darrington, 2016/05/03
- Re: [Bug-guile-ncurses] [PATCH 1/2] Add reference count to ITEM structs contained by SCMs,
John Darrington <=
- Re: [Bug-guile-ncurses] [PATCH 1/2] Add reference count to ITEM structs contained by SCMs, Mike Gran, 2016/05/03
- Re: [Bug-guile-ncurses] [PATCH 1/2] Add reference count to ITEM structs contained by SCMs, John Darrington, 2016/05/03
- Re: [Bug-guile-ncurses] [PATCH 1/2] Add reference count to ITEM structs contained by SCMs, Mike Gran, 2016/05/26
- Re: [Bug-guile-ncurses] [PATCH 1/2] Add reference count to ITEM structs contained by SCMs, John Darrington, 2016/05/26
- Re: [Bug-guile-ncurses] [PATCH 1/2] Add reference count to ITEM structs contained by SCMs, Mike Gran, 2016/05/26
[Bug-guile-ncurses] [PATCH 2/2] Do not allow menu's internal data to be destroyed, until the menu itself is destroyed., John Darrington, 2016/05/03