GetBuffer?? ReleaseBuffer??
-
Hi, I tried to read lines from rich edit control and display in the debug window with the following code. If I set i=1 in the for loop, this program won't crash and display every line other then the first line. However, when I set i=0 as below, it will crash and have memory leak problem. Can someone please tell me what's going on? Thank you. int i, nLineLength, nLineCount; CString strText; nLineCount = m_redit.GetLineCount(); for (i=0;i < nLineCount ;i++) { nLineLength = m_redit.LineLength(i); m_redit.GetLine(i, strText.GetBuffer(nLineLength)); TRACE (strText); strText.ReleaseBuffer(nLineLength); }
-
Hi, I tried to read lines from rich edit control and display in the debug window with the following code. If I set i=1 in the for loop, this program won't crash and display every line other then the first line. However, when I set i=0 as below, it will crash and have memory leak problem. Can someone please tell me what's going on? Thank you. int i, nLineLength, nLineCount; CString strText; nLineCount = m_redit.GetLineCount(); for (i=0;i < nLineCount ;i++) { nLineLength = m_redit.LineLength(i); m_redit.GetLine(i, strText.GetBuffer(nLineLength)); TRACE (strText); strText.ReleaseBuffer(nLineLength); }
skinnyreptile wrote: TRACE (strText); strText.ReleaseBuffer(nLineLength); Try switching these two statements.
-
skinnyreptile wrote: TRACE (strText); strText.ReleaseBuffer(nLineLength); Try switching these two statements.
Even without trace statment, it's still not working.
-
Hi, I tried to read lines from rich edit control and display in the debug window with the following code. If I set i=1 in the for loop, this program won't crash and display every line other then the first line. However, when I set i=0 as below, it will crash and have memory leak problem. Can someone please tell me what's going on? Thank you. int i, nLineLength, nLineCount; CString strText; nLineCount = m_redit.GetLineCount(); for (i=0;i < nLineCount ;i++) { nLineLength = m_redit.LineLength(i); m_redit.GetLine(i, strText.GetBuffer(nLineLength)); TRACE (strText); strText.ReleaseBuffer(nLineLength); }
Where does it crash? On what line? Ryan Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact" -
Where does it crash? On what line? Ryan Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"On line 0. If the for loop is start at line 1, it won't crash.
-
Hi, I tried to read lines from rich edit control and display in the debug window with the following code. If I set i=1 in the for loop, this program won't crash and display every line other then the first line. However, when I set i=0 as below, it will crash and have memory leak problem. Can someone please tell me what's going on? Thank you. int i, nLineLength, nLineCount; CString strText; nLineCount = m_redit.GetLineCount(); for (i=0;i < nLineCount ;i++) { nLineLength = m_redit.LineLength(i); m_redit.GetLine(i, strText.GetBuffer(nLineLength)); TRACE (strText); strText.ReleaseBuffer(nLineLength); }
I haven't tried, but... nLineCount = m_redit.GetLineCount(); for (i=0; i < nLineCount - 1; i++) { nLineLength = m_redit.LineLength(i); m_redit.GetLine(i, strText.GetBuffer(nLineLength)); TRACE (strText); strText.ReleaseBuffer(nLineLength); } Do this works?
-
I haven't tried, but... nLineCount = m_redit.GetLineCount(); for (i=0; i < nLineCount - 1; i++) { nLineLength = m_redit.LineLength(i); m_redit.GetLine(i, strText.GetBuffer(nLineLength)); TRACE (strText); strText.ReleaseBuffer(nLineLength); } Do this works?
I tried the following code with display line 0 only when nLineCount > 1. THe code still crash. if (nLineCount > 1) { nLineLength = m_redit.LineLength(0); m_redit.GetLine(0, strText.GetBuffer(nLineLength)); TRACE (strText); strText.ReleaseBuffer(nLineLength); }
-
Even without trace statment, it's still not working.
Read the documentation carefully. Note the requirement that the first word of the buffer must specify the maximum number of bytes that can be copied into the buffer. You need something like:
int i, nLineLength, nLineCount; char szBuffer\[64\]; nLineCount = m\_edit.GetLineCount(); for (i=0;i < nLineCount ;i++) { nLineLength = m\_edit.LineLength(i); m\_edit.GetLine(i, szBuffer, nLineLength); szBuffer\[nLineLength\] = '\\0'; TRACE("%s\\n", szBuffer); }
or
int i, nLineLength, nLineCount; char szBuffer\[64\]; nLineCount = m\_edit.GetLineCount(); for (i=0;i < nLineCount ;i++) { nLineLength = m\_edit.LineLength(i); \*((int \*) szBuffer) = nLineLength; m\_edit.GetLine(i, szBuffer); szBuffer\[nLineLength\] = '\\0'; TRACE("%s\\n", szBuffer); }
-
Hi, I tried to read lines from rich edit control and display in the debug window with the following code. If I set i=1 in the for loop, this program won't crash and display every line other then the first line. However, when I set i=0 as below, it will crash and have memory leak problem. Can someone please tell me what's going on? Thank you. int i, nLineLength, nLineCount; CString strText; nLineCount = m_redit.GetLineCount(); for (i=0;i < nLineCount ;i++) { nLineLength = m_redit.LineLength(i); m_redit.GetLine(i, strText.GetBuffer(nLineLength)); TRACE (strText); strText.ReleaseBuffer(nLineLength); }
The function LineLength will return the ONLY the number of characters for the given line not including the carriage-return character. According MSDN the function LineLength actually calls the message EM_LINELENGTH and in the MSDN this message states that It does not include the carriage-return character at the end of the line. Also you should account for the NULL character at the end of the string when assigning the length of the buffer. This code works for me:
int i, nLineLength, nLineCount; CString strText; nLineCount = m_redit.GetLineCount(); for (i=0;i < nLineCount ;i++) { nLineLength = m_redit.LineLength(i); //Add 2 to account for the CR and NULL character. int nActualCharactersReturned = m_redit.GetLine(i, strText.GetBuffer(nLineLength + 2)); //Release the buffer with the actual # characters returned. strText.ReleaseBuffer(nActualCharactersReturned); TRACE (strText); }
Kelly Herald Software Developer MPC
-
The function LineLength will return the ONLY the number of characters for the given line not including the carriage-return character. According MSDN the function LineLength actually calls the message EM_LINELENGTH and in the MSDN this message states that It does not include the carriage-return character at the end of the line. Also you should account for the NULL character at the end of the string when assigning the length of the buffer. This code works for me:
int i, nLineLength, nLineCount; CString strText; nLineCount = m_redit.GetLineCount(); for (i=0;i < nLineCount ;i++) { nLineLength = m_redit.LineLength(i); //Add 2 to account for the CR and NULL character. int nActualCharactersReturned = m_redit.GetLine(i, strText.GetBuffer(nLineLength + 2)); //Release the buffer with the actual # characters returned. strText.ReleaseBuffer(nActualCharactersReturned); TRACE (strText); }
Kelly Herald Software Developer MPC
Thank you for all the help, guys!