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. Function pointer corrupts the stack or heap

Function pointer corrupts the stack or heap

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionc++data-structures
6 Posts 2 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.
  • B Offline
    B Offline
    BadKarma
    wrote on last edited by
    #1

    Hi, I have encountered this really strange bug. For a new project I have split a old, working class and its member class into two single classes (functional, wrapper). When I use the same code that has worked for years, it crashes with heap or stack corruption. I have narrowed the bug to the use of a function pointer in the wrapper class to a member of the functional class. When i add this pointer as a member of the wrapper class, the corruption happenes i don't even have to call it. This is the code that does the woe.

    int _tmain(int argc, _TCHAR* argv[])
    {
      functional oFun;
    
      {
        // this is to use the scope effect
        wrapper wrap(&oFun);  
      }
      
      return 0;
    }
    

    When exiting the stack is corrupted. The is the code of the wrapper class --wrapper.h--

    #pragma once
    class functional;
    class wrapper
    {
    public:
      wrapper(functional* pFun);
      ~wrapper(void);
      
      void (functional::*pExitFunction)();	// the addition of this line causes the corruption
                                            // even if you don't use it in the cpp side
      functional* _pFun;
    };
    

    --wrapper.cpp--

    #include "wrapper.h"
    #include "functional.h"
    wrapper::wrapper(functional* pFun)
    {
      _pFun = pFun;
      
      pExitFunction = &functional::set_one;
    }
    wrapper::~wrapper(void)
    {
      (_pFun->*pExitFunction)();
    }
    

    This is the code of the functional class --functional.h--

    #pragma once
    
    class functional
    {
    public:
      functional(void);
      ~functional(void);
      void set_one();
      int _iData;
    };
    

    --functional.cpp--

    #include "functional.h"
    
    functional::functional(void)
    {
      _iData = 0;
    }
    
    functional::~functional(void)
    {
    }
    
    void functional::set_one()
    {
      _iData = 1;
    }
    

    Why does the stack get corrupted? I'm really stuck on this one, so any pointers would help me a lot.

    Learn from the mistakes of others, you may not live long enough to make them all yourself.

    S 1 Reply Last reply
    0
    • B BadKarma

      Hi, I have encountered this really strange bug. For a new project I have split a old, working class and its member class into two single classes (functional, wrapper). When I use the same code that has worked for years, it crashes with heap or stack corruption. I have narrowed the bug to the use of a function pointer in the wrapper class to a member of the functional class. When i add this pointer as a member of the wrapper class, the corruption happenes i don't even have to call it. This is the code that does the woe.

      int _tmain(int argc, _TCHAR* argv[])
      {
        functional oFun;
      
        {
          // this is to use the scope effect
          wrapper wrap(&oFun);  
        }
        
        return 0;
      }
      

      When exiting the stack is corrupted. The is the code of the wrapper class --wrapper.h--

      #pragma once
      class functional;
      class wrapper
      {
      public:
        wrapper(functional* pFun);
        ~wrapper(void);
        
        void (functional::*pExitFunction)();	// the addition of this line causes the corruption
                                              // even if you don't use it in the cpp side
        functional* _pFun;
      };
      

      --wrapper.cpp--

      #include "wrapper.h"
      #include "functional.h"
      wrapper::wrapper(functional* pFun)
      {
        _pFun = pFun;
        
        pExitFunction = &functional::set_one;
      }
      wrapper::~wrapper(void)
      {
        (_pFun->*pExitFunction)();
      }
      

      This is the code of the functional class --functional.h--

      #pragma once
      
      class functional
      {
      public:
        functional(void);
        ~functional(void);
        void set_one();
        int _iData;
      };
      

      --functional.cpp--

      #include "functional.h"
      
      functional::functional(void)
      {
        _iData = 0;
      }
      
      functional::~functional(void)
      {
      }
      
      void functional::set_one()
      {
        _iData = 1;
      }
      

      Why does the stack get corrupted? I'm really stuck on this one, so any pointers would help me a lot.

      Learn from the mistakes of others, you may not live long enough to make them all yourself.

      S Offline
      S Offline
      Saurabh Garg
      wrote on last edited by
      #2

      Are you sure this code has a bug? I coudn't find any problem with the code so I decide to run it myself and it runs perfectly.

      BadKarma wrote:

      void (functional::*pExitFunction)(); // the addition of this line causes the corruption // even if you don't use it in the cpp side

      This cannot be true because if you comment out this line then you will have to comment it in wrapper's constructor and destructor. If you do that than the class do nothing so it cannot corrupt memory. -Saurabh

      B 1 Reply Last reply
      0
      • S Saurabh Garg

        Are you sure this code has a bug? I coudn't find any problem with the code so I decide to run it myself and it runs perfectly.

        BadKarma wrote:

        void (functional::*pExitFunction)(); // the addition of this line causes the corruption // even if you don't use it in the cpp side

        This cannot be true because if you comment out this line then you will have to comment it in wrapper's constructor and destructor. If you do that than the class do nothing so it cannot corrupt memory. -Saurabh

        B Offline
        B Offline
        BadKarma
        wrote on last edited by
        #3

        Saurabh.Garg wrote:

        Are you sure this code has a bug?

        Unfortunatly, yes. I mean you're right, this code is valid(i have been using it for years in another project). But when i have ported it into an new project and after splitting the two classes the corruption takes place. Did you use two *.h and *.cpp file, or did't you implement them in a single file.

        Saurabh.Garg wrote:

        This cannot be true because if you comment out this line then you will have to comment it in wrapper's constructor and destructor. If you do that than the class do nothing so it cannot corrupt memory.

        Correct, i commented the usage of the function pointer in the constructor and destructor. Maybe I shouldn't have showed them in this example.

        Learn from the mistakes of others, you may not live long enough to make them all yourself.

        S 1 Reply Last reply
        0
        • B BadKarma

          Saurabh.Garg wrote:

          Are you sure this code has a bug?

          Unfortunatly, yes. I mean you're right, this code is valid(i have been using it for years in another project). But when i have ported it into an new project and after splitting the two classes the corruption takes place. Did you use two *.h and *.cpp file, or did't you implement them in a single file.

          Saurabh.Garg wrote:

          This cannot be true because if you comment out this line then you will have to comment it in wrapper's constructor and destructor. If you do that than the class do nothing so it cannot corrupt memory.

          Correct, i commented the usage of the function pointer in the constructor and destructor. Maybe I shouldn't have showed them in this example.

          Learn from the mistakes of others, you may not live long enough to make them all yourself.

          S Offline
          S Offline
          Saurabh Garg
          wrote on last edited by
          #4

          Well I tried with separate files, just like you mentioned, and I tried within a single file and this code runs perfectly fine. Btw I am using VS 2008. -Saurabh

          B 1 Reply Last reply
          0
          • S Saurabh Garg

            Well I tried with separate files, just like you mentioned, and I tried within a single file and this code runs perfectly fine. Btw I am using VS 2008. -Saurabh

            B Offline
            B Offline
            BadKarma
            wrote on last edited by
            #5

            Saurabh.Garg wrote:

            Btw I am using VS 2008.

            I'm still using VS 2005. But I have found a way to get it working. When i replace the forward class declaration with in including of the class, everything works fine?? :confused:

            #pragma once
            class functional;
            #include "functional.h"
            class wrapper
            {
            public:
              wrapper(functional* pFun);
              ~wrapper(void);
              
              void (functional::*pExitFunction)();	// the addition of this line causes the corruption
                                                    // even if you don't use it in the cpp side
              functional* _pFun;
            };
            

            Maybe its a bug in the VS 2005 compiler, I'm really not getting this. :omg: Either way its working now, thanks for the response anyway. :)

            Learn from the mistakes of others, you may not live long enough to make them all yourself.

            S 1 Reply Last reply
            0
            • B BadKarma

              Saurabh.Garg wrote:

              Btw I am using VS 2008.

              I'm still using VS 2005. But I have found a way to get it working. When i replace the forward class declaration with in including of the class, everything works fine?? :confused:

              #pragma once
              class functional;
              #include "functional.h"
              class wrapper
              {
              public:
                wrapper(functional* pFun);
                ~wrapper(void);
                
                void (functional::*pExitFunction)();	// the addition of this line causes the corruption
                                                      // even if you don't use it in the cpp side
                functional* _pFun;
              };
              

              Maybe its a bug in the VS 2005 compiler, I'm really not getting this. :omg: Either way its working now, thanks for the response anyway. :)

              Learn from the mistakes of others, you may not live long enough to make them all yourself.

              S Offline
              S Offline
              Saurabh Garg
              wrote on last edited by
              #6

              Hmmm thats interesting, it seems too simple to be a bug. Anyway its good you got it working. -Saurabh

              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