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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Does CString;;GetBufferSetLength Set/Allocate the CString Buffer

Does CString;;GetBufferSetLength Set/Allocate the CString Buffer

Scheduled Pinned Locked Moved C / C++ / MFC
help
6 Posts 3 Posters 1 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.
  • F Offline
    F Offline
    ForNow
    wrote on last edited by
    #1

    Hi I found the source of problem the exception occurred when I did CAsncSocket::Receive(CString*,int) I has previously done a GetBufferSetLength to a large enough size so I don't understand why I got an exception

    L J 2 Replies Last reply
    0
    • F ForNow

      Hi I found the source of problem the exception occurred when I did CAsncSocket::Receive(CString*,int) I has previously done a GetBufferSetLength to a large enough size so I don't understand why I got an exception

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Check the documentation: CAsyncSocket::Receive[^]. The first parameter should be a pointer to a byte buffer, which CString* is not.

      1 Reply Last reply
      0
      • F ForNow

        Hi I found the source of problem the exception occurred when I did CAsncSocket::Receive(CString*,int) I has previously done a GetBufferSetLength to a large enough size so I don't understand why I got an exception

        J Offline
        J Offline
        Jochen Arndt
        wrote on last edited by
        #3

        The CString class is not thread safe but using CAsyncSocket indicates that you are using multiple threads. When using GetBuffer[Xxx], the string object is locked until ReleaseBuffer is called (which must be called from the same thread). As Richard already noted, you need a byte buffer. While this can be achieved by using CStringA, I recommend to rethink your design. Don't use string classes for binary data and ensure thread safety.

        F 1 Reply Last reply
        0
        • J Jochen Arndt

          The CString class is not thread safe but using CAsyncSocket indicates that you are using multiple threads. When using GetBuffer[Xxx], the string object is locked until ReleaseBuffer is called (which must be called from the same thread). As Richard already noted, you need a byte buffer. While this can be achieved by using CStringA, I recommend to rethink your design. Don't use string classes for binary data and ensure thread safety.

          F Offline
          F Offline
          ForNow
          wrote on last edited by
          #4

          Are you to

          CByteArray

          if so how can I ensure the size

          L J 2 Replies Last reply
          0
          • F ForNow

            Are you to

            CByteArray

            if so how can I ensure the size

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            You should just use a simple array of bytes, which is what the socket method requires.

            1 Reply Last reply
            0
            • F ForNow

              Are you to

              CByteArray

              if so how can I ensure the size

              J Offline
              J Offline
              Jochen Arndt
              wrote on last edited by
              #6

              You probably don't need an array class if you just want to copy data and do not need inserting or removing of items. If you know the (max.) size in advance just use a plain array allocated with new. If not, you can still grow by allocating a new array, copying data from the old, and delete the old. CByteArray is also an MFC class which is probably also not thread safe. But it is impossible to help without knowing what your code is doing. The question are (you may ask and solve them yourself): Does my array require special modification functions besides copying? Which functions use my array under which conditions (read only, write only, both)? Where is may array allocated (this might be at different code points if resizing is necessary)? Where it is deleted? Do I need thread safety?

              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