From: Matthias Lampert Date: 2001-06-21T22:00:56+09:00 Subject: [ruby-talk:16702] Re: embedding C++ Hello, Chris! Also las ich bei Chris Uzdavinis [mailto:chris@atdesk.com] > > [...] > > There are some important issues to remember if you're writing code by > hand. The biggest problem that needs to always be on your mind is > when a Ruby exception is thrown. > > Since Ruby exceptions use longjmp, the stack is not unwound like C++ > programmers expect. Local variables will NOT have their destructors > invoked. Therefore you must always be very careful to protect your > objects. HooaMooa! That's an issue worth smoking 10 pipes over, indeed. I would have certainly been swallowed by this kind of quicksand. Thank you for warning me! > There are a few ways to do this. One slowish way is to > heap-allocate all your C++ objects and keep track of them in a global > structure. You can stuff these objects into Ruby objects and let the > Ruby garbage collector take care of the memory for you if you'd like. > (But you still need to keep track of those ruby objects somewhere so > you can mark them for the garbage collector while you're still using > them.) That's certainly the easiest solution. By the way, Java has become tremendously popular among coders despite or even for handling matters just like that. > Another thing you can do is create local scopes around local variables > in such a way that they always go out of scope before any ruby > function that can possibly "throw" is called. This is a variation of > the design rule of "don't allow ANY stack-based variables to exist who > need a destructor invoked to exist when you call ruby functions." That seems to me the most performative solution, and as far as I can see, it's also the cleanest. > A final way to accomplish this may be to use setjmp yourself in each > of your C++ functions, however I have not experimented at all with > this approach and it sounds like a high-overhead solution. I don't know yet what calling setjmp implies. As for my own code, I could manipulate XEmacs to automatically produce markers in my methods that can be handled by some useful tool (Ruby, for instance) to be converted into the appropriate C++ code. Just two other lines in the Makefile ought to do the job in time. Of course, this can't easily be done on third-party code... And what about third-party C++ code that throws exceptions itself? ... Honestly, I consider obtaining some more tobacco! > > I've been thinking about writing up the issues I've encountered, but I > still don't have a good enough solution to where I'm convinced it's > worth writing about. > That's not the issue about writing, especially not in the OS community. Some German success trainer said: If you have a problem you can't solve, tell the world that there's a problem and it WILL be solved. That's what I did here, and I want to thank all those who gave me all those precious hints. CU, -- Hippo (aka Matthias Lampert, Hamburg) Just another XP and Ruby fan