SIGSEGV, Segmentation fault. Only on linux, it works on windows
<p>I have been stuck on this for hours, for some reason I am getting a Segmentation fault error only on Linux. It only happens with packets of length 17 or more characters. I am using <strong><a href="http://enet.bespin.org/" rel="nofollow noreferrer">Enet</a></strong> library to send the packets. Anyone can help me out, I don't know what I am missing here ...</p>

<p>Here is a screenshot of gdb output
<a href="https://i.stack.imgur.com/DvtiM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DvtiM.png" alt="Here is a screenshot of gdb output"></a>
Here is the server side sample:</p>

<pre><code>void GameServer::start()
{
if (!m_isRunning)
    m_isRunning = true;

ENetEvent  event;
//ENetPacket *packet;

while (m_isRunning)
{
    while (enet_host_service(server, &amp;event, 1000) &gt; 0)
    {
        switch (event.type)
        {
        case ENET_EVENT_TYPE_CONNECT:
            std::cout &lt;&lt; "A new client connected from ";
            std::cout &lt;&lt; event.peer-&gt;address.host &lt;&lt; " ";
            std::cout &lt;&lt; event.peer-&gt;address.port &lt;&lt; std::endl;

            break;
        case ENET_EVENT_TYPE_DISCONNECT:
            printf("%s disconnected.\n", event.peer-&gt;data);
            delete (char*)event.peer-&gt;data;
            break;
        case ENET_EVENT_TYPE_RECEIVE:
        {
            std::cout &lt;&lt; "A packet of length ";
            std::cout &lt;&lt; event.packet-&gt;dataLength;
            std::cout &lt;&lt; " containing ";
            std::cout &lt;&lt; event.packet-&gt;data;
            std::cout &lt;&lt; " was received from ";
            printf("%s", event.peer-&gt;data);
            std::cout &lt;&lt; " on channel ";
            std::cout &lt;&lt; event.channelID &lt;&lt; std::endl;

            // Split the packet data, ex:   packettype:data:data
            std::vector&lt;std::string&gt; tokens;
            std::string data;
            std::istringstream split((const char *)event.packet-&gt;data);

            // Process the packet data
            while (std::getline(split, data, ':')) Line 89: Crash here
                tokens.push_back(data);

            if (tokens.size() &gt; 1)
            {

                //pc = player connected so we will broadcast the name to all peers
                if (tokens[0] == "pc")
                {
                    if (tokens.size() == 2)
                    {
                        char* data = new char[tokens[1].length() + 1];
                        strcpy(data, tokens[1].c_str());
                        event.peer-&gt;data = (void*)data;
                        for (size_t i = 0; i &lt; server-&gt;connectedPeers; i++)
                        {
                            enet_host_broadcast(server, 0, event.packet);
                        }
                    }
                }

                //ma = message all so we are going to broadcast the message to all peers
                else if (tokens[0] == "ma") 
                {
                    if (tokens.size() == 2)
                    {
                        enet_host_broadcast(server, 0, event.packet);
                    }
                }


                /* One could just use enet_host_service() instead. */
                enet_host_flush(server);
            }
            /* Clean up the packet now that we're done using it. */
            enet_packet_destroy(event.packet);
            break;
        }
        case ENET_EVENT_TYPE_NONE:
            break;
        default:
            break;
        }

    }
}

}
</code></pre>

<p>Here is how the packet is created by the client and then sent to the server.</p>

<pre><code>if (connected) {
        std::cout &lt;&lt; "Input: ";
        std::string str = "";
        std::getline(std::cin, str);

        if (str.length() == 0) { continue; }

        packet = enet_packet_create(str.c_str(), str.length() + 1, ENET_PACKET_FLAG_RELIABLE);
        enet_peer_send(peer, 0, packet);

    }
</code></pre>

<p>If code sample if not enough I can provide more.</p>