I think that because he was in a coma when the curse happened his memories never implanted properly. And maybe when he got too close to the boundary around Storybrooke it caused his memory to go strange again. Or maybe Regina is messing with the curse or the curse is still trying to work on David. David said in 7:15 that he had memories of feelings of loving Kathryn and real feelings of loving Mary so it seemed like his two selves, meaning his real memories and his fake memories, are waring with themselves.