Load data from .ini crash my game! What can I do? c++ - c++

Hello I am trying to create a script to save/load my custom Weapon Layout for my game.
The save function works fine, but if I want load the layout, it crash my game.
I know where the problem is, but idk how to fix these, so I need your knowledge!
void SaveWeaponsLayout()
{
Ini wpLayout = (".\\Files\\Settings\\WeaponsLayout.ini");
Log::Msg("Reading Data for WeaponsLayout.ini!");
Player playerPed = PLAYER::PLAYER_PED_ID();
if (!ENTITY::IS_ENTITY_DEAD(playerPed))
{
for (auto & wpID : weaponLayoutList)
{
Hash weapon = wpID.weaponHash;
char * wpName = wpID.WeaponName;
if (WEAPON::HAS_PED_GOT_WEAPON(playerPed, wpID.weaponHash, 0))
{
int weaponInHex = weapon;
std::string weaponInHex_ = hexify< int >(weaponInHex);
wpLayout.WriteString(weaponInHex_, wpName, "WEAPONHASH");
}
for (auto & wpCompID : weaponComponentList)
{
Hash weaponComp = wpCompID.weaponCompHash;
char * wpCompName = wpCompID.weaponComponent;
if (WEAPON::HAS_PED_GOT_WEAPON_COMPONENT(playerPed, wpID.weaponHash, wpCompID.weaponCompHash))
{
int weaponCompInHex = weaponComp;
std::string weaponCompInHex_ = hexify< int >(weaponCompInHex);
wpLayout.WriteString(weaponCompInHex_, wpName, wpCompName);
}
}
}
}
Log::Msg("Files WeaponsLayout.ini created!");
Notifications::MessageCentre6000("~p~Saved~s~: Weapons Layout");
}
The list where I get the Weapon Data from the game >>
static struct wpList
{
__int64 weaponHash;
char * WeaponName;
};
static std::vector<wpList> weaponLayoutList{
//{ 0xA2719263, "Unarmed" },
{ 0xFBAB5776, "Parachute" },
{ 0x99B507EA, "Knife" },
{ 0x8BB05FD7, "Flashlight" },
{ 0xCD274149, "Battle Axe" },
{ 0x94117305, "Pool Cue" },
...
}; // 88x Weapons
The list where I get the Weapon Components Data from the game >>
static struct wpCompList
{
unsigned int weaponCompHash;
char * weaponComponent;
};
static std::vector<wpCompList> weaponComponentList{
{ 0x75414F30, "COMPONENT_AT_RAILCOVER_01" },
{ 0x0C164F53, "COMPONENT_AT_AR_AFGRIP" },
{ 0x359B7AAE, "COMPONENT_AT_PI_FLSH" },
{ 0x7BC4CDDC, "COMPONENT_AT_AR_FLSH" },
{ 0x9D2FBF29, "COMPONENT_AT_SCOPE_MACRO" },
...
}; // 551x Weapon Components
This is the WeaponsLayout.ini what I have saved!
[Micro SMG]
WEAPONHASH=0x13532244
COMPONENT_AT_PI_FLSH=0x359b7aae
COMPONENT_AT_AR_SUPP_02=0xa73d4664
COMPONENT_MICROSMG_CLIP_02=0x10e6ba2b
[Assault Rifle]
WEAPONHASH=0xbfefff6d
COMPONENT_ASSAULTRIFLE_CLIP_03=0xdbf0a53d
[Advanced Rifle]
WEAPONHASH=0xaf113f99
COMPONENT_AT_AR_FLSH=0x7bc4cddc
COMPONENT_ADVANCEDRIFLE_CLIP_01=0xfa8fa10f
[Pump Shotgun]
WEAPONHASH=0x1d073a89
COMPONENT_AT_AR_FLSH=0x7bc4cddc
COMPONENT_AT_SR_SUPP=0xe608b35e
COMPONENT_PUMPSHOTGUN_CLIP_01=0xd16f1438
COMPONENT_PUMPSHOTGUN_VARMOD_LOWRIDER=0xa2d79ddb
...so now my script what make me problems, because it search in my file for all Weapons & Weapon Components!
void LoadWeaponsLayout()
{
Log::Msg("Files reading WeaponsLayout.ini!");
Ini wpLayout = (".\\Files\\Settings\\WeaponsLayout.ini");
Player playerPed = PLAYER::PLAYER_PED_ID();
if (!ENTITY::IS_ENTITY_DEAD(playerPed))
{
for (auto wpID : weaponLayoutList)
{
char * wpName = wpID.WeaponName;
unsigned weaponX = wpLayout.GetInt(wpName, "WEAPONHASH");
if (!WEAPON::HAS_PED_GOT_WEAPON(playerPed, weaponX, 0))
{
WEAPON::GIVE_DELAYED_WEAPON_TO_PED(playerPed, weaponX, 9999, 0);
for (auto wpCompID : weaponComponentList)
{
char * wpCompoName = wpCompID.weaponComponent;
unsigned weaponComp = wpLayout.GetInt(wpName, wpCompoName);
WAIT(0);
if (WEAPON::DOES_WEAPON_TAKE_WEAPON_COMPONENT(weaponX, weaponComp))
{
WEAPON::GIVE_WEAPON_COMPONENT_TO_PED(playerPed, weaponX, weaponComp);
}
}
}
}
}
Log::Msg("Files WeaponsLayout.ini loaded!");
Notifications::MessageCentre4000("~p~Loaded~s~: Weapons Layout");
}
[my ini class]
class Ini
{
private:
std::string inifile;
public:
Ini(std::string file)
{
this->inifile = file;
}
void WriteString(std::string string, std::string app, std::string key)
{
WritePrivateProfileStringA(app.c_str(), key.c_str(), string.c_str(), this->inifile.c_str());
}
std::string GetString(std::string app, std::string key)
{
char buf[100];
GetPrivateProfileStringA(app.c_str(), key.c_str(), "NULL", buf, 100, this->inifile.c_str());
return (std::string)buf;
}
void WriteInt(int value, std::string app, std::string key)
{
WriteString(std::to_string(value), app, key);
}
int GetInt(std::string app, std::string key)
{
return std::stoi(GetString(app, key));
}
}
DEBUG:
Files reading WeaponsLayout.ini!
Exception thrown at 0x000007FEFD71A06D in GTA5.exe: Microsoft C++ exception: std::invalid_argument at memory location 0x000000013AD4E610.
The thread 0x4628 has exited with code 0 (0x0).
The thread 0x4d78 has exited with code 0 (0x0).
The thread 0x4cd0 has exited with code 0 (0x0).
The thread 0x3c20 has exited with code 0 (0x0).
The thread 0x3678 has exited with code 0 (0x0).
The thread 0x1278 has exited with code 0 (0x0).
The thread 0x59a0 has exited with code 0 (0x0).
The thread 0x5458 has exited with code 0 (0x0).
The thread 0x4a8c has exited with code 0 (0x0).
The thread 0x5300 has exited with code 0 (0x0).
The thread 0x4b20 has exited with code 0 (0x0).
The thread 0x526c has exited with code 0 (0x0).
The thread 0x500c has exited with code 0 (0x0).
The thread 0x4be0 has exited with code 0 (0x0).
The thread 0x266c has exited with code 0 (0x0).
The thread 0x59ec has exited with code 0 (0x0).
The thread 0x3020 has exited with code 0 (0x0).
The thread 0x581c has exited with code 0 (0x0).
The thread 0x5098 has exited with code 0 (0x0).
The thread 0xd98 has exited with code 0 (0x0).
The thread 0x5180 has exited with code 0 (0x0).
The thread 0x1cc8 has exited with code 0 (0x0).
The thread 0x4c84 has exited with code 0 (0x0).
The thread 0x112c has exited with code 0 (0x0).
The thread 0x2dc0 has exited with code 0 (0x0).
The thread 0x5910 has exited with code 0 (0x0).
The thread 0x59cc has exited with code 0 (0x0).
The thread 0x2eb4 has exited with code 0 (0x0).
The thread 0x5aec has exited with code 0 (0x0).
The thread 0x58cc has exited with code 0 (0x0).
The thread 0x5a94 has exited with code 0 (0x0).
The thread 0x481c has exited with code 0 (0x0).
The thread 0x5554 has exited with code 0 (0x0).
The thread 0x2358 has exited with code 0 (0x0).
The thread 0x1b70 has exited with code 0 (0x0).
The thread 0x5764 has exited with code 0 (0x0).
The thread 0x53cc has exited with code 0 (0x0).
The thread 0x4ba4 has exited with code 0 (0x0).
The thread 0x4008 has exited with code 0 (0x0).
The thread 0x566c has exited with code 0 (0x0).
The thread 0x415c has exited with code 0 (0x0).
The thread 0x4cc0 has exited with code 0 (0x0).
The thread 0x512c has exited with code 0 (0x0).
The thread 0x5614 has exited with code 0 (0x0).
The thread 0x5390 has exited with code 0 (0x0).
The thread 0x4d68 has exited with code 0 (0x0).
The thread 0x56e8 has exited with code 0 (0x0).
The thread 0x54e8 has exited with code 0 (0x0).
The thread 0x1bc0 has exited with code 0 (0x0).
The thread 0x55d0 has exited with code 0 (0x0).
The thread 0x4f1c has exited with code 0 (0x0).
The thread 0x4efc has exited with code 0 (0x0).
The thread 0x54b4 has exited with code 0 (0x0).
The thread 0x4144 has exited with code 0 (0x0).
The thread 0x614 has exited with code 0 (0x0).
The thread 0x5820 has exited with code 0 (0x0).
The thread 0x4878 has exited with code 0 (0x0).
The thread 0x4184 has exited with code 0 (0x0).
The thread 0x2064 has exited with code 0 (0x0).
The thread 0x568c has exited with code 0 (0x0).
The thread 0x30cc has exited with code 0 (0x0).
The thread 0x57d8 has exited with code 0 (0x0).
The thread 0x5658 has exited with code 0 (0x0).
The thread 0x4c94 has exited with code 0 (0x0).
The thread 0x5084 has exited with code 0 (0x0).
The thread 0x4ff4 has exited with code 0 (0x0).
The thread 0x5858 has exited with code 0 (0x0).
The thread 0x148c has exited with code 0 (0x0).
The thread 0x209c has exited with code 0 (0x0).
The thread 0x52bc has exited with code 0 (0x0).
The thread 0x5170 has exited with code 0 (0x0).
The thread 0x1388 has exited with code 0 (0x0).
The thread 0xc10 has exited with code 0 (0x0).
The thread 0x12c0 has exited with code 0 (0x0).
The thread 0xca8 has exited with code 0 (0x0).
The thread 0x2040 has exited with code 0 (0x0).
The thread 0x4d1c has exited with code 0 (0x0).
The thread 0x358c has exited with code 0 (0x0).
The thread 0x1eac has exited with code 0 (0x0).
The thread 0x5608 has exited with code 0 (0x0).
The thread 0x28d8 has exited with code 0 (0x0).
The thread 0x4c44 has exited with code 0 (0x0).
The thread 0x5a0c has exited with code 0 (0x0).
The thread 0x4420 has exited with code 0 (0x0).
The thread 0x5828 has exited with code 0 (0x0).
The thread 0x29f4 has exited with code 0 (0x0).
The thread 0x4e60 has exited with code 0 (0x0).
The thread 0x54c4 has exited with code 0 (0x0).
The thread 0x4564 has exited with code 0 (0x0).
The thread 0x50ec has exited with code 0 (0x0).
The thread 0x43cc has exited with code 0 (0x0).
The thread 0x4e74 has exited with code 0 (0x0).
The thread 0x549c has exited with code 0 (0x0).
The thread 0xd74 has exited with code 0 (0x0).
The thread 0x5b90 has exited with code 0 (0x0).
The thread 0x4f7c has exited with code 0 (0x0).
The thread 0x30e8 has exited with code 0 (0x0).
The thread 0x4f74 has exited with code 0 (0x0).
The thread 0x1530 has exited with code 0 (0x0).
The thread 0x5584 has exited with code 0 (0x0).
DXGI WARNING: Process is terminating. Using simple reporting. Please call ReportLiveObjects() at runtime for standard reporting. [ STATE_CREATION WARNING #0: ]
DXGI WARNING: Live Producer at 0x0000000000474EA8, Refcount: 4. [ STATE_CREATION WARNING #0: ]
DXGI WARNING: Live Object at 0x0000000000488880, Refcount: 4. [ STATE_CREATION WARNING #0: ]
DXGI WARNING: Live Object at 0x00000000004640E0, Refcount: 1. [ STATE_CREATION WARNING #0: ]
DXGI WARNING: Live Object : 2 [ STATE_CREATION WARNING #0: ]
The program '[22656] GTA5.exe' has exited with code 0 (0x0).

Related

OpenCV unhandled exception

I am a hardware (arduino hobbyist) programmer trying to get into OpenCV but struggling with a random error.
I have been following Adam Hacks youtibe guide to OpenCV and up to very recently it has worked great. I had compiled OpenCV as 32bit as the prebuilt was only available in x64 and my intended application (driving a set of servos via a FT232H usb to I2C adapter) was in 32 bit.
I typed in his code for "Using OpenCV and Haar Cascades to Detect Faces in a Video [C++]" and it worked fine (see below). It identified my face (some of the time) and drew a box round it. Yeah I was very happy.
The code:
#include <opencv2/opencv.hpp>
#include <vector>
using namespace std;
/* Include D2XX header*/
#include "ftd2xx.h"
/* Include libMPSSE header */
#include "libMPSSE_i2c.h"
#include "FT232.h"
using namespace cv;
int main()
{
//FT232 ft232;
//ft232.setUpFT232H();
double scale = 1.0;
CascadeClassifier faceCascade;
faceCascade.load("C:\\OpenCV32\\etc\\haarcascade_frontalface_alt.xml");
VideoCapture cap(0);
if (!cap.isOpened())
return -1;
for (;;)
{
Mat frame;
cap >> frame;
Mat grayscale;
cvtColor(frame, grayscale, COLOR_BGR2GRAY);
resize(grayscale, grayscale, Size(grayscale.size().width / scale, grayscale.size().height / scale));
vector <Rect> faces;
faceCascade.detectMultiScale(grayscale, faces, 1.1, 3, 0, Size(30, 30));
for (Rect area : faces) {
Scalar drawColor = Scalar(255, 0, 0);
rectangle(frame, Point(cvRound(area.x * scale), cvRound(area.y * scale)),
Point(cvRound((area.x + area.width-1)*scale), cvRound((area.y + area.height - 1) * scale)), drawColor);
}
imshow("Webcam Frame", frame);
if (waitKey(30) >= 0)
break;
}
return 0;
}
Then with no intentional changes on my part other that swapping about from one haarcascade to another the program stopped working and threw an unhandled error.
Unhandled exception at 0x76979862 in OpenCV_Nerf.exe: Microsoft C++ exception: cv::Exception at memory location 0x00EFF428. occurred
in this part of the system.cpp code
void error( const Exception& exc )
{
#ifdef CV_ERROR_SET_TERMINATE_HANDLER
{
cv::AutoLock lock(getInitializationMutex());
if (!cv_terminate_handler_installed)
{
if (param_setupTerminateHandler)
cv_old_terminate_handler = std::set_terminate(cv_terminate_handler);
cv_terminate_handler_installed = true;
}
cv_terminate_handler_exception = exc;
}
#endif
if (customErrorCallback != 0)
customErrorCallback(exc.code, exc.func.c_str(), exc.err.c_str(),
exc.file.c_str(), exc.line, customErrorCallbackData);
else if (param_dumpErrors)
{
dumpException(exc);
}
if(breakOnError)
{
static volatile int* p = 0;
*p = 0;
}
throw exc;
#ifdef __GNUC__
# if !defined __clang__ && !defined __APPLE__
// this suppresses this warning: "noreturn" function does return [enabled by default]
__builtin_trap();
// or use infinite loop: for (;;) {}
# endif
#endif
}
the error box pointing to the very last line.
The camera still functions and I have narrowed it down to the line
faceCascade.detectMultiScale(grayscale, faces, 1.1, 3, 0, Size(30, 30));
Build output: (I don't remember the warnings about loss of data first few time,, but I can't be sure)
1>------ Rebuild All started: Project: OpenCV_Nerf, Configuration: Debug Win32 ------
1>Source.cpp
1>D:\Vincent\Documents\Programming\Visual Studio Projects\OpenCV_Nerf\Source.cpp(32,93): warning C4244: 'argument': conversion from 'double' to '_Tp', possible loss of data
1> with
1> [
1> _Tp=int
1> ]
1>D:\Vincent\Documents\Programming\Visual Studio Projects\OpenCV_Nerf\Source.cpp(32,60): warning C4244: 'argument': conversion from 'double' to '_Tp', possible loss of data
1> with
1> [
1> _Tp=int
1> ]
1>OpenCV_Nerf.vcxproj -> D:\Vincent\Documents\Programming\Visual Studio Projects\OpenCV_Nerf\Debug\OpenCV_Nerf.exe
1>Done building project "OpenCV_Nerf.vcxproj".
Debug output (last bit as it was over character count)
'OpenCV_Nerf.exe' (Win32): Loaded 'C:\Windows\System32\DriverStore\FileRepository\nv_dispi.inf_amd64_95bdb3a23d6478de\NvCamera\NvCameraWhitelisting32.dll'. Symbol loading disabled by Include/Exclude setting.
'OpenCV_Nerf.exe' (Win32): Unloaded 'C:\Windows\System32\DriverStore\FileRepository\nv_dispi.inf_amd64_95bdb3a23d6478de\NvCamera\NvCameraWhitelisting32.dll'
'OpenCV_Nerf.exe' (Win32): Loaded 'C:\Windows\System32\DriverStore\FileRepository\nv_dispi.inf_amd64_95bdb3a23d6478de\NvCamera\NvCameraWhitelisting32.dll'. Symbol loading disabled by Include/Exclude setting.
'OpenCV_Nerf.exe' (Win32): Unloaded 'C:\Windows\System32\DriverStore\FileRepository\nv_dispi.inf_amd64_95bdb3a23d6478de\NvCamera\NvCameraWhitelisting32.dll'
'OpenCV_Nerf.exe' (Win32): Loaded 'C:\Windows\System32\DriverStore\FileRepository\nv_dispi.inf_amd64_95bdb3a23d6478de\NvCamera\NvCameraWhitelisting32.dll'. Symbol loading disabled by Include/Exclude setting.
'OpenCV_Nerf.exe' (Win32): Unloaded 'C:\Windows\System32\DriverStore\FileRepository\nv_dispi.inf_amd64_95bdb3a23d6478de\NvCamera\NvCameraWhitelisting32.dll'
'OpenCV_Nerf.exe' (Win32): Loaded 'C:\Windows\System32\DriverStore\FileRepository\nv_dispi.inf_amd64_95bdb3a23d6478de\NvCamera\NvCameraWhitelisting32.dll'. Symbol loading disabled by Include/Exclude setting.
'OpenCV_Nerf.exe' (Win32): Unloaded 'C:\Windows\System32\DriverStore\FileRepository\nv_dispi.inf_amd64_95bdb3a23d6478de\NvCamera\NvCameraWhitelisting32.dll'
The thread 0x31a8 has exited with code 0 (0x0).
The thread 0x1f70 has exited with code 0 (0x0).
The thread 0x3668 has exited with code 0 (0x0).
The thread 0x3698 has exited with code 0 (0x0).
The thread 0x3a34 has exited with code 0 (0x0).
The thread 0x374 has exited with code 0 (0x0).
The thread 0x5d8 has exited with code 0 (0x0).
The thread 0x3288 has exited with code 0 (0x0).
The thread 0x35bc has exited with code 0 (0x0).
The thread 0x348c has exited with code 0 (0x0).
The thread 0x3968 has exited with code 0 (0x0).
The thread 0x8c0 has exited with code 0 (0x0).
The thread 0x1068 has exited with code 0 (0x0).
The thread 0x1ef8 has exited with code 0 (0x0).
The thread 0x3a30 has exited with code 0 (0x0).
The thread 0x184c has exited with code 0 (0x0).
The thread 0x37f8 has exited with code 0 (0x0).
The thread 0x18b4 has exited with code 0 (0x0).
The thread 0x3380 has exited with code 0 (0x0).
The thread 0x3bf8 has exited with code 0 (0x0).
'OpenCV_Nerf.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvproc.dll'. Symbol loading disabled by Include/Exclude setting.
Exception thrown at 0x76979862 in OpenCV_Nerf.exe: Microsoft C++ exception: cv::Exception at memory location 0x00FCF7EC.
Unhandled exception at 0x76979862 in OpenCV_Nerf.exe: Microsoft C++ exception: cv::Exception at memory location 0x00FCF7EC.
I am completely stumped and would really appreciate any help or pointers I could get.
Regards

Segfault with thread_local and std::unordered_map

During a debugging session, I found a problem that I was able to reduce down to this C++11 code:
#include <thread>
#include <vector>
#include <unordered_map>
class MyClass
{
public:
MyClass(){
printf("%p\n", this);
}
~MyClass(){
printf("~%p\n", this);
}
std::unordered_map<int, int> member;
};
thread_local MyClass threadLocalObject;
int main(){
std::vector<std::thread> threads;
for (int i = 0; i < 40; ++i){
threads.emplace_back([&](){
printf("%ld\n", threadLocalObject.member.size());
});
}
for (auto &thread : threads)
thread.join();
return 0;
}
The compiler I use is g++-6 (Homebrew GCC 6.4.0) 6.4.0 on MacOS 10.12.6.
The problem is that it seems to crash inside the destructor of member.
It doesn't always crash, though, which makes me wonder if it's some kind of race condition between the threads.
Any help is appreciated, this drives me insane.
I had similar crashes with MinGW on Windows, so I really hope to gain some knowledge from someone who understands what is going on here.
This is one of the more drastic crashlogs of this code:
0x7f9b2ba00228
0x7f9b2ac02728
0x7f9b2ba004e8
0x7f9b2ae00128
0x7f9b2b800128
0x7f9b2ad00128
0x7f9b2ac025f8
0x7f9b2ad00178
0x7f9b2b900138
0x7f9b2b800288
0x7f9b2ad002d8
0x7f9b2b900188
0x7f9b2ba00388
0x7f9b2b800548
0x7f9b2b8003e8
0x7f9b2b800cd8
0x7f9b2af00898
0x7f9b2ba00a68
0x7f9b2ae00ad8
0x7f9b2af00c08
0x7f9b2b900918
0x7f9b2ba00dd8
0x7f9b2b801048
0x7f9b2ae00e48
0x7f9b2b8013b8
0x7f9b2b801728
0x7f9b2af00f78
0x7f9b2ba00538
0x7f9b2af012e8
0x7f9b2b9002e8
0x7f9b2af00268
0x7f9b2ae004a8
0x7f9b2b900338
0x7f9b2af003c8
0x7f9b2ae00608
0x7f9b2af00528
0x7f9b2ae00768
0x7f9b2ae008c8
0x7f9b2af00688
0x7f9b2af006d8
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
~0x7f9b2ba00228
0
0
0
0
0
0
0
0
0
0
0
0
~0x7f9b2ac025f8
~0x7f9b2ad00178
~0x7f9b2ad00128
a.out(32726,0x70000e123000) malloc: *** error for object 0xa38333130303962: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
~0x7f9b2b900138
~0x7f9b2ad002d8
a.out(32726,0x70000e2ac000) malloc: *** error for object 0xa38633830306561: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
~0x7f9b2ba00388
~0x7f9b2b800288
~0x7f9b2b800548
~0x7f9b2b8003e8
~0x7f9b2b800cd8
~0x7f9b2af00898
~0x7f9b2ba00a68
~0x7f9b2ae00ad8
~0x7f9b2af00c08
~0x7f9b2b900918
~0x7f9b2ba00dd8
~0x7f9b2ae00e48
~0x7f9b2b801048
~0x7f9b2b8013b8
~0x7f9b2b801728
~0x7f9b2af00f78
~0x7f9b2ba00538
~0x7f9b2af012e8
~0x7f9b2b9002e8
~0x7f9b2ae004a8
~0x7f9b2af00268
~0x7f9b2b900338
~0x7f9b2af003c8
~0x7f9b2ae00608
~0x7f9b2af00528
~0x7f9b2ae00768
~0x7f9b2ae008c8
~0x7f9b2af00688
~0x7f9b2af006d8
~0x7f9b2ac02728
~0x7f9b2ae00128
~0x7f9b2b800128
Illegal instruction: 4

Mysterious crash in cppwinrt example

I am using Visual Studio 17 v15.0 and Win 10 Anniversary Update SDK.
I build the following code (basically sample in github repo) with cl /EHsc /O2 /DUNICODE /bigobj /await /std:c++latest, with /MT or MD. It compiles without error.
If I run when `"message.png" is not present in current directory, exception will be thrown, caught and reported with printf, then exit without crashing.
If I run when `"message.png" is present in current directory, "Hello World!" will be printed, then crash for no reason.
Weird thing is If I run it inside GDB debugger, GDB always say the program exits normally (and indeed no crash happen).
GDB output:
[New Thread 1364.0x2324]
[New Thread 1364.0x624]
[New Thread 1364.0x12cc]
[New Thread 1364.0x58c]
[New Thread 1364.0x1134]
[New Thread 1364.0x10d8]
[New Thread 1364.0x18a8]
[New Thread 1364.0x1794]
[New Thread 1364.0x20e8]
[New Thread 1364.0x2204]
[New Thread 1364.0x1030]
[New Thread 1364.0x1474]
Hello world!
[Thread 1364.0x10d8 exited with code 0]
[Thread 1364.0x624 exited with code 0]
[Thread 1364.0x20e8 exited with code 0]
[Thread 1364.0x1794 exited with code 0]
[Thread 1364.0x18a8 exited with code 0]
[Thread 1364.0x58c exited with code 0]
[Thread 1364.0x1134 exited with code 0]
[Thread 1364.0x12cc exited with code 0]
[Thread 1364.0x8d0 exited with code 0]
[Thread 1364.0x2324 exited with code 0]
[Thread 1364.0x1b38 exited with code 0]
[Thread 1364.0x2204 exited with code 0]
[Thread 1364.0x1030 exited with code 0]
[Thread 1364.0x1474 exited with code 0]
[Inferior 1 (process 1364) exited normally]
Code:
#pragma comment(lib, "windowsapp")
#pragma comment(lib, "pathcch")
#include <winrt/Windows.Storage.Streams.h>
#include <winrt/Windows.Graphics.Imaging.h>
#include <winrt/Windows.Media.Ocr.h>
#include <winrt/Windows.Networking.Sockets.h>
#include <pathcch.h>
using namespace winrt;
using namespace std::chrono;
using namespace Windows::Foundation;
using namespace Windows::Storage;
using namespace Windows::Storage::Streams;
using namespace Windows::Graphics::Imaging;
using namespace Windows::Media::Ocr;
hstring MessagePath()
{
wchar_t buffer[1024]{};
GetCurrentDirectory(_countof(buffer), buffer);
check_hresult(PathCchAppendEx(buffer, _countof(buffer), L"message.png", PATHCCH_ALLOW_LONG_PATHS));
return buffer;
}
IAsyncOperation<hstring> AsyncSample()
{
StorageFile file = co_await StorageFile::GetFileFromPathAsync(MessagePath());
IRandomAccessStream stream = co_await file.OpenAsync(FileAccessMode::Read);
BitmapDecoder decoder = co_await BitmapDecoder::CreateAsync(stream);
SoftwareBitmap bitmap = co_await decoder.GetSoftwareBitmapAsync();
OcrEngine engine = OcrEngine::TryCreateFromUserProfileLanguages();
OcrResult result = co_await engine.RecognizeAsync(bitmap);
return result.Text();
}
int main()
{
init_apartment();
try
{
printf("%ls\n", AsyncSample().get().c_str());
}
catch (hresult_error const & e)
{
printf("hresult_error: (0x%8X) %ls\n", e.code(), e.message().c_str());
}
return 0;
}
Turns out hstring returned by AsyncSample().get() is not null terminated, so printf crashes.
try
{
auto ans = AsyncSample().get();
printf("[%u]: ", ans.size());
auto s = ans.c_str();
for (uint32_t i = 0; i < ans.size(); i++) {
printf("%lc", s[i]);
}
putchar('\n');
}

Thread Scheduler Error Visual Studio 2013 vs 2015

I recently upgraded some projects from VS2013 to VS2015 and I have found some problems with my threaded code. The issue seems to be that a thread that is waiting on a mutex to get unlocked is not getting control when another thread unlocks the mutex. Since the other thread is a loop that locks the mutex, does something, then unlocks it, the result is that thread running the loop many times and the other thread waiting >10sec for control. Below is a very small sample program that reproduces the bug for me.
#include "stdafx.h"
#include <thread>
#include <mutex>
#include <iostream>
std::mutex print_guard_;
int i = 0;
void RenderLoop()
{
while (true) {
print_guard_.lock();
std::cout << "Render: " << i << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(200));
print_guard_.unlock();
std::this_thread::yield();
}
}
void Print()
{
print_guard_.lock();
std::cout << "Main Thread: " << i << std::endl;
print_guard_.unlock();
}
int main()
{
std::thread* thread_ = new std::thread(RenderLoop);
while (true) {
Print();
}
return 0;
}
The error occurs both with and without the yield command in the RenderLoop function. In Visual Studio 2015 I get something like:
Render: 0
Render: 0
Render: 0
Render: 0
Render: 0
Render: 0
Render: 0
Render: 0
Render: 0
... (continues for a few seconds or longer)
Main Thread: 0
Main Thread: 0
Main Thread: 0
Main Thread: 0
Main Thread: 0
Main Thread: 0
Render: 0
Render: 0
Render: 0
...
and on VS2013 I get
Render: 0
Main Thread: 0
Render: 0
Main Thread: 0
Render: 0
Main Thread: 0
Render: 0
Main Thread: 0
Render: 0
Main Thread: 0
Render: 0
Main Thread: 0
Render: 0
Main Thread: 0
Render: 0
Main Thread: 0
Render: 0
Main Thread: 0
Render: 0
Main Thread: 0
Render: 0
Main Thread: 0
Anyone have any ideas on how to force the thread to give up control?

How does gdb retrieve the exit code of target program?

Under command line, I know that using echo $? gets me the exit code. In gdb, I use "r" to run through the program and the program terminates, so how does gdb gets this exit code? Any commands inside gdb?
Thanks!
When a program exits, gdb sets the convenience variable $_exitcode to the exit code.
So given:
int main() {
return 23;
}
Running it in gdb, I get:
(gdb) run
Starting program: /tmp/q
[Inferior 1 (process 3677) exited with code 027]
(gdb) print $_exitcode
$1 = 23
It just prints exit code at the end of debug session when the program terminates. Or prints exited normally for 0 exit code. See test debug session for this test program:
#include <stdlib.h>
int main(int argc, char *argv[]) {
return atoi(argv[1]);
}
Debug session:
[ksemenov#NB824RIH ~]$ gdb -q ./a.out
Reading symbols from ./a.out...(no debugging symbols found)...done.
(gdb) r 0
Starting program: /home/ksemenov/a.out 0
Missing separate debuginfos, use: dnf debuginfo-install glibc-2.23.1-10.fc24.x86_64
[Inferior 1 (process 19162) exited normally]
(gdb) r 1
Starting program: /home/ksemenov/a.out 1
[Inferior 1 (process 19166) exited with code 01]
(gdb) r 6
Starting program: /home/ksemenov/a.out 6
[Inferior 1 (process 19167) exited with code 06]
(gdb)