i want to construct a Message class which encapsulates and handles data for sending over a network; i want to acheive something like this:
Client->Send(new CMsgPacket("Some Message", CHAT_COLOR_WHITE, CHAT_TYPE_SERVICE));
// send function deletes the data after processing it
// this is the CMsgPacket class
class CMsgPacket
{
private:
int Msg_Len, To_Len, From_Len;
char* m_Msg, *m_To, *m_From;
bool new_Msg, new_To, new_From;
public:
CHAT_TYPE ChatType;
DWORD Color;
DWORD SenderUID;
char* GetMessage( );
void SetMessage(char* value);
__declspec(property(put = SetMessage, get = GetMessage)) char* Message;
char* GetTo( );
void SetTo(char* value);
__declspec(property(put = SetTo, get = GetTo)) char* To;
char* GetFrom( );
void SetFrom(char* value);
__declspec(property(put = SetFrom, get = GetFrom)) char* From;
CMsgPacket( );
CMsgPacket::CMsgPacket(char* _Message, CHAT_COLOR _Color, CHAT_TYPE _ChatType);
};
char* CMsgPacket::GetMessage( ) { return m_Msg; }
void CMsgPacket::SetMessage(char* value)
{
if(new_Msg)
{
delete[] m_Msg;
new_Msg = false;
}
m_Msg = value;
Msg_Len = strlen(value);
}
//
char* CMsgPacket::GetTo( ) { return m_To; }
void CMsgPacket::SetTo(char* value)
{
if(new_To)
{
delete[] m_To;
new_To = false;
}
m_To = value;
To_Len = strlen(m_To);
}
//
char* CMsgPacket::GetFrom( ) { return m_From; }
void CMsgPacket::SetFrom(char* value)
{
if(new_From)
{
delete[] m_From;
new_From = false;
}
m_From = value;
From_Len = strlen(m_From);
}
//
//
// Size Function
WORD CMsgPacket::Size( )
{
return 32 + From_Len + To_Len + Msg_Len;
}
CMsgPacket::CMsgPacket( )
{
m_To = m_From = m_Msg = NULL;
To_Len = From_Len = Msg_Len = 0;
new_From = new_To = new_Msg = false;
Color = SenderUID = 0;
ChatType = CHAT_TYPE_NONE;
}
CMsgPacket::CMsgPacket(char* _Message, CHAT_COLOR _Color, CHAT_TYPE _ChatType)
{
Msg_Len = strlen(_Message);
m_Msg = new char[Msg_Len];
strcpy(m_Msg, _Message);
To = "ALL"; // <--- error at this line
From = "SYSTEM"; // <--- also i suppose here too
Color = _Color;
ChatType = _ChatType;
}
the problem is i get a coruption of the heap when i am trying to initialize the char array fields
anybody has any idea how to deal with this and why is it happening? should i assign the values to variables "m_To" and "m_From" as i did with "m_Msg", using strcpy and strlen?