Skip to content

Commit

Permalink
[lang] Avoid memory leaks caused by cyclic references in the action p…
Browse files Browse the repository at this point in the history
…rototype provider.

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Oct 3, 2019
1 parent 1db1258 commit e7999e6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

package io.sarl.lang.sarl.actionprototype;

import java.lang.ref.WeakReference;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.common.types.JvmTypeReference;

Expand All @@ -36,7 +38,7 @@ public class InferredStandardParameter {

/** Original parameter.
*/
protected final EObject source;
protected final WeakReference<EObject> source;

/** Name.
*/
Expand All @@ -60,7 +62,7 @@ public class InferredStandardParameter {
* @param type the type of the formal parameter.
*/
public InferredStandardParameter(EObject source, String name, JvmTypeReference type) {
this.source = source;
this.source = new WeakReference<>(source);
this.name = name;
this.type = type;
}
Expand All @@ -70,7 +72,7 @@ public InferredStandardParameter(EObject source, String name, JvmTypeReference t
* @return the source parameter.
*/
public EObject getParameter() {
return this.source;
return this.source.get();
}

/** Replies the name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package io.sarl.lang.sarl.actionprototype;

import java.io.Serializable;
import java.lang.ref.WeakReference;

import com.google.common.base.Objects;
import org.eclipse.xtext.common.types.JvmIdentifiableElement;
Expand All @@ -41,7 +42,7 @@ public class QualifiedActionName implements Cloneable, Serializable, Comparable<

private String resourceID;

private JvmIdentifiableElement declaringType;
private final WeakReference<JvmIdentifiableElement> declaringType;

private String functionName;

Expand All @@ -53,7 +54,7 @@ public class QualifiedActionName implements Cloneable, Serializable, Comparable<
protected QualifiedActionName(String resourceID, JvmIdentifiableElement declaringType, String functionName) {
this.functionName = Strings.emptyIfNull(functionName);
this.resourceID = Strings.emptyIfNull(resourceID);
this.declaringType = declaringType;
this.declaringType = new WeakReference<>(declaringType);
}

/** Replies the ID of the resource.
Expand All @@ -69,7 +70,7 @@ public String getResourceID() {
* @return the container.
*/
public JvmIdentifiableElement getDeclaringType() {
return this.declaringType;
return this.declaringType.get();
}

/** Replies the name of the function.
Expand Down Expand Up @@ -103,8 +104,8 @@ public boolean equals(Object obj) {
final QualifiedActionName k = (QualifiedActionName) obj;
return Objects.equal(this.resourceID, k.resourceID)
&& Objects.equal(
this.declaringType.getQualifiedName(),
k.declaringType.getQualifiedName())
this.declaringType.get().getQualifiedName(),
k.declaringType.get().getQualifiedName())
&& Objects.equal(this.functionName, k.functionName);
}
return false;
Expand All @@ -129,7 +130,7 @@ public String toString() {
* @return the container identifier.
*/
public String getContainerID() {
return this.resourceID + "/" + this.declaringType.getQualifiedName(); //$NON-NLS-1$
return this.resourceID + "/" + this.declaringType.get().getQualifiedName(); //$NON-NLS-1$
}

@Override
Expand All @@ -141,8 +142,8 @@ public int compareTo(QualifiedActionName otherName) {
if (cmp != 0) {
return cmp;
}
cmp = this.declaringType.getQualifiedName().compareTo(
otherName.declaringType.getQualifiedName());
cmp = this.declaringType.get().getQualifiedName().compareTo(
otherName.declaringType.get().getQualifiedName());
if (cmp != 0) {
return cmp;
}
Expand Down

0 comments on commit e7999e6

Please sign in to comment.