add buffer flush at exit