To drop or not to drop?

Because people are already speculating about my sudden death, here is a short new blog entry. :) (There'll be a more interesting one tomorrow, I hope)

A frequently asked question I've received several times by mail now: Using irrklang's reference counted objects, when do I have to use drop() and when not?
The answer is very easy: Just as in Irrlicht, you only need to call drop() to an object if you received it by calling a method starting with 'create' or when creating it yourself via the C++ 'new'.
After you don't need the object anymore, just call drop() to let the reference counter handle the object descruction automaticly. There is one little single exception to this rule in irrKlang: If you recieved a pointer to an ISound interface, you always have to call drop() after you don't need it any more. Why?
ISound objects are the only public objects which are pushed and used between multiple threads, and you can never know if another thread already has deleted it in between. That's why ISound is also derived from IVirtualUnknown instead of IUnknown.
So maybe a more future prove rule would be: If you get a pointer to an object derived from IVirtualObject, always call drop() if you don't need it anymore. But don't despair: If you are still unsure, just don't call drop() temporarily. Nothing really bad will happen then, you will only waste some small amount of memory used for storing some file names and positions etc.
IrrKlang will still free huge and critical system resources itself in this situation automaticly behind the scenes. Going to add this to the FAQ soon.

eight comments, already:

Hes alive ;)

Just kiddin… good to hear something new
Halan - 15 01 07 - 23:34

Why not make the reference counting automatic? Having a manual drop() sounds just like Release() in COM & D3D, and their excuse for making it manual was that it’s compatible with C as well as C++. If you’re aiming at C++ it’s much better to just make it all automatic – ie when the reference goes out of scope, it’s released. Then you get no double-release or failure-to-release issues or confusion over when / if you have to release. boost::shared_ptr is a good example.
Steve () (link) - 16 01 07 - 10:41

That would mean that niko would have to rewrite half of irrlicht ;)
Fre4LeTy - 16 01 07 - 11:09

When are we going to rename IUnknown to IReferenceCounter… IUnknown makes no sense… IReferenceCounter…makes sense.
RabidLockerGnome - 16 01 07 - 14:58

When I started with Irrlicht some years ago I decided to use manual reference counting instead of smart_pointers for various reasons. Don’t know how often I defended this approach since then :) I think the main reason was that if you are using smart pointers, you still have to know about what is happening inside, and you can run into trouble if you don’t. If you force people to call grab() and drop() themselves, they are forced to think a bit about it, and no one prevents them to use smart pointers anyway then. :)
irrKlang is using the same interface for simple compatibility with Irrlicht of course. And yep, IReferenceCounter or ..ed would be an idea, originally I planned to populate IUnknown with MetaData-stuff similar to the debugdata in it currently.
niko - 16 01 07 - 18:00

Fair enough, your choice :) We’ve used an auto reference counter (scope based) for some years without any real problems though, yeah people have to be aware of what they do, but in the grand scheme of things it’s harder to make mistakes than it is with manual reference counting, since the default behaviour tends to be ‘safe’ (no need for docs telling people when / if to Release()). But yeah, nothing is totally idiot-proof ;)
Steve () (link) - 16 01 07 - 19:30

Is the reference counting used by the [i]ISound[/i] derived class thread safe? If you are calling grab() or drop() from your threads, the reference count needs to be safely managed.
vitek - 17 01 07 - 05:10

yep, it is threadsafe. That’s why its IVirtualUnknown :)
niko - 17 01 07 - 07:51


Name:  
Remember personal info?
yes
no
Email (optional):
URL (optional):
Enter "layered" (antispam):
Comment:Emoticons / Textile

  ( Register your username / Log in )

Notify: Yes, send me email when someone replies.  

Small print: All html tags except <b> and <i> will be removed from your comment. You can make links by just typing the url or mail-address.
Note: If you type in your email adress above, it will be visible to other visitors, although it will be hidden for bots using javaScript.