Thread: C++/CLI arrogance

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607

    C++/CLI arrogance

    Well I just started reading my new book, Pro Visual C++/CLI and the .NET 2.0 platform. It is written by Stephen R. G. Fraser, one of the 4 key people in the devlopment of C++/CLI.

    Right from the start the book is leaving a very Microsoftie taste in my mouth. I have no qualms about attempting to modernize C++ to meet with current demands as that will contribute to it's lifespan. However, I do have a problem with the idea that any code not written for the .NET (CLI) platform being called 'unsafe' code. For instance the book claims any code written in traditional C++ is now 'unsafe' code. We all go to great lengths to ensure our C++ code is indeed memory and thread safe and then for someone to tell us it is 'unsafe' simply because it's ISO-C++ and not C++/CLI (a Microsoftie way of pushing pure .NET 2.0), doesn't make me all that excited about the direction we are heading.

    Check out this code sample:
    Code:
    ref class Wrapper {
      
      Native *pn;
    
    public:
    
      //RAII
      Wrapper(int val) {pn=new Native( val ); }
      ~Wrapper() {delete pn; }
    
      void mfunc();
    
    protected:
    
      //an explicit Finalize() method - as a failsafe ...    
      ! Wrapper() {delete pn; }
      
    };
    
    void f1()
    {
      //normal treatment of a reference type
      Wrapper^ w1=gcnew Wrapper(1024);
    
      //mapping a reference to a lifetime
      Wrapper w2(2048);  //no ^ token !
    
      //just illustrating a semantic difference
      w1->mfunc(); w2.mfunc();
    
      //w2 is disposed of here
    
    }
    
    // ...later, w1 is finalized at some point, maybe ...
    I see some very odd things going on in this code and now I see the reasons that some folks out there totally dislike CLI. One article I read talks about Finalize() and now I see why.

    Ok, let's get this straight. When the object is destroyed, the destructor will be called according to traditional 'unsafe' C++. The destructor then will clean up. So what the heck is Finalize() for? Does Microsoft not understand fundamental memory management in C++? If the object cleans up upon destruction, what the hell is Finalize() gonna do later? There is nothing left to clean up. It seems pointless.

    Secondly, notice the CLI reference ^w1. It is constructed with gcnew, and I assume this is also instantiation, but it is never cleaned up. In fact the last statement is saying the garbage collector will clean it up ....when it feels damn good and ready to do so - if it ever feels damn good and ready. So let's say we have 10 apps up that have 30 or so w1's out there. Since the GC only cares about the current process - a fact that blows my mind in a cooperative pre-emptive multi-tasking environment, it may never clean up any of those objects. So now instead of having 1 or 2 poorly coded C++ apps here and there, now we have 10 apps using the VM that don't care about each other, the system, or anything not related to itself. Seems to me a virtual mess. So as computers get faster, our software gets slower. And for what? So more people can code without using their brain? I don't like the tradeoff. Whoever said code was easy or simple? So because of our beloved new technology called CLI or .NET 2.0, we have an instant leak and it is called 'correct' and 'safe'. This is hideous memory management and is akin to Java and C# memory management, both problem-plagued and doomed memory managers if you ask me.
    So if the w1 reference is never cleaned up, what the heck happens to it? Are we to assume that the user's system has so much memory that we need not worry about little old w1?

    I'm not a professional programmer by any means, but I'm also not an idiot in C++. So far this CLI .NET stuff doesn't look faster or better, it looks bloated and slow. I'm going to read this book in its entirety and give this CLI a chance, but I cringe at the Microsoftie jargon, propoganda, and insistence on 'our way is better, not because it is, but because it's our way'.

    A link to ponder and lead to more links. A quick google of CLI vs C++ would probably yield hours of reading.
    http://www.sysinternals.com/blog/200...im-scared.html

    Everyone's response is 'dude, you get like memory management taken care of for ya'.
    I'm like 'dude, if you can't perform your own memory management, why the hell are you programming?'

    It's pretty simple in C++ folks. If you call new, then by all means call delete when you are done. Damn that was hard eh.


    Comments welcome.

    <confuzzled>
    Last edited by VirtualAce; 07-27-2006 at 12:31 AM.

Popular pages Recent additions subscribe to a feed