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. CString assignment crashes on Windows 7

CString assignment crashes on Windows 7

Scheduled Pinned Locked Moved C / C++ / MFC
11 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.
  • P pandit84

    Hi I have a function

    void TakeData(myStruct &structRef)
    {
    CString str = TmpFileMgr->TmpFilePathName() ; //Returns file path
    structRef.cstrFilePath = str ; //Crashes at this point on Windows 7
    }

    in a dll which takes Temporary folder path and assign it in a CString variables. This CString variable is member of structure object which is passed as a reference to TakeData(myStruct &structRef)function. I am able to print temporary folder path in TakeData function and CString assigment works perfectly on Windows XP. But when I run this code on Windows 7 it crashes at the point where I am doing assigment of temp folder path in a CString variable. I have all admin rights on Windows 7 System , also I have put UAC to OFF. Please provide any input in this regard. Thanks

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

    pandit84 wrote:

    But when I run this code on Windows 7 it crashes at the point where I am doing assigment of temp folder path in a CString variable.

    And yet this is the code you do not show. If TakeData() is not pages long, please post it.

    "One man's wage rise is another man's price increase." - Harold Wilson

    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

    "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

    1 Reply Last reply
    0
    • P pandit84

      Hi I have a function

      void TakeData(myStruct &structRef)
      {
      CString str = TmpFileMgr->TmpFilePathName() ; //Returns file path
      structRef.cstrFilePath = str ; //Crashes at this point on Windows 7
      }

      in a dll which takes Temporary folder path and assign it in a CString variables. This CString variable is member of structure object which is passed as a reference to TakeData(myStruct &structRef)function. I am able to print temporary folder path in TakeData function and CString assigment works perfectly on Windows XP. But when I run this code on Windows 7 it crashes at the point where I am doing assigment of temp folder path in a CString variable. I have all admin rights on Windows 7 System , also I have put UAC to OFF. Please provide any input in this regard. Thanks

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #3

      I suppose this is worth reading: "Using and Writing DLLs with MFC"[^].

      Veni, vidi, vici.

      1 Reply Last reply
      0
      • P pandit84

        Hi I have a function

        void TakeData(myStruct &structRef)
        {
        CString str = TmpFileMgr->TmpFilePathName() ; //Returns file path
        structRef.cstrFilePath = str ; //Crashes at this point on Windows 7
        }

        in a dll which takes Temporary folder path and assign it in a CString variables. This CString variable is member of structure object which is passed as a reference to TakeData(myStruct &structRef)function. I am able to print temporary folder path in TakeData function and CString assigment works perfectly on Windows XP. But when I run this code on Windows 7 it crashes at the point where I am doing assigment of temp folder path in a CString variable. I have all admin rights on Windows 7 System , also I have put UAC to OFF. Please provide any input in this regard. Thanks

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

        What is the definition of myStruct, and what is the value of structRef when the crash occurs?

        One of these days I'm going to think of a really clever signature.

        P 1 Reply Last reply
        0
        • L Lost User

          What is the definition of myStruct, and what is the value of structRef when the crash occurs?

          One of these days I'm going to think of a really clever signature.

          P Offline
          P Offline
          pandit84
          wrote on last edited by
          #5

          Struct is defined as below. Before crash ,value of cstrFilePath is empty.

          struct myStruct
          {
          int charsize ;
          CString cstrFilePath;
          myStruct() : charsize (0)
          {
          cstrFilePath.Empty();
          }
          } ;

          L 1 Reply Last reply
          0
          • P pandit84

            Struct is defined as below. Before crash ,value of cstrFilePath is empty.

            struct myStruct
            {
            int charsize ;
            CString cstrFilePath;
            myStruct() : charsize (0)
            {
            cstrFilePath.Empty();
            }
            } ;

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

            pandit84 wrote:

            Before crash ,value of cstrFilePath is empty.

            Which is rather irrelevant. What I asked was, what is the value of structRef when the crash occurs?

            One of these days I'm going to think of a really clever signature.

            1 Reply Last reply
            0
            • P pandit84

              Hi I have a function

              void TakeData(myStruct &structRef)
              {
              CString str = TmpFileMgr->TmpFilePathName() ; //Returns file path
              structRef.cstrFilePath = str ; //Crashes at this point on Windows 7
              }

              in a dll which takes Temporary folder path and assign it in a CString variables. This CString variable is member of structure object which is passed as a reference to TakeData(myStruct &structRef)function. I am able to print temporary folder path in TakeData function and CString assigment works perfectly on Windows XP. But when I run this code on Windows 7 it crashes at the point where I am doing assigment of temp folder path in a CString variable. I have all admin rights on Windows 7 System , also I have put UAC to OFF. Please provide any input in this regard. Thanks

              S Offline
              S Offline
              Stephen Hewitt
              wrote on last edited by
              #7

              You have supplied very little to go on, so in addition to suggesting you provide more information I'll ask a question: are you allocating memory in one module and freeing it in another (a module is a dll or exe)?

              Steve

              P 1 Reply Last reply
              0
              • S Stephen Hewitt

                You have supplied very little to go on, so in addition to suggesting you provide more information I'll ask a question: are you allocating memory in one module and freeing it in another (a module is a dll or exe)?

                Steve

                P Offline
                P Offline
                pandit84
                wrote on last edited by
                #8

                I am sorry for adding incomplete info. I have allocated memory to object of structure myStruct in one dll and passed this struct object as a reference to another dll. This crash happens only while assinging value to

                structRef.cstrFilePath

                variable. Finally I was able to assign value inside CString variable using

                wsccpy

                method. I was able to resolve this crash with following code , but I am sure this is not the correct way to do it.

                wcscpy (structRef.cstrFilePath.GetBuffer (), str.GetBuffer ()) ;

                I am still trying to resolve this issue in a correct way. I am using Windows 7 system and GCC Compiler. Thanks all for replying to my previous post.

                S 1 Reply Last reply
                0
                • P pandit84

                  I am sorry for adding incomplete info. I have allocated memory to object of structure myStruct in one dll and passed this struct object as a reference to another dll. This crash happens only while assinging value to

                  structRef.cstrFilePath

                  variable. Finally I was able to assign value inside CString variable using

                  wsccpy

                  method. I was able to resolve this crash with following code , but I am sure this is not the correct way to do it.

                  wcscpy (structRef.cstrFilePath.GetBuffer (), str.GetBuffer ()) ;

                  I am still trying to resolve this issue in a correct way. I am using Windows 7 system and GCC Compiler. Thanks all for replying to my previous post.

                  S Offline
                  S Offline
                  Stephen Hewitt
                  wrote on last edited by
                  #9

                  Unless you configure things properly each module (dll or exe or other executable module) has it's own heap. In this environment you can't allocate memory in one module and free it another. Given that CString manages a buffer using an instance in multiple modules could result in this happening. Forget wcscpy, it's ugly and isn't a fix for the root problem, whatever it is. If it's heap corruption, which is my guess, it's going to blow up sooner or later and "fixing" one problem with an ugly hack will just result in it popping up somewhere else.

                  Steve

                  P 1 Reply Last reply
                  0
                  • S Stephen Hewitt

                    Unless you configure things properly each module (dll or exe or other executable module) has it's own heap. In this environment you can't allocate memory in one module and free it another. Given that CString manages a buffer using an instance in multiple modules could result in this happening. Forget wcscpy, it's ugly and isn't a fix for the root problem, whatever it is. If it's heap corruption, which is my guess, it's going to blow up sooner or later and "fixing" one problem with an ugly hack will just result in it popping up somewhere else.

                    Steve

                    P Offline
                    P Offline
                    pandit84
                    wrote on last edited by
                    #10

                    Finally I have solved this issue. The CString when used with GCC was Used as LinuxCString which is internally std::string. Capacity of std::string when calculated was every time 0. so when I tried to assign const char* to LinuxCString ( i.e. CString in MSVC ) it was crashing. I have used std::string.reserve (200) method to specify the capacity first while initialization. This solves my issue. I am still evaluating whether my fix is perfect or not. Thanks all

                    S 1 Reply Last reply
                    0
                    • P pandit84

                      Finally I have solved this issue. The CString when used with GCC was Used as LinuxCString which is internally std::string. Capacity of std::string when calculated was every time 0. so when I tried to assign const char* to LinuxCString ( i.e. CString in MSVC ) it was crashing. I have used std::string.reserve (200) method to specify the capacity first while initialization. This solves my issue. I am still evaluating whether my fix is perfect or not. Thanks all

                      S Offline
                      S Offline
                      Stephen Hewitt
                      wrote on last edited by
                      #11

                      The fix is far from perfect, you shouldn't need to call reserve. If I were you I'd be looking for the real problem.

                      Steve

                      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