     log "BreakpointManager.removeBreakpoint #{index}"
     @breakpoints.splice index, 1
     @onRemoveBreakpointEvent.broadcast breakpoint
-    @detachBreakpoint breakpoint, 'removed'
+    @detachBreakpoint breakpoint.id
+    breakpoint.dispose()
 
   addBreakpoint: (editor, script, line) ->
     log "BreakpointManager.addBreakpoint #{script}, #{line}"
-    breakpoint =
-      script: script
-      line: line
-      marker: null
-      editor: editor
-      id: null
+    breakpoint = new Breakpoint(editor, script, line)
     log "BreakpointManager.addBreakpoint - adding to list"
     @breakpoints.push breakpoint
-    log "BreakpointManager.addBreakpoint - adding default decoration"
-    @decorateBreakpoint breakpoint
     log "BreakpointManager.addBreakpoint - publishing event, num breakpoints=#{@breakpoints.length}"
     @onAddBreakpointEvent.broadcast breakpoint
     log "BreakpointManager.addBreakpoint - attaching"

NodeDebuggerView = require './node-debugger-view'
jumpToBreakpoint = require './jump-to-breakpoint'

log = (msg) -> # console.log(msg)

class ProcessManager extends EventEmitter
  constructor: (@atom = atom)->

      @process.once 'exit', onProcessEnd
      @process.once 'close', onProcessEnd

class Breakpoint
  constructor: (@editor, @script, @line) ->
    @marker = null
    @marker = @editor.markBufferPosition([@line, 0], invalidate: 'never')
    @decoration = null
    @onDidChangeSubscription = @marker.onDidChange (event) =>
      log "Breakpoint.markerchanged: #{event.newHeadBufferPosition}"
      @line = event.newHeadBufferPosition.row
    @onDidDestroySubscription = @marker.onDidDestroy () =>
      @marker = null
      @decoration = null
    @id = null
    @updateVisualization()

  dispose: () ->
    @onDidChangeSubscription.dispose()
    @onDidDestroySubscription.dispose()
    @id = null
    @decoration.destroy() if @decoration
    @decoration = null
    @marker.destroy() if @marker
    @marker = null

  setId: (@id) =>
    @updateVisualization()

  clearId: () =>
    @setId(null)

  updateVisualization: () =>
    @decoration.destroy() if @decoration
    className = if @id then 'node-debugger-attached-breakpoint' else 'node-debugger-detached-breakpoint'
    @decoration = @editor.decorateMarker(@marker, type: 'line-number', class: className) if @marker

class BreakpointManager
  constructor: (@debugger) ->
    log "BreakpointManager.constructor"
    @breakpoints = []
    @client = null
    @removeOnConnected = @debugger.subscribe 'connected', =>
      log "BreakpointManager.connected"
      @client = @debugger.client
      @attachBreakpoint(breakpoint) for breakpoint in @breakpoints
      log "BreakpointManager.connected - done"
    @removeOnDisconnected = @debugger.subscribe 'disconnected', =>
      log "BreakpointManager.disconnected"
      @client = null
      breakpoint.clearId() for breakpoint in @breakpoints
      log "BreakpointManager.disconnected - done"
    @onAddBreakpointEvent = Event()
    @onRemoveBreakpointEvent = Event()

