OpenCV unhandled exception - c++

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

Related

Load data from .ini crash my game! What can I do? 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).

Why does my MPI application trigger a breakpoint?

I have a simple MPI application that is used to pass values from process 0 to other processes upon entering the desired value but it is triggering a breakpoint on "else".
What am I missing?
#include "stdafx.h"
#include "mpi.h"
#include "stdio.h"
#include "stdlib.h"
int main(int argc, char* argv[])
{
int rank;
int value;
int size;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
do
{
if (rank == 0)
{
printf("Enter the value: ");
scanf_s("%d", &value);
MPI_Send(&value, 1, MPI_INT, rank + 1, 0, MPI_COMM_WORLD);
}
else
{
MPI_Recv(&value, 1, MPI_INT, rank - 1, 0, MPI_COMM_WORLD,
&status);
if (rank < size - 1)
MPI_Send(&value, 1, MPI_INT, rank + 1, 0, MPI_COMM_WORLD);
}
printf("Process %d got %d ", rank, value);
} while (value >= 0);
MPI_Finalize();
return 0;
}
This is output from debug:
'MPIHelloWorld.exe' (Win32): Loaded 'C:\WINDOWS\SysWOW64\KernelBase.dll'. Cannot find or open the PDB file.
'MPIHelloWorld.exe' (Win32): Loaded 'C:\WINDOWS\SysWOW64\gdi32.dll'. Cannot find or open the PDB file.
'MPIHelloWorld.exe' (Win32): Loaded 'C:\Program Files (x86)\Bonjour\mdnsNSP.dll'. Cannot find or open the PDB file.
MPIHelloWorld.exe has triggered a breakpoint.
MPIHelloWorld.exe has triggered a breakpoint.
The thread 0x3308 has exited with code 0 (0x0).
The thread 0x1dfc has exited with code 0 (0x0).
The thread 0x2e5c has exited with code 0 (0x0).
The program '[4400] MPIHelloWorld.exe' has exited with code 0 (0x0).
UPDATE:
When I press CTRL + 5 I get this error written in my console:
job aborted:
[ ranks ] message
[0] fatal error
Fatal error in MPI_Send:
Invalid rank has value 1 but must be nonnegative and less than 1
Ok, I have finally found an answer!
I couldn't use Cluster Debugger for MPI since I am running Visual Studio 2015
and the problem was that only 1 process was running.
I opened CMD and ran my application using "mpiexec.exe -n 4 myMpiApp.exe" and it worked.

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');
}

decreasing the size of executable which is statically linked with opencv

I have written this code to capture the photo from webcam using opencv library.
#include <opencv2/highgui/highgui.hpp>
#include<windows.h>
#include<iostream>
using namespace std;
using namespace cv;
void camcapture()
{
VideoCapture cap(0);
char name[100];
int i=0;
while(1){
Sleep(1000);
sprintf(name,"%s%d.jpg","test",i++);
Mat save_img;
cap >> save_img;
if(save_img.empty())
{
std::cerr << "Something is wrong with the webcam, could not get frame." << std::endl;
}
// Save the frame into a file
imwrite(name, save_img); // A JPG FILE IS BEING SAVED
printf("image %s saved\n",name);
}
}
int main()
{
camcapture();
}
then i statically linked with some of the required opencv library to make it portable. when i compile the code the size of the executable is 4.73,
Is there any way to reduce the size of executable without affecting the portability ?
i don't want to link the libraries dynamically.
here is the compilation log.
Build started on: 25-10-2016 at 22:52.58
Build ended on: 25-10-2016 at 22:53.00
-------------- Build: Release in cvtest (compiler: GNU GCC Compiler)---------------
mingw32-g++.exe -Wall -fexceptions -O2 -ID:\open\opencv\mybuild4\install\include -ID:\open\opencv\mybuild4\install\include\opencv -ID:\open\opencv\mybuild4\install\include\opencv2 -c D:\c_cpp_codes\cvtest\main.cpp -o obj\Release\main.o
mingw32-g++.exe -LD:\open\opencv\mybuild4\install\x86\mingw\staticlib -o bin\Release\cvtest.exe obj\Release\main.o -s D:\open\opencv\mybuild4\lib\libopencv_world310.a D:\open\opencv\mybuild4\3rdparty\lib\liblibjpeg.a D:\open\opencv\mybuild4\3rdparty\lib\liblibwebp.a D:\open\opencv\mybuild4\3rdparty\lib\liblibtiff.a D:\open\opencv\mybuild4\3rdparty\lib\liblibpng.a D:\open\opencv\mybuild4\3rdparty\lib\liblibjasper.a D:\open\opencv\mybuild4\3rdparty\lib\libIlmImf.a D:\open\opencv\mybuild4\3rdparty\lib\libzlib.a C:\MinGW\lib\Vfw32.Lib C:\MinGW\lib\Uuid.Lib C:\MinGW\lib\Ole32.Lib C:\MinGW\lib\OleAut32.Lib
Output file is bin\Release\cvtest.exe with size 4.73 MB
Process terminated with status 0 (0 minute(s), 1 second(s))
0 error(s), 0 warning(s) (0 minute(s), 1 second(s))
It depends how you want to use it. In addition to responses in comments, you can use executable packer, like UPX.

Close all processes

I'm trying to make a command which will close all processes, but it will not work for me.
#include "StdAfx.h"
int _tmain(int argc, _TCHAR* argv[])
{
// Get the list of process identifiers.
DWORD ExitCode;
DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i;
if (!EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
{
return 1;
}
// Calculate how many process identifiers were returned.
cProcesses = cbNeeded / sizeof(DWORD);
// exit each process.
for ( i = 0; i < cProcesses; i++ )
{
std::cout<<"end";
if( aProcesses[i] != 0 )
{
GetExitCodeProcess(OpenProcess(PROCESS_ALL_ACCESS,false,aProcesses[i]),&ExitCode);
ExitProcess(ExitCode);
}
}
}
In addition, I get those errors:
> 'check2.exe': Loaded 'C:\Users\Barak Shriky\Documents\Visual Studio 2010\Projects\check2\Debug\check2.exe', Symbols loaded.
'check2.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Symbols loaded (source information stripped).
'check2.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Symbols loaded (source information stripped).
'check2.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Symbols loaded (source information stripped).
'check2.exe': Loaded 'C:\Windows\SysWOW64\msvcp100d.dll', Symbols loaded (source information stripped).
'check2.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded (source information stripped).
'check2.exe': Loaded 'C:\Windows\SysWOW64\psapi.dll', Symbols loaded (source information stripped).
The program '[3292] check2.exe: Native' has exited with code -858993460 (0xcccccccc).
Can someone please help me with this issue?
1) You are not getting any errors
2) ExitProcess is ending your process. Please read the documentation.
Looks to me like it's working just fine - you just haven't got symbols installed for some system DLL's, which is normal.
Of course, you would get a more meaningful message of why the process exited if you actually set ExitCode to something - say ExitCode = 0xDeadBeef; - and then you would see that it was YOUR process that killed itself.
Doing this seems like a very bad thing to do (assuming it is "successful" in closing the process in the first place), since there are certainly plenty of processes in Windows that when stopped causes the rest of the system to not work very well. Such as the page-in/out process, for example, which is also used to load/unload executables. Being SLIGHTLY more selective in which processes you kill will probably be useful.
see the code below where I used terminate process...
// exit each process.
for ( i = 0; i < cProcesses; i++ )
{
std::cout<<"end";
if( aProcesses[i] != 0)
{
GetExitCodeProcess(OpenProcess(PROCESS_ALL_ACCESS,false,aProcesses[i]),&ExitCode);
TerminateProcess(aProcesses[i], ExitCode);
}
}
}