Function pointer corrupts the stack or heap
-
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.
-
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.
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
-
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
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.
-
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.
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
-
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
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.
-
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.
Hmmm thats interesting, it seems too simple to be a bug. Anyway its good you got it working. -Saurabh