question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use Try Catch Finally to dispose garbage

See original GitHub issue

I am having garbage collection issues on my application which is built using Abp 1.5.1. Please is there a way to dispose of garbage using the code below?

try
                {
                        entries = _ventryRepository.GetAll().Where(x => x.CustReference == custRef)
                                .OrderBy(x => x.Id)
                                .Skip((page - 1) * pageSize)
                                .Take(pageSize);
                            longCount = _ventryRepository.GetAll().Where(x => x.CustReference == custRef).LongCount();
                        
                }
                catch (Exception e)
                {
                    return new TransactionHistoryResponse
                    {
                        CustomerId = custRef,
                        StatusCode = 1,
                        Message = message
                    };
                }
            });

What exactly can I dispose of here in a finally() block? I want to dispose of as much possible without breaking the application.

Thank you

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
hikalkancommented, Nov 13, 2017

In general, you should not dispose an object while using dependency injection.

What is your framework, AspNet Core? Can’t you upgrade to latest ABP since your version is a bit old to provide support.

0reactions
KennyEng12commented, Nov 27, 2017

Okay @acjh just did opened a new issue: #2732

Read more comments on GitHub >

github_iconTop Results From Across the Web

Try/Finally block vs calling dispose?
In this case "writer" will point to trash and // The "finally" section will be executed writer = new StreamWriter(.
Read more >
How to execute cleanup code using finally
If an exception is thrown before you can close your connection, using the finally block is better than waiting for garbage collection. See...
Read more >
Try catch, finally throw — or Exception Handling 101 for .NET
The only action you should worry about is resource disposal. If it's about language without garbage collection (e.g. C++), deallocation is ...
Read more >
How assertive should I be in handling exceptions in objects?
Net's garbage collection and exception handling - I want to bulletproof my code and be efficient with objects, but I am trying to...
Read more >
Difference between Finally, Finalize and Dispose in C#
Finalize can only be called by the garbage collector and cannot be by the user. Whereas in Dispose the users must explicitly invoke...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found