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. based pointer crashed my app

based pointer crashed my app

Scheduled Pinned Locked Moved C / C++ / MFC
17 Posts 6 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.
  • K KarstenK

    The Memory that you point to, is only valid in the function because it goes out of scope. It is better with static memory or allocated with GlobalAlloc. Attention: I think with most above written you 'may' run into trouble on the new MS Windows Vista.:confused:

    Greetings from Germany

    A Offline
    A Offline
    Alex Cutovoi
    wrote on last edited by
    #4

    In a site(www.flounder.com), I read this: "The problem of storing pointers is a little bit misleading. You must not store pointers in the structure you pass, even if they are pointers into the same area you are passing, because when the message is received the recipient has no control over where it is placed, and the pointers are likely to be nonsense. Obviously, you can't store pointers to other areas of your own memory because they will be completely nonsensical in the receiving process; they will be interpreted in its own address space. But the misleading aspect is that if you have a structure which is entirely self-contained in the data area you are sending over (that is, every pointer refers to another structure in the area you are sending), then you can use based pointers to encode them. Based pointers are actually relative offsets measured from a specific starting point (such as the start of the area being transmitted), and are automatically adjusted by the recipient (as long as you set up the correct base value) so they are valid pointers." So I have a based pointer in my class that is the same type of the class. This pointer must have to arrive(I think) to the other window safetly. toxcct, if I use new I lose the control over the pointer in the other window.

    K 1 Reply Last reply
    0
    • A Alex Cutovoi

      Hi for all I'm trying to pass some data between 2 windows. This is the class that I pass between my 2 windows. It's just a single class with a based pointer: #ifndef TESTE_H #define TESTE_H #include void * vValue; class Teste {     public:        Teste();        ~Teste();        void SetMsg(char * cMsg);        void ExibeMsg();        Teste __based(vValue) * Single;     private:        char * m_cMsg;        char * m_cClassName; }; Teste::Teste() {     if(m_cClassName == 0)m_cClassName = "Classe Teste"; } Teste::~Teste(){} void Teste::SetMsg(char * cMsg) {     m_cMsg = cMsg; } void Teste::ExibeMsg() {     if(m_cMsg != NULL)MessageBox(NULL, m_cMsg, "msg", MB_OK);} #endif TESTE_H This is the code that sends the data and handles the data: case WM_COPYDATA: {     COPYDATASTRUCT * dataReceived = (COPYDATASTRUCT*)lParam;     Teste * tempTeste = (Teste*)dataReceived->lpData;     Teste * ttt = tempTeste->Single;     char cResult[256] = {0};     int nCount = 0;     if(ttt != NULL)     {        MessageBox(NULL, "exibindo msg janela 1", "", MB_OK);        ttt->ExibeMsg();     }     break; } case WM_COMMAND: {     switch(LOWORD(wParam))     {        case IDC_BUTTON1:        {           char cLength[20], cSize[5];           if(HIWORD(wParam) == BN_CLICKED)           {              if(GetDlgItemText(hwnd, IDC_EDIT1, cLength, 20) != 0)              {                 Teste teste1;                 teste1.SetMsg("aaa");                 vValue = (void*)&teste1;                 COPYDATASTRUCT dataToSend;     &

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

      Alex Cutovoi wrote:

      Teste __based(vValue) * Single;

      So what address is 4 bytes from vValue? Unless it is the beginning of ExibeMsg(), there's no way that the following could work:

      ttt->ExibeMsg();

      What you're doing seems very unreliable and dangerous. What is the receivng window supposed to do with the data it receives?


      "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

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

      A 1 Reply Last reply
      0
      • T toxcct

        KarstenK wrote:

        allocated with GlobalAlloc

        why don't you just new it ?


        Don't know where to start ?
        Refer the Forums Guidelines and ask a friend

        [VisualCalc 3.0][Flags Beginner's Guide]

        K Offline
        K Offline
        KarstenK
        wrote on last edited by
        #6

        if it is in the same app/process it is OK, but he didnt say that. In the same app I would use a global (memory) object.

        Greetings from Germany

        T 1 Reply Last reply
        0
        • A Alex Cutovoi

          In a site(www.flounder.com), I read this: "The problem of storing pointers is a little bit misleading. You must not store pointers in the structure you pass, even if they are pointers into the same area you are passing, because when the message is received the recipient has no control over where it is placed, and the pointers are likely to be nonsense. Obviously, you can't store pointers to other areas of your own memory because they will be completely nonsensical in the receiving process; they will be interpreted in its own address space. But the misleading aspect is that if you have a structure which is entirely self-contained in the data area you are sending over (that is, every pointer refers to another structure in the area you are sending), then you can use based pointers to encode them. Based pointers are actually relative offsets measured from a specific starting point (such as the start of the area being transmitted), and are automatically adjusted by the recipient (as long as you set up the correct base value) so they are valid pointers." So I have a based pointer in my class that is the same type of the class. This pointer must have to arrive(I think) to the other window safetly. toxcct, if I use new I lose the control over the pointer in the other window.

          K Offline
          K Offline
          KarstenK
          wrote on last edited by
          #7

          Try use GlobalAlloc()/GlobalFree, but check/test it on Vista. Read the MSDN.

          Greetings from Germany

          M 1 Reply Last reply
          0
          • K KarstenK

            if it is in the same app/process it is OK, but he didnt say that. In the same app I would use a global (memory) object.

            Greetings from Germany

            T Offline
            T Offline
            toxcct
            wrote on last edited by
            #8

            but GlobalAlloc() is not portable...


            Don't know where to start ?
            Refer the Forums Guidelines and ask a friend

            [VisualCalc 3.0][Flags Beginner's Guide]

            1 Reply Last reply
            0
            • K KarstenK

              Try use GlobalAlloc()/GlobalFree, but check/test it on Vista. Read the MSDN.

              Greetings from Germany

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #9

              What about GlobalAlloc()/GlobalFree changed on Vista? I see nothing in the PSDK. Mark

              1 Reply Last reply
              0
              • D David Crow

                Alex Cutovoi wrote:

                Teste __based(vValue) * Single;

                So what address is 4 bytes from vValue? Unless it is the beginning of ExibeMsg(), there's no way that the following could work:

                ttt->ExibeMsg();

                What you're doing seems very unreliable and dangerous. What is the receivng window supposed to do with the data it receives?


                "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

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

                A Offline
                A Offline
                Alex Cutovoi
                wrote on last edited by
                #10

                The code that I put in my question is the same for both windows. It doesn't change. If the way I choose is unreliable and dangerous, what you suggest for me to solve this?? I need a big help

                D 1 Reply Last reply
                0
                • A Alex Cutovoi

                  The code that I put in my question is the same for both windows. It doesn't change. If the way I choose is unreliable and dangerous, what you suggest for me to solve this?? I need a big help

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

                  Alex Cutovoi wrote:

                  The code that I put in my question is the same for both windows.

                  Which is irrelevant. What matters is that vValue plus 4 bytes does not reference ExibeMsg(), but references m_cMsg instead. Again I ask, what is it that you are trying to do?


                  "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

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

                  A 1 Reply Last reply
                  0
                  • D David Crow

                    Alex Cutovoi wrote:

                    The code that I put in my question is the same for both windows.

                    Which is irrelevant. What matters is that vValue plus 4 bytes does not reference ExibeMsg(), but references m_cMsg instead. Again I ask, what is it that you are trying to do?


                    "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

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

                    A Offline
                    A Offline
                    Alex Cutovoi
                    wrote on last edited by
                    #12

                    Ok, I need to pass an entire object between 2 windows. In both windows they recieve different handles. I only get to pass structs(which is very easy).

                    D 1 Reply Last reply
                    0
                    • A Alex Cutovoi

                      Ok, I need to pass an entire object between 2 windows. In both windows they recieve different handles. I only get to pass structs(which is very easy).

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

                      Try this:

                      // the structure to pass around
                      class MyStruct
                      {
                      public:
                      MyStruct()
                      {
                      m_pMsg = new char[128];
                      lstrcpy(m_pMsg, "Hello World");
                      }

                      ~MyStruct()
                      {
                         delete \[\] m\_pMsg;
                         m\_pMsg = NULL;
                      }
                      
                      char \*m\_pMsg;
                      
                      void Exec( void )
                      {
                          AfxMessageBox(m\_pMsg);
                      }
                      

                      };

                      // on the sending end
                      MyStruct myStruct;
                      COPYDATASTRUCT dataToSend;

                      dataToSend.cbData = sizeof(MyStruct);
                      dataToSend.lpData = &myStruct;
                      dataToSend.dwData = 0;

                      ::SendMessage(GetSafeHwnd(), WM_COPYDATA, (WPARAM) GetSafeHwnd(), (LPARAM)&dataToSend);

                      // on the receiving end
                      COPYDATASTRUCT *dataReceived = (COPYDATASTRUCT *)pCopyDataStruct;
                      MyStruct *pStruct = (MyStruct *) dataReceived->lpData;
                      pStruct->Exec();


                      "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

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

                      1 Reply Last reply
                      0
                      • A Alex Cutovoi

                        Hi for all I'm trying to pass some data between 2 windows. This is the class that I pass between my 2 windows. It's just a single class with a based pointer: #ifndef TESTE_H #define TESTE_H #include void * vValue; class Teste {     public:        Teste();        ~Teste();        void SetMsg(char * cMsg);        void ExibeMsg();        Teste __based(vValue) * Single;     private:        char * m_cMsg;        char * m_cClassName; }; Teste::Teste() {     if(m_cClassName == 0)m_cClassName = "Classe Teste"; } Teste::~Teste(){} void Teste::SetMsg(char * cMsg) {     m_cMsg = cMsg; } void Teste::ExibeMsg() {     if(m_cMsg != NULL)MessageBox(NULL, m_cMsg, "msg", MB_OK);} #endif TESTE_H This is the code that sends the data and handles the data: case WM_COPYDATA: {     COPYDATASTRUCT * dataReceived = (COPYDATASTRUCT*)lParam;     Teste * tempTeste = (Teste*)dataReceived->lpData;     Teste * ttt = tempTeste->Single;     char cResult[256] = {0};     int nCount = 0;     if(ttt != NULL)     {        MessageBox(NULL, "exibindo msg janela 1", "", MB_OK);        ttt->ExibeMsg();     }     break; } case WM_COMMAND: {     switch(LOWORD(wParam))     {        case IDC_BUTTON1:        {           char cLength[20], cSize[5];           if(HIWORD(wParam) == BN_CLICKED)           {              if(GetDlgItemText(hwnd, IDC_EDIT1, cLength, 20) != 0)              {                 Teste teste1;                 teste1.SetMsg("aaa");                 vValue = (void*)&teste1;                 COPYDATASTRUCT dataToSend;     &

                        M Offline
                        M Offline
                        Michael Dunn
                        wrote on last edited by
                        #14

                        You cannot pass a pointer between processes. All the data has to be self-contained in the block of memory that you refer to in the COPYDATASTRUCT.

                        --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ"); Ford, what's this fish doing in my ear?

                        A 1 Reply Last reply
                        0
                        • M Michael Dunn

                          You cannot pass a pointer between processes. All the data has to be self-contained in the block of memory that you refer to in the COPYDATASTRUCT.

                          --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ"); Ford, what's this fish doing in my ear?

                          A Offline
                          A Offline
                          Alex Cutovoi
                          wrote on last edited by
                          #15

                          Exactly, like I said to toxcct, if I do this, I lost the control of the pointer. And I did this before, Michael, can you tell me how can I solve? I've tested with structures, works very fine, but what I want is a object. Hence I'm trying to solve this using based pointers. It's my very first time using based pointers.

                          M 1 Reply Last reply
                          0
                          • A Alex Cutovoi

                            Exactly, like I said to toxcct, if I do this, I lost the control of the pointer. And I did this before, Michael, can you tell me how can I solve? I've tested with structures, works very fine, but what I want is a object. Hence I'm trying to solve this using based pointers. It's my very first time using based pointers.

                            M Offline
                            M Offline
                            Michael Dunn
                            wrote on last edited by
                            #16

                            You cannot pass a pointer between processes. A based pointer doesn't solve this, because it's just an offset from another pointer.

                            --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ"); Ford, what's this fish doing in my ear?

                            A 1 Reply Last reply
                            0
                            • M Michael Dunn

                              You cannot pass a pointer between processes. A based pointer doesn't solve this, because it's just an offset from another pointer.

                              --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ"); Ford, what's this fish doing in my ear?

                              A Offline
                              A Offline
                              Alex Cutovoi
                              wrote on last edited by
                              #17

                              That means, it's not possible to send an object to another window right? If an object is a pointer, there's not a way to send it. My app actually is passing structs, but what I want to pass is an object.

                              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