From c0868b9caa1982d946531b7eb64d4ee6277e8e77 Mon Sep 17 00:00:00 2001
From: stepan <stepan.sindelar@oracle.com>
Date: Tue, 5 Dec 2017 15:19:50 +0100
Subject: [PATCH] Update reference count of attrs when copying them

---
 .../r/runtime/data/RAttributesLayout.java        | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RAttributesLayout.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RAttributesLayout.java
index ef5fb88f7c..6245346502 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RAttributesLayout.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RAttributesLayout.java
@@ -146,7 +146,21 @@ public final class RAttributesLayout {
     public static DynamicObject copy(DynamicObject attrs) {
         assert isRAttributes(attrs);
 
-        return attrs.copy(attrs.getShape());
+        DynamicObject result = attrs.copy(attrs.getShape());
+        Shape shape = result.getShape();
+        Property prop = shape.getLastProperty();
+        while (prop != null) {
+            Object value = result.get(prop.getKey());
+            if (value instanceof RSharingAttributeStorage) {
+                // There is no simple way to determine the correct reference count here and since
+                // the value will end up in two attributes collections, it will end up being shared
+                // most likely anyway.
+                ((RSharingAttributeStorage) value).makeSharedPermanent();
+            }
+            shape = shape.getParent();
+            prop = shape.getLastProperty();
+        }
+        return result;
     }
 
     @TruffleBoundary
-- 
GitLab