Re: [Jastadd] Beaver + fullCopy()

From: Jesper Mattsson <jesper.mattsson_at_modelon.com>
Date: Wed, 26 Sep 2012 09:19:41 +0000

Hi Jesper

It's good that it will be fixed in Beaver. I could see that any workaround in JastAdd would have been awkward at best.
I thought that it was a dead project, but I haven't looked at the forums.

We have a workaround that will do until the new version is released, or perhaps we'll try your patch.

Thank you.

Jesper

Jesper MATTSSON, MSc
Software Developer & IT Administrator

Phone direct: +46 73 324 5909
Email: jesper.mattsson_at_modelon.com<mailto:jesper.mattsson_at_modelon.com>

[Description: Description: Description: Modelon_2011_Gradient_RGB_400]
________________________________
Modelon AB
Ideon Science Park
SE-223 70 Lund, Sweden

Phone: +46 46 286 2200
Fax: +46 46 286 2201


Web: http://www.modelon.com<http://www.modelon.com/>



From: jastadd-bounces_at_cs.lth.se [mailto:jastadd-bounces_at_cs.lth.se] On Behalf Of Jesper Öqvist
Sent: den 20 september 2012 11:37
To: jastadd_at_cs.lth.se
Subject: Re: [Jastadd] Beaver + fullCopy()

Hello again,

We can not currently do anything to "fix" this problem with Beaver in JastAdd. The value field in beaver.Symbol is final. It is required to be set during the parsing as all the parser actions may use this field. For example:

            case 626: // resource_declarations = resource_declaration.a
            {
                    final Symbol _symbol_a = _symbols[offset + 1];
                    final ResourceDeclaration a = (ResourceDeclaration) _symbol_a.value;
                     return new List().add(a);
            }

The problem here lies with the Beaver API and it would be a bad idea to design around this flaw in JastAdd since a new version of Beaver (1.0) will be released in the near future that, from what I've seen in the pre-release version, does not use this final field. Hopefully it will be simple to move to the new Beaver version for existing JastAdd projects.

Developers using Beaver 9.10 or previous together with JastAdd could build a custom version of Beaver. This approach only requires these changes in beaver.Symbol:

--- Java1.4Frontend/beaver/Symbol.java (revision 9319)
+++ Java1.4Frontend/beaver/Symbol.java (working copy)
_at__at_ -11,7 +11,7 @@
 /**
  * Represents a symbol of a grammar.
  */
-public class Symbol
+public class Symbol implements Cloneable
 {
     static private final int COLUMN_FIELD_BITS = 12;
     static private final int COLUMN_FIELD_MASK = (1 << COLUMN_FIELD_BITS) - 1;
_at__at_ -43,7 +43,7 @@
     /**
      * Value assigned to this symbol.
      */
- public final Object value;
+ public Object value;

     /**
      * Numeric symbol ID.
_at__at_ -160,4 +160,10 @@
     {
         return end;
     }
+
+ public Symbol clone() throws CloneNotSupportedException {
+ Symbol copy = (Symbol) super.clone();
+ copy.value = copy;
+ return copy;
+ }
 }

/Jesper

On 08/22/2012 05:16 PM, Jesper Mattsson wrote:
Hi

I have found a problem when using the beaver flag to JastAdd. The beaver.Symbol class has a final field value, that in the default constructor is set to this. When using copy() or fullCopy(), then the node is copied using clone(), and thus the value field is set to a reference to the original node. This leads to strange memory leaks, where old nodes are retained because of this field.

We have had huge problems in Eclipse with memory leaks, and changing ASTNode.clone() to set value to this solves those problems. Doing this requires using reflection (since value is final). Here is one way to do that:

  public ASTNode<T> clone() throws CloneNotSupportedException {
    ASTNode node = (ASTNode)super.clone();
    node.in$Circle(false);
    node.is$Final(false);
    try {
           Field f = beaver.Symbol.class.getField("value");
           new ClearFinalFieldAction().perform(f, node);
    } catch (Exception e) {
           e.printStackTrace();
    }
    return node;
  }

  private static class ClearFinalFieldAction implements PrivilegedExceptionAction {
           private Field field;

           public void perform(Field f, Object o) throws Exception {
                      field = f;
                      AccessController.doPrivileged(this);
                      f.set(o, o);
           }

           public Object run() throws Exception {
                      field.setAccessible(true);
                      return null;
           }
  }

Jesper

Jesper MATTSSON, MSc
Software Developer & IT Administrator

Phone direct: +46 73 324 5909
Email: jesper.mattsson_at_modelon.com<mailto:jesper.mattsson_at_modelon.com>

[Description: Description: Description: Modelon_2011_Gradient_RGB_400]
________________________________
Modelon AB
Ideon Science Park
SE-223 70 Lund, Sweden

Phone: +46 46 286 2200
Fax: +46 46 286 2201


Web: http://www.modelon.com<http://www.modelon.com/>



This email and any attachments are intended solely for the use of the individual or entity to whom it is addressed and may be confidential and/or privileged. If you are not one of the named recipients or have received this email in error, (i) you should not read, disclose, or copy it, (ii) please notify sender of your receipt by reply email and delete this email and all attachments, (iii) Modelon does not accept or assume any liability or responsibility for any use of or reliance on this email.




_______________________________________________

JastAdd mailing list

JastAdd_at_cs.lth.se<mailto:JastAdd_at_cs.lth.se>

https://mail1.cs.lth.se/cgi-bin/mailman/listinfo/jastadd

This email and any attachments are intended solely for the use of the individual or entity to whom it is addressed and may be confidential and/or privileged. If you are not one of the named recipients or have received this email in error, (i) you should not read, disclose, or copy it, (ii) please notify sender of your receipt by reply email and delete this email and all attachments, (iii) Modelon does not accept or assume any liability or responsibility for any use of or reliance on this email.




image001.png
(image/png attachment: image001.png)

Received on Wed Sep 26 2012 - 11:20:07 CEST

This archive was generated by hypermail 2.3.0 : Wed Apr 16 2014 - 17:19:06 CEST