在以下代码中获得上述错误.如何纠正它.谢谢.请找
protected override void Finalize() { Dispose(false); }
Run Code Online (Sandbox Code Playgroud)
在下面的代码中.
using Microsoft.Win32;
using System.Runtime.InteropServices;
public class Kiosk : IDisposable
{
#region "IDisposable"
// Implementing IDisposable since it might be possible for
// someone to forget to cause the unhook to occur. I didn't really
// see any problems with this in testing, but since the SDK says
// you should do it, then here's a way to make sure it will happen.
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual …Run Code Online (Sandbox Code Playgroud) 在某些情况下,如何处理类似于以下类的所有异常?
class Test : IDisposable {
public Test() {
throw new Exception("Exception in ctor");
}
public void Dispose() {
throw new Exception("Exception in Dispose()");
}
~Test() {
this.Dispose();
}
}
Run Code Online (Sandbox Code Playgroud)
我试过这个,但它不起作用:
static void Main() {
Test t = null;
try {
t = new Test();
}
catch (Exception ex) {
Console.Error.WriteLine(ex.Message);
}
// t is still null
}
Run Code Online (Sandbox Code Playgroud)
我也尝试使用"使用"但它不处理从~Test()抛出的异常;
static void Main() {
try {
using (Test t = new Test()) { }
}
catch (Exception ex) {
Console.Error.WriteLine(ex.Message);
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试从我的云提供商(IBM 云私有)配置/取消配置服务实例/绑定,目前,存在一个错误,如果该服务未在 ICP 中取消配置,则会导致我的 ICP 环境中的孤立服务实例即使使用强制选项,我也无法删除。他们提供了以下解决方法:
kubectl edit ServiceInstance <service-instance-name>
kubectl edit ServiceBinding <service-binding-name>
Run Code Online (Sandbox Code Playgroud)
然后删除该行:
...
finalizers:
- kubernetes-incubator/service-catalog
...
Run Code Online (Sandbox Code Playgroud)
并且孤立服务实例/绑定将被正确删除。我想知道如何使用 bash cli(实时编辑 + 删除行 + 保存 + 退出)或任何其他方式自动执行此过程。
我正在使用 Mysql GET_LOCK在分布式系统中实现锁定服务。在调用我的 getLock() 方法时,如果客户端获得了锁,我会在数据库中创建一个条目并在释放锁时删除该条目。
假设调用客户端将在达到其目的后释放锁。但是,我想确保在客户端不释放它或不进行适当清理的情况下释放锁。
一种方法是在我的锁定对象上使用 finalize 方法以在调用 finalize 时释放它。然而,它并不理想,增加了复杂性并且在 Java 9 中被弃用。我读到了比终结器更好的 Phantom 引用,但它的复杂程度也很高。我把它作为我的最后手段。
有没有更简单、更少依赖 JVM 的方法来处理这个用例?
java garbage-collection phantom-reference finalize finalizer
我有2个程序集,A包含Main方法和Foo类,它使用Bar来自程序集B的类:
杆组件(组件B):
public sealed class Bar : IDisposable {
/* ... */
public void Dispose() { /* ... */ }
}
Run Code Online (Sandbox Code Playgroud)
Foo类(程序集A):
public class Foo : IDisposable {
private readonly Bar external;
private bool disposed;
public Foo()
{
Console.WriteLine("Foo");
external = new Bar();
}
~Foo()
{
Console.WriteLine("~Foo");
this.Dispose(false);
}
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposed) return;
if (disposing) external.Dispose();
disposed = true;
}
}
Run Code Online (Sandbox Code Playgroud)
入口点(在程序集A中):
class Program
{
static void Main(string[] …Run Code Online (Sandbox Code Playgroud) 我重写了finalize()方法,在重新声明对象的内存空间时做了一些工作.但是有人说我必须finalize()在覆盖方法时调用父finalize()进程.但是构造函数自动调用超类构造函数而不像finalize()方法.
你能告诉我一件事......?
我知道当垃圾收集确定没有对该对象的更多引用时,垃圾收集器会在对象上调用Java finalize方法.
应用程序退出后,Java finalize()方法是否会执行?
因此,默认的处置模式实现如下所示:
class SomeClass : IDisposable
{
// Flag: Has Dispose already been called?
bool disposed = false;
// Public implementation of Dispose pattern callable by consumers.
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
// Protected implementation of Dispose pattern.
protected virtual void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing) {
// Free any other managed objects here.
}
// Free any unmanaged objects here.
disposed = true;
}
~SomeClass()
{
Dispose(false);
}
}
Run Code Online (Sandbox Code Playgroud)
据说:
如果方法调用来自终结器(也就是说,如果处理为
false),则仅执行释放非托管资源的代码。因为未定义垃圾回收器在完成过程中销毁托管对象的顺序,所以 …
我想测试垃圾收集器,但这样做很困难。
我写了以下琐碎的测试代码:
using System;
class Foo
{
int i;
public Foo(int v)
{
i = v;
Console.WriteLine($"{i} was born");
}
~Foo()
{
Console.WriteLine($"{i} has died");
}
}
public class Program
{
[STAThread]
public static void Main(string[] args)
{
Foo n1 = new Foo(1);
Foo n2 = new Foo(2);
Foo n3 = new Foo(3);
Foo n4 = new Foo(4);
Console.WriteLine("built everything");
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true, true);
GC.WaitForPendingFinalizers();
System.Threading.Thread.Sleep(1000);
n1 = null;
n2 = null;
n3 = n4;
Console.WriteLine("deref n1..n3");
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true, …Run Code Online (Sandbox Code Playgroud) 在覆盖Object.Finalize()的类的存在下,我似乎无法理解GC.Collect()的行为.这是我的基本代码:
namespace test
{
class Foo
{
~Foo() { Console.WriteLine("Inside Foo.Finalize()"); }
}
static class Program
{
static void Main()
{
{
Foo bar = new Foo();
}
GC.Collect();
GC.WaitForPendingFinalizers();
Console.ReadLine();
}
}
}
Run Code Online (Sandbox Code Playgroud)
与我期望的相反,我只在程序终止时获得控制台输出而不是之后 GC.WaitForPendingFinalizers()
为什么不应该这样做?当执行垃圾收集时将调用终结器,为什么我们不能在这里添加持久性相关代码?
只是想了解为什么destructor在实例被处置后在 USING 的范围之外被调用。我知道不需要destructor何时IDisposable实施。这是我的例子:
using System;
namespace Destructor
{
class Program
{
static void Main(string[] args)
{
using (var a = new A())
{
Console.WriteLine("... inside USING ...");
}
Console.WriteLine("...END...");
}
}
//............................................................
class A : IDisposable
{
public A()
{
Console.WriteLine("...Constructor called...");
}
~A()
{
Console.WriteLine("...Destructor called...");
}
public void Dispose()
{
Console.WriteLine("...Dispose called...");
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
...构造函数调用...
... 里面使用 ...
...处置称为...
...结尾...
...析构函数调用...
finalizer ×13
c# ×7
java ×4
idisposable ×3
.net ×2
destructor ×2
apache-flex ×1
dll ×1
finalize ×1
jdk1.6 ×1
kubernetes ×1
roslyn ×1