0

I have the following method:

void ConnectionManager::SendAll()
{
for (int t = 0; t < (int)m_Connections.size(); ++t)
{
    if (m_Connections[t].socket != INVALID_SOCKET)
    {
        // Create the object
        MessageID message;
        message.set_type(MessageID::Type::MessageID_Type_PLAYERDATA);

        for (int i = 0; i < (int)m_Connections.size(); ++i)
        {
            if (m_Connections[i].playerData.username() != google::protobuf::internal::kEmptyString)
            {
                auto temp = message.add_playerdata();   
                temp = &m_Connections[i].playerData;
            }
        }

        if (message.playerdata_size() > 0)
        {
            // Serialize to byte array
            int size = message.ByteSize();
            void* buffer = malloc(size);
            message.SerializeToArray(buffer, size);

            Send(m_Connections[t].socket, buffer, size);
        }
    }
}
}

Now the problem lies at the end of this method, at the line:

int size = message.ByteSize();

I know that the data is loaded correctly (or it should be at least) but the size isn't right. It should be 30 and it returns 2. I have no idea what I'm doing wrong.

The data in m_Connections is available and should be reached by pointer temp. I think that, for some reason, the data is lost from the "message" object but I don't know how to solve it.

1 Answer 1

1
auto temp = message.add_playerdata();   
temp = &m_Connections[i].playerData;

These lines look wrong. add_playerdata() returns a pointer. That means that the second line is just setting temp to some other pointer, not doing anything to the message which temp points at. It's more obvious if you write out the type rather than use auto:

MessageID::PlayerData* temp = message.add_playerdata();   
temp = &m_Connections[i].playerData;

Maybe you wanted to do this instead:

*temp = m_Connections[i].playerData;

However, I do not see how this bug would lead to ByteSize() being 2. It looks like ByteSize() should only be 2 if you haven't added any players to the message, but then playerdata_size() would be zero, so you wouldn't get to the serialization step at all.

Sign up to request clarification or add additional context in comments.

3 Comments

Well the thing is that temp is a new item in a vector of type PlayerData* in the MessageID object "message". What I wanted to do in the line you copied was to assign the data from a vector I have created to the vector that is used in the MessageID object. I'll try your ideas.
Well, I haven't checked the data yet but apparently the line: *temp = m_Connections[i].playerData; solved the crash. Could you perhaps explain why?
Well, this is basic C... temp is a pointer, *temp is the thing the pointer points at. If you write temp = &foo;, you're only changing the pointer, to make it point at foo rather than at whatever it was pointing at before. Since you don't even use temp again after that, this has no effect. On the other hand, when you write *temp = foo;, you are copying the value of foo into the thing that temp points at.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.