Archived

This topic is now archived and is closed to further replies.

Pointer Problem

This topic is 6355 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was experimenting with DPlay, and decided to write a small chat-program to learn it properly. DPlay proved not to be the problem, the problem is this programming with Windows-dialogs, cause I didn''t do that before (Doing a 3d-engine with D3D doesn''t require much windows-programming...) So here''s my problem: in the last line if this procdedure I get a debug error, saying ''Damage!!''. I can continue my app, but I don''t think that it''s too healthy for the system. So what did I wrong with this damn char*? THX char *text = NULL; DWORD size = SendDlgItemMessage( hDlg, IDC_EDIT1, WM_GETTEXTLENGTH, 0, 0 ); text = (char*)malloc(size); GetDlgItemText( hDlg, IDC_EDIT1, text, size+1); //SendChat(PlayerList[0].ID, text); SetDlgItemText( hDlg, IDC_EDIT2, text); if(text) free(text);

Share this post


Link to post
Share on other sites
> text = (char*)malloc(size);
Firstly, don''t cast malloc. It''s unnecessary, and can hide other errors. But this is the problem line. You''re not allocating space for the null zero, as size contains only the number of characters in the string, not the amount of memory the string requires. You want size + 1 here.



> GetDlgItemText( hDlg, IDC_EDIT1, text, size+1);
Here you''re telling the function to use copy more characters than you''ve allocated memory for. This function expects a number of characters, not an amount of memory.

Share this post


Link to post
Share on other sites