Log in

No account? Create an account

Previous Entry | Next Entry

I Counted Them All Back In...

A while ago I mused on programming languages (as programmers are wont to do), and concluded that the best thing about languages like Python (and, I now admit, Java) is the freedom from all the hideous memory management that Some Other Languages have to do.

Raymond Chen's blog has an interesting example of querying information from an Explorer window.  Interesting not for the way in which it introspects down through the multiple-interface objectery[1] of Windows to get at the data, but because it exemplifies, for me, the reasons I dislike C++, and Windows C++, so much.

Take a look (should you feel like it) at his final example (at the end of that page).  He excuses the ugliness by saying "because I put everything into one huge function instead of breaking them out into subfunctions", and indeed, this is partly an excuse.  But look at the actual code; that ever-deepening cascase of nested ifs matched by the sudden flurry of CoTaskMemFree and Release calls.  I have to wonder how much simpler this would be in a dynamic language, with grown-up memory management.  But, as Raymond often points out, that's legacy for ya.

The syntax of C++ also grates, after a good couple of years now with the simpler, more streamlined Python; stuff like if (SUCCEEDED(pwba->QueryInterface(IID_IServiceProvider, (void**)&psp))) just... looks ugly (though I can still read it perfectly well), but I think it's the sheer effort required to do anything via COM and the Windows API that I remember - the slow trudge through looking up IIDs, playing with QueryInterface and trying to work out who owned what.  It was a lot of work for what was often very little effect and I don't miss it at all.

But then I'm a curmudgeonly old Hector at the best of times...

[1] I don't know if this is a real word, but it should be, shouldn't it?  Like an orangery is to oranges.


( 4 comments — Leave a comment )
(Deleted comment)
Jul. 29th, 2004 07:19 am (UTC)
I preferred the previous photo, Grimmtooth!

Interesting point; I'd rather have all my memory management in one place (ie, the interpreter and/or GC) which is used in systems all over the world than have it scattered through my code in lots of little explicit allocate/release calls :) I think the chances of me getting a couple of those wrong are a lot higher than there being a major memory snafu in Python...

I should really have explained "curmudgeonly old Hector" for non-Brits...

Jul. 29th, 2004 11:01 am (UTC)
Baked in memory management is not a panacea
We just today found an issue in one of our products because of baked in memory management and garbage collection. It turns out that a resource was being held because the garbage collector was taking its time in cleaning stuff up. We had to force a garbage collection to get everything properly taken care of because there is no way to explicitly destroy the objects we are using.

Granted this is on .NET and not Python, Java, etc. using COM interop to some legacy COM objects we have, but the point is that relying on garbage collection can be troublesome if you're not careful.

Also, the problems pointed out in the entry are as much or more due to Windows and COM vs. C++. C++ seems to get a worse rap than it deserves because MFC, COM and the Windows API is most people's exposure to a large collection of C++ classes and interfaces and it is not very well done.

In the general case I do not consider C++ memory management hideous and I think properly utilized is superior to most other management schemes, given that the constructor is called once an object goes out of scope. Unless you are writing low level objects, most C++ needn't have very many instances of memory management, if any at all. The fact that the Windows API and COM make this much more difficult than it needs to be is not C++'s fault.

Also, as noted in the article you reference with the code, the various objects and interfaces were designed for use from 'a scripting language like JScript or Visual Basic'. I don't think C++ is necessarily the best tool for this particular job.

And all this being said, when possible I reach for Python.
Jan. 11th, 2007 11:34 am (UTC)
Is it in adderss that he improves?
Hi all!

( 4 comments — Leave a comment )