[前][次][番号順一覧][スレッド一覧]

ruby-changes:35290

From: nobu <ko1@a...>
Date: Wed, 3 Sep 2014 16:56:27 +0900 (JST)
Subject: [ruby-changes:35290] nobu:r47372 (trunk): object.c: fix memory leak

nobu	2014-09-03 16:56:09 +0900 (Wed, 03 Sep 2014)

  New Revision: 47372

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=47372

  Log:
    object.c: fix memory leak
    
    * object.c (rb_obj_copy_ivar): allocate no memory for empty
      instance variables.  [ruby-core:64700] [Bug #10191]

  Modified files:
    trunk/ChangeLog
    trunk/object.c
    trunk/test/ruby/test_object.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 47371)
+++ ChangeLog	(revision 47372)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Sep  3 16:56:07 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* object.c (rb_obj_copy_ivar): allocate no memory for empty
+	  instance variables.  [ruby-core:64700] [Bug #10191]
+
 Wed Sep  3 12:05:17 2014  Tanaka Akira  <akr@f...>
 
 	* process.c (retry_fork_async_signal_safe): Use vfork() if available.
Index: object.c
===================================================================
--- object.c	(revision 47371)
+++ object.c	(revision 47372)
@@ -270,8 +270,11 @@ rb_obj_copy_ivar(VALUE dest, VALUE obj) https://github.com/ruby/ruby/blob/trunk/object.c#L270
     }
     else {
 	long len = ROBJECT(obj)->as.heap.numiv;
-	VALUE *ptr = ALLOC_N(VALUE, len);
-	MEMCPY(ptr, ROBJECT(obj)->as.heap.ivptr, VALUE, len);
+	VALUE *ptr = 0;
+	if (len > 0) {
+	    ptr = ALLOC_N(VALUE, len);
+	    MEMCPY(ptr, ROBJECT(obj)->as.heap.ivptr, VALUE, len);
+	}
 	ROBJECT(dest)->as.heap.ivptr = ptr;
 	ROBJECT(dest)->as.heap.numiv = len;
 	ROBJECT(dest)->as.heap.iv_index_tbl = ROBJECT(obj)->as.heap.iv_index_tbl;
Index: test/ruby/test_object.rb
===================================================================
--- test/ruby/test_object.rb	(revision 47371)
+++ test/ruby/test_object.rb	(revision 47372)
@@ -811,4 +811,14 @@ class TestObject < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_object.rb#L811
     assert_raise_with_message(TypeError, "can't convert Array into Integer") {Integer([42])}
     assert_raise_with_message(TypeError, 'no implicit conversion of Array into Integer') {[].first([42])}
   end
+
+  def test_copied_ivar_memory_leak
+    bug10191 = '[ruby-core:64700] [Bug #10191]'
+    assert_no_memory_leak([], <<-"end;", <<-"end;", bug10191, rss: true)
+      def (a = Object.new).set; @v = nil; end
+      num = 500_000
+    end;
+      num.times {a.clone.set}
+    end;
+  end
 end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]