[Kde-bindings] KDE/kdebindings/qtruby

Richard Dale Richard_Dale at tipitina.demon.co.uk
Tue Mar 13 10:54:58 UTC 2007


SVN commit 642098 by rdale:

* When a ruby string was converted to a 'char *' arg, a copy was malloc'd
  and then never freed. This behaviour was added to fix a problem with the
  small number of methods that 'took ownership' of the string and expected
  it to stay around after the method call. However, it introduces a 
  memory leak for the 95% of methods that don't need it, and also means
  that if the 'char *' is changed during the method call, the changes 
  aren't reflected in the ruby string. Thanks to Jan Wedekind for the
  discussion leading up to changing the code. Any methods where this 
  behaviour is wrong will now need to be special cased.

CCMAIL: kde-bindings at kde.org
CCMAIL: jan at wedesoft.de



 M  +12 -0     ChangeLog  
 M  +0 -67     rubylib/qtruby/handlers.cpp  
 M  +1 -2      rubylib/qtruby/marshall_basetypes.h  
 M  +2 -6      rubylib/qtruby/marshall_primitives.h  


--- trunk/KDE/kdebindings/qtruby/ChangeLog #642097:642098
@@ -1,3 +1,15 @@
+2007-03-13  Richard Dale  <rdale at foton.es>
+
+	* When a ruby string was converted to a 'char *' arg, a copy was malloc'd
+	  and then never freed. This behaviour was added to fix a problem with the
+	  small number of methods that 'took ownership' of the string and expected
+	  it to stay around after the method call. However, it introduces a 
+	  memory leak for the 95% of methods that don't need it, and also means
+	  that if the 'char *' is changed during the method call, the changes 
+	  aren't reflected in the ruby string. Thanks to Jan Wedekind for the
+	  discussion leading up to changing the code. Any methods where this 
+	  behaviour is wrong will now need to be special cased.
+
 2007-03-06  Richard Dale  <rdale at foton.es>
 
 	* Call rb_gc_mark() on the internalPointer VALUE within a Qt::ModelIndex 
--- trunk/KDE/kdebindings/qtruby/rubylib/qtruby/handlers.cpp #642097:642098
@@ -721,63 +721,10 @@
     m->unsupported();
 }
 
-static void marshall_charP(Marshall *m) {
-  marshall_it<char *>(m);
-}
-/*
-	switch(m->action()) {
-      case Marshall::FromVALUE:
-	{
-	    VALUE rv = *(m->var());
-	    if(rv == Qnil) {
-                m->item().s_voidp = 0;
-                break;
-	    }
-		
-        m->item().s_voidp = StringValuePtr(rv);
-	}
-	break;
-      case Marshall::ToVALUE:
-	{
-	    char *p = (char*)m->item().s_voidp;
-	    if(p)
-                *(m->var()) = rb_str_new2(p);
-	    else
-                *(m->var()) = Qnil;
-	    if(m->cleanup())
-		delete[] p;
-	}
-	break;
-      default:
-	m->unsupported();
-	break;
-    }
-}
-*/
-
 void marshall_ucharP(Marshall *m) {
   marshall_it<unsigned char *>(m);
 }
-/*
 
-    switch(m->action()) {
-      case Marshall::FromVALUE:
-	{
-	    VALUE rv = *(m->var());
-	    if(rv == Qnil) {
-		m->item().s_voidp = 0;
-		break;
-	    }
-        m->item().s_voidp = StringValuePtr(rv);
-	}
-	break;
-      case Marshall::ToVALUE:
-      default:
-	m->unsupported();
-	break;
-    }
-}
-*/
 static void marshall_doubleR(Marshall *m) {
 	switch(m->action()) {
 	case Marshall::FromVALUE:
@@ -933,20 +880,6 @@
 	return rstringFromQString(&s);
 }
 
-#if 0
-static const char *not_ascii(const char *s, uint &len)
-{
-    bool r = false;
-    for(; *s ; s++, len--)
-      if((uint)*s > 0x7F)
-      {
-        r = true;
-        break;
-      }
-    return r ? s : 0L;
-}
-#endif
-
 void marshall_QDBusVariant(Marshall *m) {
 	switch(m->action()) {
 	case Marshall::FromVALUE: 
--- trunk/KDE/kdebindings/qtruby/rubylib/qtruby/marshall_basetypes.h #642097:642098
@@ -46,8 +46,7 @@
 template <> 
 static void marshall_from_ruby<char *>(Marshall *m) 
 {
-	VALUE obj = *(m->var());
-	m->item().s_voidp = ruby_to_primitive<char*>(obj);
+	m->item().s_voidp = ruby_to_primitive<char*>(*(m->var()));
 }
 
 template <>
--- trunk/KDE/kdebindings/qtruby/rubylib/qtruby/marshall_primitives.h #642097:642098
@@ -190,12 +190,8 @@
 {
 	if(rv == Qnil)
 		return 0;
-	
-	int len = RSTRING(rv)->len;
-	char* mem = (char*) malloc(len+1);
-	memcpy(mem, StringValuePtr(rv), len);
-	mem[len] ='\0';
-	return (char*) mem;
+
+	return StringValuePtr(rv);
 }
 
 template <>



More information about the Kde-bindings mailing list