How to fix leaking this in constructor - memory-leaks

i have a class
class Foo {
private static Foo foo_obj = null;
public Foo() {
if (foo_obj == null) {
foo = this;
}
}
}
I get a warning about leaking this, how would i go about fixing it.

It looks like you are trying to use the singleton pattern. It's an odd way of doing it though and doesn't work. Because each time Foo() gets instantiated, you create a new one. Why not use a more traditional approach like:
class Foo {
private static Foo foo_obj = new Foo();
private Foo() {}
public static Foo getInstance() { return Foo(); }
}
There are many variations of this pattern on wikipedia.

Related

Is AutoMapper expected to silently fail when mapping the same type without a map configured?

Consider the following:
public class Foo
{
public int Bar { get; set; }
}
[Test]
void Main()
{
var mapper = new MapperConfiguration(c => c.AddProfile<EmptyProfile>()).CreateMapper();
var foo = new Foo(){Bar = 1};
var baz = new Foo();
mapper.Map(foo, baz); // does not throw AutoMapperMappingException
Assert.AreEqual(foo.Bar, baz.Bar); // fails
}
Is this expected behavior using 10.0.0?
According to the maintainers, this is by design.
That's by design. But check
https://docs.automapper.org/en/latest/Configuration-validation.html#custom-validations.
from https://github.com/AutoMapper/AutoMapper/issues/3480

Creating anonymous object with inline function. Does it contain a leak of Enclosing Class?

As you know each anonymous object in java contains hidden reference to enclosing class.
But with kotling things get more complicated.
Lambda is another representation of anonymous class, but in kotlin it compiles not straightforward, because if lambda didn't capture a reference of enclosing class explicitely than it would be compiled like nested, not inner class (anonymous class) and is safe from the leak.
But what about inline functions. Consider the code below
class A {
fun test(){
val it = withReference {
//todo make sth
}
}
}
inline fun withReference(crossinline action: () -> Unit) = object: Reference {
override fun method1() {
action()
}
override fun method2() {
}
}
interface Reference {
fun method1()
fun method2()
}
As i know inline function would be compiled like non-wrapped code to the A class, so the question is open.
Does the anonymous object: Reference contain a link to enclosing class A, which could lead to a memory leak?
PS: i have read this article, but it doesn't contain an answer to my case
I used the decompiler of IntelliJ and there is no reference to the outer A
public final class A$test$$inlined$withReference$1 implements Reference {
public void method1() {
}
public void method2() {
}
}
If the lambda references a variable from the outer class A like this:
class A {
val valFromA = 10;
fun test(){
val it = withReference {
println("use $valFromA")
}
}
}
Then the decompiler shows the reference to the A object:
public final class A$test$$inlined$withReference$1 implements Reference {
// $FF: synthetic field
final A this$0;
public A$test$$inlined$withReference$1(A var1) {
this.this$0 = var1;
}
public void method1() {
String var1 = "use " + this.this$0.getValFromA();
System.out.println(var1);
}
public void method2() {
}
}
If you think about it, the withReference function has no way of referring to the outer scope that it gets inlined into, therefore it has no reason to contain a reference to the scope that it's called from. You don't even know what class it's being called in, or if it's even called inside a class, for that matter.
For this specific case, here's the decompiled and simplified bytecode of the withReference function:
public static Reference withReference(final Function0 action) {
return new Reference() {
public void method1() {
action.invoke();
}
public void method2() {
}
};
}
At the places where it gets inlined, there's of course no call to this function, this one is for Java interop only. Kotlin call sites all get their own class generated to represent this object depending on what code you pass into the action parameter. For your call of the test function, a class like is generated:
public final class A$test$$inlined$withReference$1 implements Reference {
public void method1() {
//todo make sth
}
public void method2() {
}
}
And this is what's instantiated in the test method:
public final class A {
public final void test() {
Reference it = new A$test$$inlined$withReference$1();
}
}

Execute an objects method without making a new instance

How can I execute Print from within TestObject?
class Program
{
private int Value;
static void Main()
{
TestObject test = new TestObject();
Program p1 = new Program();
Program p2 = new Program();
p1.Value = 1;
p2.Value = 2;
p1.Print();
p2.Print();
}
private void Print()
{
Console.Write(Value.ToString());
Console.ReadKey();
}
}
class TestObject
{
// How to execute p1.Print here?
}
There are multiple ways to do this:
Pass Program directly to TestObject
Pros:
Simple change
Cons:
You will have to make Print public
You will expose other things in Program
You're coupling TestObject to Program directly
Here's sample code:
class Program
{
static void Main()
{
TestObject test = new TestObject(this);
}
public void Print()
{
Console.Write(Value.ToString());
Console.ReadKey();
}
}
class TestObject
{
public TestObject(Program p)
{
p.Print();
}
}
Pass a delegate to TestObject
Pros:
Simple change
Doesn't have to make Print public
Only exposes 1 method to TestObject
Cons:
The coupling is TestObject wants to do something, not TestObject wants access to something that does something
Here's sample code:
class Program
{
static void Main()
{
TestObject test = new TestObject(() => Print());
}
private void Print()
{
Console.Write(Value.ToString());
Console.ReadKey();
}
}
class TestObject
{
public TestObject(Action print)
{
print();
}
}
Implement an interface in Program and pass it to TestObject
Pros:
Only exposes what the interface exposes
Easier to implement other places (better to say "need this interface" than "needs a delegate", clearer contract specification)
No coupling to a specific type, coupling is to any object that meets certain criteria - implements an interface
Cons:
None relevant (in my opinion)
Here's sample code:
interface IPrintable
{
void Print();
}
class Program : IPrintable
{
static void Main()
{
TestObject test = new TestObject(this);
}
public void Print()
{
Console.Write(Value.ToString());
Console.ReadKey();
}
}
class TestObject
{
public TestObject(IPrintable p)
{
p.Print();
}
}
Conclusion: My advice would be to pick the interface way. Clearer design, easier to extend without having multiple delegates being passed around.

ArrayList with Objects which implements an Interface

I want a ArrayList, where you can add Objects , which implements an Interface.
Something like this:
ArrayList<Object implements Interface> list =
new ArrayList<Object which implements a specific Interface>();
What is the correct syntax for this?
Just set the interface as the type of the generic. Here you go the code in C#:
interface IFoo {
void foo();
}
class Foo1 : IFoo {
public void foo() {
Console.WriteLine("foo1");
}
}
class Foo2 : IFoo {
public void foo() {
Console.WriteLine("foo2");
}
}
class Program {
public static void Main() {
// IFoo type: any object implementing IFoo may go in
List<IFoo> list = new List<IFoo>();
list.Add(new Foo1());
list.Add(new Foo2());
foreach(IFoo obj in list) obj.foo(); // foo1, foo2
Console.ReadKey();
}
}

Adding static method to IronPython scope

Assume I have the following code:
public static class Foo
{
public static void Bar() {}
}
In IronPython, I would like to have:
Bar()
Without having to include the Foo on the line. Now, I know I can say:
var Bar = Foo.Bar
Bar()
But I would like to add Bar to the ScriptScope in my C# code using SetVariable. How can I do this?
Create delegate to method and set in to scope.
public class Program
{
public static void Main(string[] args)
{
var python = Python.CreateEngine();
var scriptScope = python.CreateScope();
scriptScope.SetVariable("Print", new Action<int>(Bar.Print));
python.Execute(
"Print(10)",
scriptScope
);
}
}
public static class Bar
{
public static void Print(int a)
{
Console.WriteLine("Print:{0}", a);
}
}

Resources