Why does the below crash? My pointer is still alive and is constructed in the main so it should not die until main returns :S

I was using this with createthread a while back but my Mingw4.5 never supported it. I just upgraded to 4.7 but now this crashes and if I use it with CreateThread, it does not crash but rather it prints nothing. So I decided to do the following to test why CreateThread did nothing with the pointer but instead I get a bad crash.

void Meh(int X, int Y)
{
    std::cout<< X <<" | "<<Y;
}

void Test(LPVOID lpParameter)
{
    std::function<void()> func = *((std::function<void()>*)lpParameter);
    func();
}

void Run(std::function<void()>* F)
{
    Test((void*)&F);
}


int main(int argc, char* argv[])
{
    //std::cout.flags(std::ios::boolalpha);

    std::function<void()> F = std::bind(Meh, 10, 10);
    Run(&F);

    return 0;
};

Edited 4 Years Ago by triumphost

It's a simple typo. Line 14 should be:

Test((void*)F);  // notice no & here.

Because when you do &F, the type of that expression will be std::function<void()>**, and then, later you cast that into the type std::function<void()>*, and thus, the crash (or undefined behavior).

Comments
Very good eye lol didn't notice that

Hey is that (void) needed? I removed the void and it still worked :S so is there a need for casting? Also is there a way to do it with class functions? I have problems doing that with Class Functions since I don't have the grasp of it yet.

//Where X is just a class that has the functions previously posted but working.

int main()
{
    Debug X;
    std::function<void()> F = std::bind(X.DebugWindow, "one", "two", 100, 100);  //Also tried: &Debug::DebugWindow
    X.Run(&F);
}

//I cannot pass X's functions to it?

Other than that, it works fine if I do not pass it class functions. And it works fine without the void* but I'm not sure why it works without the cast?

Edited 4 Years Ago by triumphost

Hey is that (void) needed? I removed the void and it still worked

Can you post that code. I'm not sure what you mean, where you removed the void* cast. If you only remove the cast to a void* type, it should still work because all pointers are implicitly convertible to a void* pointer (this is a relic of C). But you will still need the cast from void* to std::function<void()>*. Also, you should get used to using the C++ cast operators instead of the C-style casts. In other words, you should use:

std::function<void()> func = *static_cast<std::function<void()>*>(lpParameter);

I have problems doing that with Class Functions since I don't have the grasp of it yet.

To use bind with class member functions, you have to provide the pointer to the member function, and the object on which to call it. As so:

Debug X;
std::function<void()> F = std::bind(&Debug::DebugWindow, &X, "one", "two", 100, 100);

This the same mechanism as for boost::bind, see here.

Edited 4 Years Ago by mike_2000_17: typo

Comments
:D I'm quite happy today

instead of Test((void*)F); I just did Test(F); and it worked but like you said, I should get used to the C++ style casting so I replaced it with the static cast you have above. I have to read a tutorial on C++ style casting though as I only know the C-style.

For now I'll mark the thread solved. +Rep+ to you sir :)

This question has already been answered. Start a new discussion instead.