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. What's wrong with this code ? (CString issue)

What's wrong with this code ? (CString issue)

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
9 Posts 9 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.
  • T Offline
    T Offline
    Tnarol
    wrote on last edited by
    #1

    Hi, Can someone explain why this code crashes (when calling ReleaseBuffer()) ? #define PROFILE CString("config.ini"); CString result; GetPrivateProfileString("MySection","MyString","",result.GetBuffer(),512,PROFILE); result.ReleaseBuffer(); And how can I avoid this ?

    V K J L W 8 Replies Last reply
    0
    • T Tnarol

      Hi, Can someone explain why this code crashes (when calling ReleaseBuffer()) ? #define PROFILE CString("config.ini"); CString result; GetPrivateProfileString("MySection","MyString","",result.GetBuffer(),512,PROFILE); result.ReleaseBuffer(); And how can I avoid this ?

      V Offline
      V Offline
      Viorel
      wrote on last edited by
      #2

      I think you should specify the size of the allocated buffer:

      GetPrivateProfileString("MySection", "MyString", "", result.GetBuffer(512), 512, PROFILE);
      
      1 Reply Last reply
      0
      • T Tnarol

        Hi, Can someone explain why this code crashes (when calling ReleaseBuffer()) ? #define PROFILE CString("config.ini"); CString result; GetPrivateProfileString("MySection","MyString","",result.GetBuffer(),512,PROFILE); result.ReleaseBuffer(); And how can I avoid this ?

        J Offline
        J Offline
        Justin Tay
        wrote on last edited by
        #3

        My guess is the crash is due to writing past the allocated buffer memory that the CString has. Debug builds have some checks to see if you make this mistake. Get the CString to return a buffer that has a certain minimum size. #define PROFILE CString("config.ini"); CString result; GetPrivateProfileString("MySection","MyString","",result.GetBuffer(512),512,PROFILE); result.ReleaseBuffer();

        1 Reply Last reply
        0
        • T Tnarol

          Hi, Can someone explain why this code crashes (when calling ReleaseBuffer()) ? #define PROFILE CString("config.ini"); CString result; GetPrivateProfileString("MySection","MyString","",result.GetBuffer(),512,PROFILE); result.ReleaseBuffer(); And how can I avoid this ?

          K Offline
          K Offline
          kk tvm
          wrote on last edited by
          #4

          Hi Please remove ';' in define line KK

          1 Reply Last reply
          0
          • T Tnarol

            Hi, Can someone explain why this code crashes (when calling ReleaseBuffer()) ? #define PROFILE CString("config.ini"); CString result; GetPrivateProfileString("MySection","MyString","",result.GetBuffer(),512,PROFILE); result.ReleaseBuffer(); And how can I avoid this ?

            L Offline
            L Offline
            Laxman Auti
            wrote on last edited by
            #5

            Tnarol wrote:

            GetPrivateProfileString("MySection","MyString","",result.GetBuffer(),512,PROFILE);

            Result of the string where you get the data from ini file, it should be LPTSTR buffer, specify the size of that buffer instead of 512. Knock out 't' from can't, You can if you think you can :cool:

            1 Reply Last reply
            0
            • T Tnarol

              Hi, Can someone explain why this code crashes (when calling ReleaseBuffer()) ? #define PROFILE CString("config.ini"); CString result; GetPrivateProfileString("MySection","MyString","",result.GetBuffer(),512,PROFILE); result.ReleaseBuffer(); And how can I avoid this ?

              W Offline
              W Offline
              welkass
              wrote on last edited by
              #6

              Try this: #define PROFILE "config.ini"; CString result(_T(" "),512); GetPrivateProfileString("MySection","MyString","",result.GetBuffer(),512,PROFILE); result.ReleaseBuffer(); May be you are trying to release some memory not allocated.

              1 Reply Last reply
              0
              • T Tnarol

                Hi, Can someone explain why this code crashes (when calling ReleaseBuffer()) ? #define PROFILE CString("config.ini"); CString result; GetPrivateProfileString("MySection","MyString","",result.GetBuffer(),512,PROFILE); result.ReleaseBuffer(); And how can I avoid this ?

                _ Offline
                _ Offline
                _AnsHUMAN_
                wrote on last edited by
                #7

                For GetBuffer method : Call this method to return the buffer contents of the CSimpleStringT object. You are having this string empty. Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_

                1 Reply Last reply
                0
                • T Tnarol

                  Hi, Can someone explain why this code crashes (when calling ReleaseBuffer()) ? #define PROFILE CString("config.ini"); CString result; GetPrivateProfileString("MySection","MyString","",result.GetBuffer(),512,PROFILE); result.ReleaseBuffer(); And how can I avoid this ?

                  H Offline
                  H Offline
                  Hamid Taebi
                  wrote on last edited by
                  #8

                  See a little class to Read Ini File[^] maybe it is some helpful to you_**


                  **_

                  whitesky


                  1 Reply Last reply
                  0
                  • T Tnarol

                    Hi, Can someone explain why this code crashes (when calling ReleaseBuffer()) ? #define PROFILE CString("config.ini"); CString result; GetPrivateProfileString("MySection","MyString","",result.GetBuffer(),512,PROFILE); result.ReleaseBuffer(); And how can I avoid this ?

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

                    You have two compiler errors. The first is a missing argument to GetBuffer(). The second is an erroneous semicolon at the end of PROFILE.


                    "The largest fire starts but with the smallest spark." - David Crow

                    "Judge not by the eye but by the heart." - Native American Proverb

                    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