# Buggy code
def AddEventObject(self, event_object):
    """Adds an event object to the storage.

    Args:
      event_object: an event object (instance of EventObject).

    Raises:
      IOError: When trying to write to a closed storage file.
    """
    if not self._zipfile:
        raise IOError("Trying to add an entry to a closed storage file.")

    if event_object.timestamp > self._buffer_last_timestamp:
        self._buffer_last_timestamp = event_object.timestamp

    # TODO: support negative timestamps.
    if (
        event_object.timestamp < self._buffer_first_timestamp
        and event_object.timestamp > 0
    ):
        self._buffer_first_timestamp = event_object.timestamp

    attributes = event_object.GetValues()
    # Add values to counters.
    if self._pre_obj:
        self._pre_obj.counter["total"] += 1
        self._pre_obj.counter[attributes.get("parser", "N/A")] += 1
        # TODO remove plugin, add parser chain. Refactor to separate method e.g.
        # UpdateEventCounters.
        if "plugin" in attributes:
            self._pre_obj.plugin_counter[attributes.get("plugin", "N/A")] += 1

    # Add to temporary counter.
    self._count_data_type[event_object.data_type] += 1
    parser = attributes.get("parser", "unknown_parser")
    self._count_parser[parser] += 1

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

    event_object_data = self._event_object_serializer.WriteSerialized(event_object)

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

    # TODO: Re-think this approach with the re-design of the storage.
    # Check if the event object failed to serialize (none is returned).
    if event_object_data is None:
        return

    heapq.heappush(self._buffer, (event_object.timestamp, event_object_data))
    self._buffer_size += len(event_object_data)
    self._write_counter += 1

    if self._buffer_size > self._max_buffer_size:
        self._FlushBuffer()


# 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 AddEventObject(self, event_object):
    """Adds an event object to the storage.

    Args:
      event_object: an event object (instance of EventObject).

    Raises:
      IOError: When trying to write to a closed storage file.
    """
    if not self._zipfile:
        raise IOError("Trying to add an entry to a closed storage file.")

    if event_object.timestamp > self._buffer_last_timestamp:
        self._buffer_last_timestamp = event_object.timestamp

    # TODO: support negative timestamps.
    if (
        event_object.timestamp < self._buffer_first_timestamp
        and event_object.timestamp > 0
    ):
        self._buffer_first_timestamp = event_object.timestamp

    attributes = event_object.GetValues()
    # Add values to counters.
    if self._pre_obj:
        self._pre_obj.counter["total"] += 1
        self._pre_obj.counter[attributes.get("parser", "N/A")] += 1
        # TODO remove plugin, add parser chain. Refactor to separate method e.g.
        # UpdateEventCounters.
        if "plugin" in attributes:
            self._pre_obj.plugin_counter[attributes.get("plugin", "N/A")] += 1

    # Add to temporary counter.
    self._count_data_type[event_object.data_type] += 1
    parser = attributes.get("parser", "unknown_parser")
    self._count_parser[parser] += 1

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

    event_object_data = self._event_object_serializer.WriteSerialized(event_object)

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

    # TODO: Re-think this approach with the re-design of the storage.
    # Check if the event object failed to serialize (none is returned).
    if event_object_data is None:
        return

    heapq.heappush(self._buffer, (event_object.timestamp, event_object_data))
    self._buffer_size += len(event_object_data)
    self._write_counter += 1

    if self._buffer_size > self._max_buffer_size:
        self._WriteBuffer()