C ++类,它使用带有线程崩溃的计时器,使用2个递归回调 [英] C++ class that uses timer with thread crashes using 2 recursive callbacks
问题描述
我有A类从main.cpp初始化。然后,A类需要初始化Class Alpha和Class Beta。 Alpha和Beta使用Timer类。 Timer类正确地回调了Alpha和Beta上的程序,但是这两个程序没有将程序回调到A类。应用程序编译正常,它只在通话时崩溃。
问题签名:
问题事件名称:APPCRASH
应用程序名称:MyProgram.exe
应用程序版本:0.0.0.0
应用程序时间戳:00050000
故障模块名称:StackHash_5861
故障模块版本:0.0.0.0
故障模块时间戳:00000000
异常代码:c0000005
异常偏移:PCH_72_FROM_ntdll + 0x00072DA4
操作系统版本:6.3.9600.2.0.0.256.48
地区ID:1033
附加信息1:5861
附加信息2:5861822e1919d7c014bbb064c64908b2
附加信息3:4754
附加信息4:47547fafc9a3073a5bbe8e69322a8db5
// 使用WINAPI循环的WINAPI的main.cpp
A a1;
a1.Initialize();
< span class =code-comment> // A
void 初始化()
{
Alpha alpha1;
alpha1.Create(& SHandler);
}
静态 void SHandler(字符串信息)
{
A a1;
a1.Handler(info)
}
void 处理程序(字符串信息)
{printf(info);}
/ / Alpha
void (* _AddressAt)(字符串信息);
void 创建( void (* AddressAt)(字符串信息))
{
_AddressAt = AddressAt;
定时器Timer1;
Timer1.AddHandler(& Shandler);
Timer1.Interval = 1000 ;
Timer1.Start();
}
静态 void SHandler()
{
Alpha alpha1;
alpha1.Handler();
}
void Handler()
{
_AddressAt( CallBack!); // 线程崩溃APP
}
// Timer.h
#pragma once
class 计时器
{
public :
DWORD Interval = 1000 ;
bool IsRunning();
void AddHandler( void (* AddressOf)(空隙跨度>));
void Start();
void Stop();
private :
void (* _AddressOf)(空隙跨度>);
void Looper();
bool _IsRunning = false ;
};
// Timer.cpp
#include < windows.h >
#include < thread >
#include Timer.h
使用 namespace std;
void Timer :: AddHandler( void (* AddressOf)( void ))
{
_AddressOf = AddressOf;
}
void Timer :: Start()
{
_IsRunning = 真跨度>;
std :: thread first(Looper, this );
first.join();
}
void Timer :: Stop()
{
_IsRunning = 假跨度>;
}
void Timer :: Looper()
{
while (_IsRunning)
{
_AddressOf(); // make callback
Sleep(Interval);
}
}
bool Timer :: IsRunning()
{
return _IsRunning;
}
我的尝试:
-------------------------------------------- -------------
您必须将对象的范围更改为更高级别。其中一些只存在于像SHandler这样的函数的范围中。 (离开函数后,对象被破坏)
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
//main.cpp with WINAPI using the WINAPI loop
A a1;
a1.Initialize();
//A
void Initialize()
{
Alpha alpha1;
alpha1.Create(&SHandler);
}
static void SHandler(string info)
{
A a1;
a1.Handler(info)
}
void Handler(string info)
{printf(info);}
//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
}
//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:
---------------------------------------------------------
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)
这篇关于C ++类,它使用带有线程崩溃的计时器,使用2个递归回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!