Git Product home page Git Product logo

Comments (1)

justinstenning avatar justinstenning commented on June 21, 2024

Thanks,

Someone else pointed this out also, I just didn't have time to address it
yet.

I plan to do some major overhauls of the project in the coming months and
will incorporate your fix (or if you want to submit a fix To github I will
merge it in).

Cheers,
Justin

On Wednesday, 15 January 2014, Remco Ros wrote:

Hey,

I don't know if you're still active on this project, but I have been
playing with your code and found a severe memory leak.

The Screenshot (MarshalByRefObject) object is never released and it
doesn't implement IDisposable.

If capturing a lot of screenshots, memory in the target process keeps
growing.

In my case, this resulted in a OOM exception after ~30 seconds of getting
consecutive screenshots.

Fix is simple, implement IDisposable, and call
RemotingServices.Disconnect(this).

Then, make sure to call Dispose (or the using() construct) in the client
application.

public class Screenshot : MarshalByRefObject, IDisposable
{
private bool _disposed;

public Screenshot(Guid requestId, byte[] capturedBitmap)
{
    _requestId = requestId;
    _capturedBitmap = capturedBitmap;
}

~Screenshot()
{
    Dispose(false);
}

/// <summary>
/// Disconnects the remoting channel(s) of this object and all nested objects.
/// </summary>
private void Disconnect()
{
    RemotingServices.Disconnect(this);
}

[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.Infrastructure)]
public override object InitializeLifetimeService()
{
    //
    // Returning null designates an infinite non-expiring lease.
    // We must therefore ensure that RemotingServices.Disconnect() is called when
    // it's no longer needed otherwise there will be a memory leak.
    //
    return null;

    //var lease = (ILease)base.InitializeLifetimeService();
    //if (lease.CurrentState == LeaseState.Initial)
    //{
    //    lease.InitialLeaseTime = TimeSpan.FromSeconds(2);
    //    lease.SponsorshipTimeout = TimeSpan.FromSeconds(5);
    //    lease.RenewOnCallTime = TimeSpan.FromSeconds(2);
    //}
    //return lease;
}

Guid _requestId;
public Guid RequestId
{
    get
    {
        return _requestId;
    }
}

byte[] _capturedBitmap;
public byte[] CapturedBitmap
{
    get
    {
        return _capturedBitmap;
    }
}

public void Dispose()
{
    GC.SuppressFinalize(this);
    Dispose(true);
}

protected virtual void Dispose(bool disposing)
{
    if (_disposed)
        return;

    Disconnect();
    _disposed = true;
}

}

Hope that helps.


Reply to this email directly or view it on GitHubhttps://github.com//issues/7
.

from direct3dhook.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.