#! /bin/sh /usr/share/dpatch/dpatch-run
## 12-closedocument.dpatch by Enrique Perez-Terron <perezterron@yahoo.com>
##
## DP: Fix segfault when closing document window

@DPATCH@
diff -u -r -x '*~' texmacs-1.0.6.14/src/Kernel/Abstractions/basic.hpp texmacs-new/src/Kernel/Abstractions/basic.hpp
--- texmacs-1.0.6.14/src/Kernel/Abstractions/basic.hpp	2008-03-19 18:16:36.000000000 +0100
+++ texmacs-new/src/Kernel/Abstractions/basic.hpp	2008-10-03 04:41:13.000000000 +0200
@@ -105,14 +105,16 @@
 extern int concrete_count;
 struct concrete_struct {
   int ref_count;
-  inline concrete_struct (): ref_count (1) { DEBUG(concrete_count++); }
+  bool deleting;
+  inline concrete_struct (): ref_count (1), deleting(false) { DEBUG(concrete_count++); }
   virtual inline ~concrete_struct () { DEBUG(concrete_count--); }
 };
 
 extern int abstract_count;
 struct abstract_struct {
   int ref_count;
-  inline abstract_struct (): ref_count (0) { DEBUG(abstract_count++); }
+  bool deleting;
+  inline abstract_struct (): ref_count (0), deleting(false) { DEBUG(abstract_count++); }
   virtual inline ~abstract_struct () { DEBUG(abstract_count--); }
 };
 
@@ -131,9 +133,10 @@
 ******************************************************************************/
 
 #define INC_COUNT(R)      { (R)->ref_count++; }
-#define DEC_COUNT(R)      { if(0==--((R)->ref_count)) delete (R); }
+#define DEC_COUNT(R)      { if(!(R)->deleting && 0==--((R)->ref_count)) \
+      {(R)->deleting = true; delete (R);} }
 #define INC_COUNT_NULL(R) { if ((R)!=NULL) (R)->ref_count++; }
-#define DEC_COUNT_NULL(R) { if ((R)!=NULL && 0==--((R)->ref_count)) delete (R); }
+#define DEC_COUNT_NULL(R) { if ((R)!=NULL) DEC_COUNT(R); }
 
 // concrete
 #define CONCRETE(PTR)               \
