CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2008
    Location
    Earth
    Posts
    60

    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
    }

  2. #2
    Join Date
    Jan 2008
    Location
    Earth
    Posts
    60

    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.

  3. #3
    Join Date
    Feb 2002
    Posts
    4,640

    Re: RichEdit control crashes using SF_TEXT

    Quote Originally Posted by 12oclocker View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured