1

When I do this I get 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.

when calling dutyStack[0](); . But if I add dutyStack.reserve(10); or if I only have one element in the vector I do not get the error. I suspect something is going on when it's copying the elements when resizing the vector but I really have no idea.

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <functional>

using namespace std;
class Class1
{
public:
    void duty1(){cout<<"duty1";}
    void duty2(){cout<<"duty2";}
};

int _tmain(int argc, _TCHAR* argv[])
{
    vector<tr1::function<void()>> dutyStack;
    Class1 myclass;

    dutyStack.push_back( tr1::bind(&Class1::duty1, myclass) );
    dutyStack.push_back( tr1::bind(&Class1::duty2, myclass) );

    dutyStack[0]();
    dutyStack.erase(dutyStack.begin());
}

I'm under visual studio 2008, Windows 7.

DogDog
  • 4,820
  • 12
  • 44
  • 66

2 Answers2

4

I have exactly the same environment as yours (Win7, VS 2008). I've just compiled and executed successfuly your code.

To me it looks like you have to recreate your project as you might have changes calling convension or similar stuff:

Configuration Properties > C/C++ > Advanced > Calling Conventions

..so recreating your project is simpliest check if this is your issue. If not this I bet you have some of your header files corrupted.

Artur
  • 7,038
  • 2
  • 25
  • 39
  • 1
    I didn't test it but that is what I thought from reading the question. Nice clean response. Future reading for the question asker http://blogs.msdn.com/b/oldnewthing/archive/2004/01/08/48616.aspx – cbuteau Oct 31 '13 at 17:33
  • Works with VS2013 as well. The sample seems to be fine but the actual project not. – Alois Kraus Oct 31 '13 at 18:03
  • It seems to make sense, we had some changes in the project lately, I will see if I can do something with it soon. – DogDog Nov 04 '13 at 14:40
2

Do you really need "stdafx.h" ? Aka, do you have pre-defined parameters for your project? Because the following works on vs2012, vs2010:

#include <iostream>
#include <vector>
#include <functional>

using namespace std;
class Class1
{
public:
    void duty1(){cout<<"duty1";}
    void duty2(){cout<<"duty2";}
};

int main()
{
    vector<tr1::function<void()>> dutyStack;
    Class1 myclass;

    dutyStack.push_back( tr1::bind(&Class1::duty1, myclass) );
    dutyStack.push_back( tr1::bind(&Class1::duty2, myclass));

    dutyStack[0]();
    dutyStack.erase(dutyStack.begin());

    system("pause");
    return 0;
}

Note that I removed "stdafx.h" and renamed "_tmain" as "main". Also removed args in main declaration as I didn't need them there.

I read something here that might have something common (_stdcall as _cecl call I would believe, but I am not far into C++ for this)