# Buggy code
def _WriteBuffer(self):
    """Writes the buffered event objects to the storage file."""
    if not self._buffer_size:
        return

    stream_name = "plaso_index.{0:06d}".format(self._file_number)
    offset_table = _SerializedDataOffsetTable(self._zipfile, stream_name)

    stream_name = "plaso_timestamps.{0:06d}".format(self._file_number)
    timestamp_table = _SerializedDataTimestampTable(self._zipfile, stream_name)

    if self._serializers_profiler:
        self._serializers_profiler.StartTiming("write")

    stream_name = "plaso_proto.{0:06d}".format(self._file_number)
    data_stream = _SerializedDataStream(self._zipfile, stream_name)
    entry_data_offset = data_stream.WriteInitialize()
    try:
        for _ in range(len(self._buffer)):
            timestamp, entry_data = heapq.heappop(self._buffer)

            timestamp_table.AddTimestamp(timestamp)
            offset_table.AddOffset(entry_data_offset)

            entry_data_offset = data_stream.WriteEntry(entry_data)

    except:
        data_stream.WriteAbort()

        if self._serializers_profiler:
            self._serializers_profiler.StopTiming("write")

        raise

    self._WriteMetadata(
        self._file_number, self._buffer_first_timestamp, self._buffer_last_timestamp
    )

    offset_table.Write()
    data_stream.WriteFinalize()
    timestamp_table.Write()

    if self._serializers_profiler:
        self._serializers_profiler.StopTiming("write")

    # Reset the counters.
    self._count_data_type = collections.Counter()
    self._count_parser = collections.Counter()

    self._file_number += 1
    self._buffer_size = 0
    self._buffer = []
    self._buffer_first_timestamp = sys.maxint
    self._buffer_last_timestamp = -sys.maxint - 1


# Traceback (MemoryError)
2015-10-14 12:58:53,921 [WARNING] (StorageWriter) PID:2384 <multi_process> Unhandled exception in storage writer (PID: 2384).
2015-10-14 12:58:53,926 [ERROR] (StorageWriter) PID:2384 <multi_process>
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/plaso/multi_processing/multi_process.py", line 1184, in _Main
self._storage_writer.WriteEventObjects()
File "/usr/lib/python2.7/dist-packages/plaso/lib/storage.py", line 1645, in WriteEventObjects
self._Close()
File "/usr/lib/python2.7/dist-packages/plaso/lib/storage.py", line 1675, in _Close
self._storage_file.Close()
File "/usr/lib/python2.7/dist-packages/plaso/lib/storage.py", line 874, in Close
self._FlushBuffer()
File "/usr/lib/python2.7/dist-packages/plaso/lib/storage.py", line 431, in _FlushBuffer
self._WriteStream(stream_name, b''.join(proto_str))
MemoryError
2015-10-14 12:59:17,026 [WARNING] (MainProcess) PID:2379 <multi_process> Killing process: Worker_00 (PID: 2385).
2015-10-14 12:59:17,026 [WARNING] (MainProcess) PID:2379 <multi_process> Killing process: Worker_04 (PID: 2392).
Processing completed with errors.

# Fixed code
def _WriteBuffer(self):
    """Writes the buffered event objects to the storage file."""
    if not self._buffer_size:
        return

    stream_name = "plaso_index.{0:06d}".format(self._file_number)
    offset_table = _SerializedDataOffsetTable(self._zipfile, stream_name)

    stream_name = "plaso_timestamps.{0:06d}".format(self._file_number)
    timestamp_table = _SerializedDataTimestampTable(self._zipfile, stream_name)

    if self._serializers_profiler:
        self._serializers_profiler.StartTiming("write")

    stream_name = "plaso_proto.{0:06d}".format(self._file_number)
    data_stream = _SerializedDataStream(self._zipfile, self._path, stream_name)
    entry_data_offset = data_stream.WriteInitialize()
    try:
        for _ in range(len(self._buffer)):
            timestamp, entry_data = heapq.heappop(self._buffer)

            timestamp_table.AddTimestamp(timestamp)
            offset_table.AddOffset(entry_data_offset)

            entry_data_offset = data_stream.WriteEntry(entry_data)

    except:
        data_stream.WriteAbort()

        if self._serializers_profiler:
            self._serializers_profiler.StopTiming("write")

        raise

    self._WriteMetadata(
        self._file_number, self._buffer_first_timestamp, self._buffer_last_timestamp
    )

    offset_table.Write()
    data_stream.WriteFinalize()
    timestamp_table.Write()

    if self._serializers_profiler:
        self._serializers_profiler.StopTiming("write")

    # Reset the counters.
    self._count_data_type = collections.Counter()
    self._count_parser = collections.Counter()

    self._file_number += 1
    self._buffer_size = 0
    self._buffer = []
    self._buffer_first_timestamp = sys.maxint
    self._buffer_last_timestamp = -sys.maxint - 1