[Mono-list] Destructor calling not working properly under Linux?
Jerzy Bartuszek
koxta@koxta.net
Tue, 11 Jan 2005 19:07:15 +0100 (CET)
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi! I have a problem regarding destroying objects at the end of the
running program. The problem is that not all objects are properly
destroyed (or at least, destructors called):
koxta@limbo lab11 $ mono temp.exe
Which pet do you want to add? (dog, cat, penguin):
dog
Name your pet:
Spike
Which pet do you want to add? (dog, cat, penguin):
cat
Name your pet:
Mele
Which pet do you want to add? (dog, cat, penguin):
penguin
Name your pet:
Tux
Pet number 1
Woof woof!
Pet number 2
Meowwww
Pet number 3
Eeeeeeek!
A dog is dead
As you can see, only the ,,A dog is dead'' message is being displayed.
Please, look at the following source code temp.cs:
koxta@limbo lab11 $ cat temp.cs |more
using System;
abstract public class Pet {
protected string petName;
public Pet(string name) {
petName = name;
}
abstract public void Speak();
public static Pet CreatePet() {
string myString;
string myName;
getName:
Console.WriteLine("Which pet do you want to add? (dog,
cat, penguin): ");
myString = Console.ReadLine();
Pet myPet = null;
switch(myString) {
case "dog":
Console.WriteLine("Name your pet: ");
myName = Console.ReadLine();
myPet = new Dog(myName);
break;
case "cat":
Console.WriteLine("Name your pet: ");
myName = Console.ReadLine();
myPet = new Cat(myName);
break;
case "penguin":
Console.WriteLine("Name your pet: ");
myName = Console.ReadLine();
myPet = new Penguin(myName);
break;
default:
Console.WriteLine("Wrong pet type");
goto getName;
}
return myPet;
}
public static void Main() {
Pet[] myPets = new Pet[3];
for (int i = 0; i<3; i++)
myPets[i] = Pet.CreatePet();
Console.WriteLine();
for (int i = 0; i<3; i++) {
Console.WriteLine("Pet number {0}", i+1);
myPets[i].Speak();
}
}
}
public class Dog: Pet {
public Dog(string name):base(name) {}
public override void Speak() {
Console.WriteLine("Woof woof!");
}
~Dog() {
Console.WriteLine("A dog is dead");
}
}
public class Cat: Pet {
public override void Speak() {
Console.WriteLine("Meowwww");
}
public Cat(string name):base(name) {}
~Cat() {
Console.WriteLine("A cat is dead");
}
}
public class Penguin: Pet {
public override void Speak() {
Console.WriteLine("Eeeeeeek!");
}
public Penguin(string name):base(name) {}
~Penguin() {
Console.WriteLine("A penguin is dead");
}
}
I post my message here, because Windows displayed all three messages
correctly. Do you know how to fix this?
Regards,
Jurek Bartuszek
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)
iD8DBQFB5BXT6l8uJkeTVlMRAnd1AJ9EAeee+N6rsatjgxdZ4nR04DjLFwCeKBRB
cRPTf+e1W9zalOYUpGPzALQ=
=NS5F
-----END PGP SIGNATURE-----