Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. GetBuffer?? ReleaseBuffer??

GetBuffer?? ReleaseBuffer??

Scheduled Pinned Locked Moved C / C++ / MFC
debuggingperformancehelpquestion
10 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    skinnyreptile
    wrote on last edited by
    #1

    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); }

    D R J K 4 Replies Last reply
    0
    • S skinnyreptile

      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); }

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      skinnyreptile wrote: TRACE (strText); strText.ReleaseBuffer(nLineLength); Try switching these two statements.

      S 1 Reply Last reply
      0
      • D David Crow

        skinnyreptile wrote: TRACE (strText); strText.ReleaseBuffer(nLineLength); Try switching these two statements.

        S Offline
        S Offline
        skinnyreptile
        wrote on last edited by
        #3

        Even without trace statment, it's still not working.

        D 1 Reply Last reply
        0
        • S skinnyreptile

          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); }

          R Offline
          R Offline
          Ryan Binns
          wrote on last edited by
          #4

          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"

          S 1 Reply Last reply
          0
          • R Ryan Binns

            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"

            S Offline
            S Offline
            skinnyreptile
            wrote on last edited by
            #5

            On line 0. If the for loop is start at line 1, it won't crash.

            1 Reply Last reply
            0
            • S skinnyreptile

              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); }

              J Offline
              J Offline
              Joan M
              wrote on last edited by
              #6

              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?

              https://www.robotecnik.com freelance robots, PLC and CNC programmer.

              S 1 Reply Last reply
              0
              • J Joan M

                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?

                S Offline
                S Offline
                skinnyreptile
                wrote on last edited by
                #7

                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); }

                1 Reply Last reply
                0
                • S skinnyreptile

                  Even without trace statment, it's still not working.

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  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);
                  }
                  
                  1 Reply Last reply
                  0
                  • S skinnyreptile

                    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); }

                    K Offline
                    K Offline
                    Kelly Herald
                    wrote on last edited by
                    #9

                    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

                    S 1 Reply Last reply
                    0
                    • K Kelly Herald

                      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

                      S Offline
                      S Offline
                      skinnyreptile
                      wrote on last edited by
                      #10

                      Thank you for all the help, guys!

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups