title: Can't understand what causes Segmentation Fault
body: <p>As part of my homework I had to implement a something similar to facebook.</p>
  
  <p>The following lines of code result in SG:</p>
  
  <pre><code>Tyler-&gt;addFriend(302); //Tyler asks Evil to be friends.
  Evil-&gt;addFriend(1500); //Evil asks Tyler to be friends, resulting in both becoming friends.
  </code></pre>
  
  <p>Below are the relevant methods, in their execution order:
  [
  1. Tyler sends a friend request to Evil thru the server.
  2. Server send the friend request to Evil.
  3. Evil wants to send a friend request to Tyler but sees that Tyler already send him a friend request. Evil accepts the friend request.]</p>
  
  <pre><code>void Fan::addFriend(int fanId){
      shared_ptr&lt;FanBookServer&gt; server = FanBookServer::getServer();
      shared_ptr&lt;BaseRequest&gt; request;
      //Check if already friends.
      if (friends.isIn(fanId)){
          throw AlreadyFriendsException();
      }
      //Check if the other fan already send request.
      try {
          request = pendingRequests[fanId];
      } catch (...){
          //Make a friend request and send it to the server.
          auto newRequest = std::make_shared&lt;FriendRequest&gt;(id, fanId);
          server-&gt;fanToFanRequest(newRequest);
          return;
      }
      request-&gt;accept();  
  }
  </code></pre>
  
  <hr>
  
  <pre><code>void FanBookServer::fanToFanRequest(std::shared_ptr&lt;BaseRequest&gt; request){
      std::shared_ptr&lt;Fan&gt; srcFan, dstFan;
      try {   
          dstFan = connectedFans[request-&gt;getDestination()];
          srcFan = connectedFans[request-&gt;getSource()];
      } catch (mtm::MappingDoesntExist&amp;){
          throw UserNotConnectedException();
      }
  
      try {
          dstFan-&gt;pendingRequests.insert(srcFan-&gt;getId(), request);
      } catch (mtm::MappingAlreadyExists&amp;){
          throw AlreadySendRequestException();
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>void FriendRequest::accept() {
  
      std::cout &lt;&lt; "About to SG" &lt;&lt; std::endl;
  
      shared_ptr&lt;FanBookServer&gt; server = FanBookServer::getServer();
      server-&gt;confirmFriendRequest(getSource(), getDestination());
  }
  </code></pre>
  
  <hr>
  
  <p>EDIT:</p>
  
  <p>I staffed all my functions with cout prints and came into conclusion that the SG happens right after exiting the FanBookServer::confirmFriendRequest().</p>
  
  <p>The following cout lines:</p>
  
  <pre><code>void FanBookServer::confirmFriendRequest(int sourceFan, int destinationFan){
      std::cout &lt;&lt; "Within FanBookServer::confirmFriendRequest" &lt;&lt; std::endl;
      std::shared_ptr&lt;Fan&gt; srcFan, dstFan;
  
      std::cout &lt;&lt; "before try" &lt;&lt; std::endl;
      try {
          srcFan = connectedFans[sourceFan];
          dstFan = connectedFans[destinationFan];
      } catch (mtm::MappingDoesntExist&amp;){
          throw UserNotConnectedException();
      }
      std::cout &lt;&lt; "After try" &lt;&lt; std::endl;
  
      std::cout &lt;&lt; "Before insert" &lt;&lt; std::endl;
      srcFan-&gt;friends.insert(dstFan-&gt;getId(), dstFan);
      dstFan-&gt;friends.insert(srcFan-&gt;getId(), srcFan);
      std::cout &lt;&lt; "After insert" &lt;&lt; std::endl;
      dstFan-&gt;pendingRequests.remove(srcFan-&gt;getId());
      std::cout &lt;&lt; "After PR" &lt;&lt; std::endl;
  }
  </code></pre>
  
  <h2>The last thing that is printed is "After PR". Thus i think the root of the problem is that some shared pointer goes put of scope.</h2>
  
  <p>[Edit2:]
  I run LLDB and got the following result:</p>
  
  <pre><code>* thread #1: tid = 0x11ef4a, 0x0000000100004cea FanBook`std::__1::__shared_ptr_emplace&lt;mtm::FriendRequest, std::__1::allocator&lt;mtm::FriendRequest&gt; &gt;::__on_zero_shared(this=0x0000000100103fd0) + 42 at memory:3741, queue = 'com.apple.main-thread, stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
      frame #0: 0x0000000100004cea FanBook`std::__1::__shared_ptr_emplace&lt;mtm::FriendRequest, std::__1::allocator&lt;mtm::FriendRequest&gt; &gt;::__on_zero_shared(this=0x0000000100103fd0) + 42 at memory:3741
     3738 template &lt;class _Tp, class _Alloc&gt;
     3739 void
     3740 __shared_ptr_emplace&lt;_Tp, _Alloc&gt;::__on_zero_shared() _NOEXCEPT
  -&gt; 3741 {
     3742     __data_.second().~_Tp();
     3743 }
     3744 
  </code></pre>
  
  <p>Can some I help me understand what is going here?</p>
  
  <hr>