Thread: ref class

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246

    ref class

    I have a ref sealed abstract class (.NET) that all its members are static and it has a static constructor. I wnat to do something at the end of my work with the class, can I have a destructor in some way?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    No, sealed abstract classes cannot have destructors. Since it's both sealed and abstract (or, in C# terms, static), you cannot create an instance of the class. Because you don't have an instance, you'd never run the destructor either. The class will finally go away once the application domain is unloaded. At that point, what could you really want to do?

    On a semi-related note, static events can cause big problems for the garbage collector if you don't unsubscribe from them. Since static events don't go away until the application domain is unloaded and events contain a reference to every object that is subcribed to them, if you don't unsubscribe from them before releasing all references to an object, then at best you'll just have a chunk of unusable memory. At worst, you'll have an object still responding to events that you didn't expect.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    No, sealed abstract classes cannot have destructors.
    So I added a method that should be called when we finished with that calss.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    This isn't the rigth forum to ask about .Net implementations of C++.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    On a semi-related note, static events can cause big problems for the garbage collector if you don't unsubscribe from them. Since static events don't go away until the application domain is unloaded and events contain a reference to every object that is subcribed to them, if you don't unsubscribe from them before releasing all references to an object, then at best you'll just have a chunk of unusable memory. At worst, you'll have an object still responding to events that you didn't expect.

    So if we have
    Object ^ obj = gcnew Object;

    Code:
     obj = nullptr;
    Should be enough for GC right?
    Last edited by siavoshkc; 09-07-2006 at 05:07 PM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  6. #6
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by siavoshkc
    Should be enough for GC right?
    That's enough for that reference. Objects are only collected if all references to them are released. Since events keep a reference to each object that's subscribed to them, just setting all of your explicit references to nullptr isn't enough; you also have to unsubscribe from every event whose lifetime is longer than that of your object. Since static events are around until the application domain is unloaded, that basically means that your objects will be around until the application closes unless you unsubscribe from them.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  7. #7
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    OK, I'll keep that in mind, thanks.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM