Click here to Skip to main content
15,888,212 members
Please Sign up or sign in to vote.
2.22/5 (3 votes)
See more:
Hi,

Is it possible to free all unmanaged resources in the middle of the windows application run,
I'm running a C#.NET 2.0 application as it is causing an issue .

Thanks,
Shreyas
Posted
Updated 24-Sep-13 11:33am
v2
Comments
Sergey Alexandrovich Kryukov 24-Sep-13 17:33pm    
Why on Earth?!
—SA
Matt T Heffron 24-Sep-13 17:38pm    
+4 for the question to offset the 1.
The question itself is reasonable.
The proposed/asked approach to the symptom is not reasonable.

If you're asking how to clean up thing you've done wrong in the middle of the application, then NO, you can't. Well, not without quitting your application and launching it again.

The reason being is that you're leaking resources because you orphaned them by letting the pointer to them drop out of scope without properly disposing them in the first place.

The REAL solution to this is to identify the leak, why it's occurring (HINT: YOU didn't do something correctly) and modifying the code to properly clean up after itself.
 
Share this answer
 
Only if you design the use of your unmanaged resources that way. But I think it would be difficult and totally pointless. The reasonable practice is to free the resources as soon as you don't need them. All .NET FCL classes are using unmanaged resources are designed this way.

—SA
 
Share this answer
 
Yes this is possible, I would however consider when and how you release those resources. To correctly free the resources one good way is to have the IDisposable interface implemented.

See this[^] page for an excellent example.

Good luck!
Eduard
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 24-Sep-13 17:35pm    
I don't really think this is what OP is asking about, which hardly can be reasonable. Please see my answer.
—SA
If you're asking how to clean up thing you've done wrong in the middle of the application, then NO, you can't. Well, not without quitting your application and launching it again.

The reason being is that you're leaking resources because you orphaned them by letting the pointer to them drop out of scope without properly disposing them in the first place. Without the pointer, there's no way to properly free the unmanaged resource. There is no way to get that pointer back once you've lost it!

The REAL solution to this is to identify the leak, why it's occurring (HINT: YOU didn't do something correctly) and modifying the code to properly clean up after itself.
 
Share this answer
 
Comments
Matt T Heffron 24-Sep-13 20:31pm    
Dave, you managed to get two copies of your solution...
Dave Kreskowiak 24-Sep-13 21:45pm    
Yeah, I noticed the hamsters have been refusing to take their medication today.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900