-
October 30th, 2010, 03:01 PM
#1
RichEdit control crashes using SF_TEXT
Im using codeblocks ide, with GNU GCC compiler,
code WILL work in debug mode, but FAILS when you just execute the EXE without the debugger!
problem is, when using SF_TEXT|SFF_SELECTION options to stream in text, the callback function causes the program to crash, but only when NOT running the debugger, it's strange, anyone have any ideas?
here is my callback function...
Code:
DWORD CALLBACK RichEditEx::CBStreamIn(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
string* str = (string*)(WPARAM)dwCookie;
LONG len = str->length();
if(len < 1)//when using SF_TEXT, for some reason we get a 2nd call after we are finished, so handle that situation
{*pcb = len; return 0;}
//{return 1;}//this does not work eithor
if(len <= cb)//write last chunk
{
memcpy(pbBuff,str->data(),len);
*pcb = len;
delete str;
//return 1; //cant do this, does not work
}
else//write chunks
{
memcpy(pbBuff,str->data(),cb);
*pcb = cb;
str->erase(0,cb);
}
return 0;
}
//my set text function
Code:
SetSel(-1,-1);
SetRTF(msg,SF_TEXT|SFF_SELECTION);//crashes when not in debug mode, something wrong with callback function when SF_TEXT is used
char nl[] = "\r\n";
SetRTF(nl,SF_TEXT|SFF_SELECTION);
//this does not have to be heap allocated
void RichEditEx::SetRTF(char* str, WPARAM options = SF_RTF)//options SF_RTF, SF_TEXT
{
string* sstr = new string;
sstr->append(str);//copy str into string
EDITSTREAM es;
es.dwError = 0;
es.pfnCallback = CBStreamIn;
es.dwCookie = (DWORD)(WPARAM)sstr;//pointer to string memory
::SendMessage(m_hWnd,EM_STREAMIN,options,(LPARAM)&es); //tell it to start stream in
SetSel(-1,-1);//select end
}
-
October 30th, 2010, 03:23 PM
#2
Re: RichEdit control crashes using SF_TEXT
nevemind, I found the problem, I was deleting the string inside of the callback, I changed that so I don't do that anymore, thus why it only worked in debug mode, when not in debug mode a deleted variable become a random memory value, but they do not when in debug mode.
-
November 1st, 2010, 10:57 AM
#3
Re: RichEdit control crashes using SF_TEXT
Originally Posted by 12oclocker
nevemind, I found the problem, I was deleting the string inside of the callback, I changed that so I don't do that anymore, thus why it only worked in debug mode, when not in debug mode a deleted variable become a random memory value, but they do not when in debug mode.
This is not guaranteed for every compiler, every release. You should never access an object, after it's been deleted.
Viggy
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|