Click here to Skip to main content
15,889,027 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hallo community, i cant seem to find a solution to my current problem. I have 2 classes that run specific routines with timers on separate thread. The Timer i wrote works on same principles as the other but the app crashes when i need to callback from the class that has called the timer.

I have Class A which is initialized from the main.cpp. Class A then needs to initialize Class Alpha and Class Beta. Alpha and Beta use the Timer Class. The Timer class correctly calls back procedures on Alpha and Beta, but these two do not call back the procedure to Class A. The app compiles fine and it only crashed on call time.
Problem signature:
Problem Event Name: APPCRASH
Application Name: MyProgram.exe
Application Version: 0.0.0.0
Application Timestamp: 00050000
Fault Module Name: StackHash_5861
Fault Module Version: 0.0.0.0
Fault Module Timestamp: 00000000
Exception Code: c0000005
Exception Offset: PCH_72_FROM_ntdll+0x00072DA4
OS Version: 6.3.9600.2.0.0.256.48
Locale ID: 1033
Additional Information 1: 5861
Additional Information 2: 5861822e1919d7c014bbb064c64908b2
Additional Information 3: 4754
Additional Information 4: 47547fafc9a3073a5bbe8e69322a8db5




C++
//main.cpp with WINAPI using the WINAPI loop

A a1;
a1.Initialize();



C++
//A
void Initialize()
{
Alpha alpha1;     
alpha1.Create(&SHandler);
}
static void SHandler(string info)
{
A a1;
a1.Handler(info)
}
void Handler(string info)
{printf(info);}



C++
//Alpha
void (*_AddressAt)(string info);
void Create(void (*AddressAt)(string info))
{
_AddressAt = AddressAt;
Timer Timer1;
Timer1.AddHandler(&Shandler);
Timer1.Interval = 1000;
Timer1.Start();
}
static void SHandler()
{
Alpha alpha1;
alpha1.Handler();
}
void Handler()
{
_AddressAt("CallBack!"); //LINE THAT CRASHES APP 
}


C++
//Timer.h
#pragma once

class Timer
{
public:
    DWORD Interval = 1000;
    bool IsRunning();
    void AddHandler(void (*AddressOf)(void));
    void Start();
    void Stop();
private:
    void (*_AddressOf)(void);
    void Looper();
    bool _IsRunning = false;
};

//Timer.cpp
#include <windows.h>
#include <thread>
#include "Timer.h"

using namespace std;
void Timer::AddHandler(void (*AddressOf)(void))
{
    _AddressOf = AddressOf;
}

void Timer::Start()
{
    _IsRunning = true;
    std::thread first(Looper, this);
    first.join();
}

void Timer::Stop()
{
    _IsRunning = false;
}

void Timer::Looper()
{
    while (_IsRunning)
    {
        _AddressOf(); // make callback
        Sleep(Interval);
    }
}

bool Timer::IsRunning()
{
    return _IsRunning;
}


What I have tried:

---------------------------------------------------------
Posted
Updated 13-Jul-17 0:20am
Comments
Richard MacCutchan 24-Jun-17 4:23am    
The object alpha1 does not exist after the return from the call to a1.Initialize. Your code is over complicated and difficult to understand.
KarstenK 24-Jun-17 13:53pm    
Post it as answer to close the Q&A.
Philippe Mori 25-Jun-17 7:28am    
Learn C++ and multithreading. You have to understand the lifetime of an object. For example, your Create function timer is local to the function. Without such basic knowledge, you should not attempt to do multithreading... as you need to understand what you do first....

1 solution

You must change the scope of your object to a higher level. Some of them only exist in scope of a function like SHandler. (After leaving the function the objects get destroyed)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900