pointer woes

A

Ant

Hi,

I am having trouble with a member function pointer. It sees to give me the
followign error

Run-Time Check Failure #0 - The value of ESP was not properly saved across a
function call.

It only seems to give me problems if I use the friend class declaration

Any thoughs ?

Code Follows:


#include "rootclass.h"

int _tmain(int argc, _TCHAR* argv[])
{
CRootClass *testclass;
testclass = new CRootClass;
testclass->TestFunction();

delete testclass;
return 0;
}


class CStore
{
public:
CStore(void);
~CStore(void);
friend class CRootClass;
typedef void(CRootClass::*FunctionPtr)(void);
void PassPtr(FunctionPtr FPtr);
};

#include ".\store.h"

CStore::CStore(void)
{
}

CStore::~CStore(void)
{
}

void CStore::passPtr(FunctionPtr FPtr)
{
//something happens with ptr
}



#include ".\store.h"
class CRootClass
{
public:
CRootClass(void);
~CRootClass(void);

CStore *m_Store;
void TestFunction(void);
};


#include ".\rootclass.h"

CRootClass::CRootClass(void)
{
m_Store = new CStore();
m_Store->PassPtr(&CRootClass::TestFunction );
}
CRootClass::~CRootClass(void)
{
delete m_Store;
}
void CRootClass::TestFunction(void)
{
//some code
}
 
B

Bob Hairgrove

Hi,

I am having trouble with a member function pointer. It sees to give me the
followign error

Run-Time Check Failure #0 - The value of ESP was not properly saved across a
function call.

It only seems to give me problems if I use the friend class declaration

Any thoughs ?

Code Follows:

[snip]

Your code compiles and runs fine, although it doesn't do anything. You
need to show us the code that produces the error.

(BTW, please refrain from posting code which uses non-standard macros
and extensions such as _tmain and TCHAR).
 
A

Ant

Bob Hairgrove said:
Hi,

I am having trouble with a member function pointer. It sees to give me the
followign error

Run-Time Check Failure #0 - The value of ESP was not properly saved across
a
function call.

It only seems to give me problems if I use the friend class declaration

Any thoughs ?

Code Follows:

[snip]

Your code compiles and runs fine, although it doesn't do anything. You
need to show us the code that produces the error.

(BTW, please refrain from posting code which uses non-standard macros
and extensions such as _tmain and TCHAR).

Sorry about the TCHAR business, didnt notice that .

Interesting you say it runs fine, this is the code my compiler stops on. The
debug version I compile has some run time checks. It would appear that the
when my root class passes a pointer it somehow corrupts the stack pointer,
or something along those lines.

Using Visual Studio 2003 it produces this error

"Run-Time Check Failure #0 - The value of ESP was not properly saved across
a function call. This is usually a result of calling a function declared
with one calling convention with a function pointer declared with a
different calling convention.

The program '[3768] friendtest.exe: Native' has exited with code -2147483645
(0x80000003)." (One or more arguments are invalid )


I am not sure why this happenes, all the calling conventions are the same
 
B

Bob Hairgrove

Interesting you say it runs fine, this is the code my compiler stops on. The
debug version I compile has some run time checks. It would appear that the
when my root class passes a pointer it somehow corrupts the stack pointer,
or something along those lines.

Using Visual Studio 2003 it produces this error

"Run-Time Check Failure #0 - The value of ESP was not properly saved across
a function call. This is usually a result of calling a function declared
with one calling convention with a function pointer declared with a
different calling convention.

The program '[3768] friendtest.exe: Native' has exited with code -2147483645
(0x80000003)." (One or more arguments are invalid )


I am not sure why this happenes, all the calling conventions are the same

Here is the code I compiled and ran (all in one file instead of
different headers). It is essentially equivalent to the code you
posted. However, I did remove the "(void)" bits since that just isn't
C++, as well as changed _tmain to main:

// test_store.cpp
class CStore
{
public:
CStore();
~CStore();
friend class CRootClass;
typedef void(CRootClass::*FunctionPtr)();
void PassPtr(FunctionPtr FPtr);
};

CStore::CStore()
{
}

CStore::~CStore()
{
}

void CStore::passPtr(FunctionPtr FPtr)
{
//something happens with ptr
}


class CRootClass
{
public:
CRootClass();
~CRootClass();

CStore *m_Store;
void TestFunction();
};


CRootClass::CRootClass()
{
m_Store = new CStore();
m_Store->PassPtr(&CRootClass::TestFunction );
}

CRootClass::~CRootClass()
{
delete m_Store;
}

void CRootClass::TestFunction()
{
//some code
}

int main()
{
CRootClass *testclass;
testclass = new CRootClass;
testclass->TestFunction();

delete testclass;
return 0;
}
 
D

Daniel T.

"Ant said:
Hi,

I am having trouble with a member function pointer. It sees to give me the
followign error

Run-Time Check Failure #0 - The value of ESP was not properly saved across a
function call.

It only seems to give me problems if I use the friend class declaration

Any thoughs ?

The code you posted is fine, your problem lies elsewhere. Somewhere in
your program you are writing to invalid memory or writing past the end
of an array, this is destroying the runtime environment in unpredictable
ways.

You need to find out where the error really is. Good luck.
 
A

Ant

thanks for the replies.

I can appriciate that it that it would appear the error lies elsewhere, I
thought so too. The code posted is the entire code , the commens are not
snips to make it easier to post, its the entire program. I suspect its
something to do with a Microsoft compiler flag I need to set/unset

To demonstrate that this is the code that fails and it does not lie
elsewhere in some unsibmited code I can like you to my source files. Sorry
but they are in MS format, its the only compiler I have

http://www.teamkk.com/temp/friendtest.zip
 
F

Frank Schmidt

Ant said:
thanks for the replies.

I can appriciate that it that it would appear the error lies elsewhere, I
thought so too. The code posted is the entire code , the commens are not
snips to make it easier to post, its the entire program. I suspect its
something to do with a Microsoft compiler flag I need to set/unset

To demonstrate that this is the code that fails and it does not lie
elsewhere in some unsibmited code I can like you to my source files. Sorry
but they are in MS format, its the only compiler I have

http://www.teamkk.com/temp/friendtest.zip

With all the single files, a #include "rootclass.h" in store.cpp and some
#pragma once in the headers seems to solve the problem. At least on vs2005.

I'm tiered to find the explanation somewhere in "type not known enought for
a stupid compiler"
 
D

Daniel T.

"Ant said:
thanks for the replies.

I can appriciate that it that it would appear the error lies elsewhere, I
thought so too. The code posted is the entire code , the commens are not
snips to make it easier to post, its the entire program. I suspect its
something to do with a Microsoft compiler flag I need to set/unset

To demonstrate that this is the code that fails and it does not lie
elsewhere in some unsibmited code I can like you to my source files. Sorry
but they are in MS format, its the only compiler I have

http://www.teamkk.com/temp/friendtest.zip

The code above gives me an idea. Where do you make these CRootClass
objects? If you try to make one *inside* CStore, that would be a real
problem.
 
M

Michiel.Salters

Ant said:
Hi,

I am having trouble with a member function pointer. It sees to give me the
followign error

Run-Time Check Failure #0 - The value of ESP was not properly saved across a
function call.

As your code is incomplete, I'm going to guess based on symptoms. I
guess
you're improperly mixing member function pointers and normal function
pointer.
This probably takes the form of a C cast, or a reinterpret_cast.

NB. Pointers to static member functions have the same type as to free
functions.

Regards,
Michiel Salters
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,301
Latest member
BuyPureganics

Latest Threads

Top