简体   繁体   中英

Unmanaged Leak in Calling Web service in C#

I need help with my code.

Ive been trying to use RedGate to monitor the memory usage of my application, after hours of testing It pointed out the unmanaged code of my application, and I could only think of my webservice calling as the only or somewhat unmanaged part of my code. Ive been debugging for hours and cant seem to find out where or what really happened. below is my code.

private void btnstart_Click(object sender, EventArgs e)
{
    btnPressed = !btnPressed //boolean
    if(btnPressed)
    {
        myTask = Task.Factory.StartNew(() => 
        {
            do {
                _checkMatches(token.Token);
            } while (token.IsCancellationRequested != true);
        },token.Token);
    }
    else
    {
        token.Cancel();

        try {
            Task.WaitAny(myTask);
        }
        catch(Exception Error) {
            //Put to logs
        }
        finally
        {
            if(myTask.isCancelled || myTask.IsCompleted || myTask.IsFaulted)
            {
                myTask.Dispose();
            } 
        }
    }
}

private void _checkMatches(CancellationToken token) 
{
    try
    {
        if(token.IsCancellationRequested)
        {
            token.ThrowIfCancellationRequested();
        }
        EndpointAddressBuilder ServiceEndPoint = new EndpointAddressBuilder(//Read Endpoint From Notepad);
        ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });//just bypassing validation for test purposes

        // WebService is an ASMX web service
        using (WebService.SoapClient client = new WebService.SoapClient()) 
        {
            WebService.checkResponse clientRes = client.checkClient();
            if(clientRes.response == 1 || clientRes.response == 2) {
                //Put to Logs
            }
        }
    }
    catch(Exception error){
        //Put to logs
    }
}

I cant seem to find any error in this code. Can Someone Help me with what is the problem why my unmanaged code is leaking? or could someone suggest what tools to be used or even suggest to find the leaking part of my code? Any Help would be great.

If you are using RedGate memory profiler it should tell you more than just pointing unmanaged resources. Because you are not directly creating any unamanged resources, most likely your memory leak is caused by a managed object or an event subsciption(or callback subscription).

Standard .net managed objects which have unmanaged resources (ie : WebService.SoapClient) have implemented the finalizer(destructor) in order to get rid of its unmanaged resources in any case if Dispose is not called. If you use a standard .net managed object which has unmanaged resources and don't call the dispose (this is bad), still it will release unamanged resources when it is going through the finalization. Generally this shouldn't cause unmanaged resource memory leak.

How to check if there is any memory leak:

Execute your memory consumption logic a few times. Go to the Instance list and see if there is any instances are growing. Select the 'Objects with Source' option in order to get rid of heaps of system object. You should see more instances that there should be if there is any memory leak.

If there is any growing instances, pick one and see the objects retention graph which will show you exactly which instance holds the reference.

And also, make sure that you have implemented IDisposable properly and dispose all disposable objects and unsubscribe from all event subscriptions.

Have a look at below walkthroughs

http://www.red-gate.com/products/dotnet-development/ants-memory-profiler/walkthrough http://www.red-gate.com/products/dotnet-development/ants-memory-profiler/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM