Monday, August 20, 2007

How to do finally in C++

A couple of days ago, I was trying to add some new functionality to zoloFighter, and run into some trouble with the
__try{
} finally{
}

block, VC++ complained about some unfolding stuff. The __try{}__finally{} block is used to clean up the javascript engine, so even if some exception has been thrown, the engine don't leak memory.

I don't want to remove the try/finally block, but I am tired tweaking my code around to fit it. So some internet search later, I found a post with a great idea (sorry, I lost the post, because I was too lazy to blog right there).

allocate resource
class doFinally{
public:
~doFinally{
release resource
}
};

{
doFianlly f();
do your stuff;
}

Very smart. Just want to make a record about it.

No comments: