Mysterious crash in cppwinrt example - c++

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

Related

Preventing threads from unnecessary exit and keep the pool alive

Suppose I call a program with OMP_NUM_THREADS=16.
The first function calls #pragma omp parallel for num_threads(16).
The second function calls #pragma omp parallel for num_threads(2).
The third function calls #pragma omp parallel for num_threads(16).
Debugging with gdb shows me that on the second call 14 threads exit. And on the third call, 14 new threads are spawned.
Is it possible to prevent 14 threads from exiting on the second call? Thank you.
The proof listings are below.
$ cat a.cpp
#include <omp.h>
void func(int thr) {
int count = 0;
#pragma omp parallel for num_threads(thr)
for(int i = 0; i < 10000000; ++i) {
count += i;
}
}
int main() {
func(16);
func(2);
func(16);
return 0;
}
$ g++ -o a a.cpp -fopenmp -g
$ ldd a
...
libgomp.so.1 => ... gcc-9.3.0/lib64/libgomp.so.1
...
$ OMP_NUM_THREADS=16 gdb a
...
Breakpoint 1, main () at a.cpp:13
13 func(16);
(gdb) n
[New Thread 0xffffbe24f160 (LWP 27216)]
[New Thread 0xffffbda3f160 (LWP 27217)]
[New Thread 0xffffbd22f160 (LWP 27218)]
[New Thread 0xffffbca1f160 (LWP 27219)]
[New Thread 0xffffbc20f160 (LWP 27220)]
[New Thread 0xffffbb9ff160 (LWP 27221)]
[New Thread 0xffffbb1ef160 (LWP 27222)]
[New Thread 0xffffba9df160 (LWP 27223)]
[New Thread 0xffffba1cf160 (LWP 27224)]
[New Thread 0xffffb99bf160 (LWP 27225)]
[New Thread 0xffffb91af160 (LWP 27226)]
[New Thread 0xffffb899f160 (LWP 27227)]
[New Thread 0xffffb818f160 (LWP 27228)]
[New Thread 0xffffb797f160 (LWP 27229)]
[New Thread 0xffffb716f160 (LWP 27230)]
15 func(2);
(gdb)
[Thread 0xffffba9df160 (LWP 27223) exited]
[Thread 0xffffb716f160 (LWP 27230) exited]
[Thread 0xffffbca1f160 (LWP 27219) exited]
[Thread 0xffffb797f160 (LWP 27229) exited]
[Thread 0xffffb818f160 (LWP 27228) exited]
[Thread 0xffffbd22f160 (LWP 27218) exited]
[Thread 0xffffb899f160 (LWP 27227) exited]
[Thread 0xffffbda3f160 (LWP 27217) exited]
[Thread 0xffffbb1ef160 (LWP 27222) exited]
[Thread 0xffffb91af160 (LWP 27226) exited]
[Thread 0xffffba1cf160 (LWP 27224) exited]
[Thread 0xffffb99bf160 (LWP 27225) exited]
[Thread 0xffffbb9ff160 (LWP 27221) exited]
[Thread 0xffffbc20f160 (LWP 27220) exited]
17 func(16);
(gdb)
[New Thread 0xffffbb9ff160 (LWP 27231)]
[New Thread 0xffffbc20f160 (LWP 27232)]
[New Thread 0xffffb99bf160 (LWP 27233)]
[New Thread 0xffffba1cf160 (LWP 27234)]
[New Thread 0xffffbda3f160 (LWP 27235)]
[New Thread 0xffffbd22f160 (LWP 27236)]
[New Thread 0xffffbca1f160 (LWP 27237)]
[New Thread 0xffffbb1ef160 (LWP 27238)]
[New Thread 0xffffba9df160 (LWP 27239)]
[New Thread 0xffffb91af160 (LWP 27240)]
[New Thread 0xffffb899f160 (LWP 27241)]
[New Thread 0xffffb818f160 (LWP 27242)]
[New Thread 0xffffb797f160 (LWP 27243)]
[New Thread 0xffffb716f160 (LWP 27244)]
19 return 0;
The simple answer is that it isn't possible with GCC to force the runtime to keep the threads around. From cursory reading the source code of libgomp, there are no ICVs, portable or vendor-specific, that prevent the termination of excess idle threads in consecutive regions. (someone correct me if I'm wrong)
If you really need to rely on the unportable requirement that the OpenMP runtime uses persistent threads across regions with varying team sizes in between, then use Clang or Intel C++ instead of GCC. Clang's (actually LLVM's) OpenMP runtime is based on the open-source version of Intel's and they both behave the way you want. Again, this is not portable and the behaviour may change in future versions. It is instead advisable to not write your code in such a way that its performance depends on the particularities of the OpenMP implementation. For example, if the loop takes several orders of magnitude more time than the creation of a thread team (which is on the order of tens of microseconds on modern systems), it won't really matter whether the runtime uses persistent threads or not.
If OpenMP overhead is really a problem, e.g., if the work done in the loop is not enough to amortise the overhead, a portable solution is to lift the parallel region and then either re-implement the for worksharing construct like in #dreamcrash's answer or (ab)use OpenMP's loop scheduling by setting a chunk size that will only result in the desired number of threads working on the problem:
#include <omp.h>
void func(int thr) {
static int count;
const int N = 10000000;
int rem = N % thr;
int chunk_size = N / thr;
#pragma omp single
count = 0;
#pragma omp for schedule(static,chunk_size) reduction(+:count)
for(int i = 0; i < N-rem; ++i) {
count += i;
}
if (rem > 0) {
#pragma omp for schedule(static,1) reduction(+:count)
for(int i = N-rem; i < N; ++i) {
count += i;
}
}
#pragma omp barrier
}
int main() {
int nthreads = max of {16, 2, other values of thr};
#pragma omp parallel num_threads(nthreads)
{
func(16);
func(2);
func(16);
}
return 0;
}
You need chunks of exactly equal sizes in all threads. The second loop is there to take care of the case when thr does not divide the number of iterations. Also, one cannot simply sum across private variables, hence count has to be shared, e.g., by making it static. This is ugly and drags along a bunch of synchronisation necessities that may have overhead comparable with spawning new threads and make the entire exercise pointless.
One approach would be to create a single parallel region, filter out the threads that will be executing the for, and manually distribute the loop iterations per thread. For simplicity sake, I will assume a parallel for schedule(static, 1):
include <omp.h>
void func(int total_threads) {
int count = 0;
int thread_id = omp_get_thread_num();
if (thread_id < total_threads)
{
for(int i = thread_id; i < 10000000; i += total_threads) {
count += i;
}
#pragma omp barrier
}
int main() {
...
#pragma omp parallel num_threads(max_threads_to_be_used)
{
func(16);
func(2);
func(16);
}
return 0;
}
Bear in mind that there is a race condition count += i; that would have to be fixed. In the original code, you could easily fix it by using the reduction clause, namely #pragma omp parallel for num_threads(thr) reduction(sum:count). In the code with the manual for you could solved it as follows:
#include <omp.h>
#include<stdio.h>
#include <stdlib.h>
int func(int total_threads) {
int count = 0;
int thread_id = omp_get_thread_num();
if (thread_id < total_threads)
{
for(int i = thread_id; i < 10000000; i += total_threads)
count += i;
}
return count;
}
int main() {
int max_threads_to_be_used = // the max that you want;
int* count_array = malloc(max_threads_to_be_used * sizeof(int));
#pragma omp parallel num_threads(max_threads_to_be_used)
{
int count = func(16);
count += func(2);
count += func(16);
count_array[omp_get_thread_num()] = count;
}
int count = 0;
for(int i = 0; i < max_threads_to_be_used; i++)
count += count_array[i];
printf("Count = %d\n", count);
return 0;
}
I would say that most of the time, one will have the same number of thread used in each parallel region. So such type of pattern should not be much of an occurrent issue.

I can't use gdb to call a function

For the first time I use gdb to call the function Test(), it shows gdborig.exe has stopped working.
#include <cstdio>
int a=1;
int Test(){
return ++a;
}
int main(){
printf("%d",Test());
return 0;
}
Then I reboot the cmd, this time it just exit without any warning.
Reading symbols from Test... done.
(gdb) b 7
Breakpoint 1 at Ox401573: file Test.cpp, line 7.
(gdb) r
Starting program: C:\Users\He\Desktop\Test.exe
[New Thread 12420. Ox41ec]
[New Thread 12420. Ox2c68]
Thread 1 hit Breakpoint 1, main () at Test.cpp:7
7 printf("%d", Test()) ;
(gdb) call Test()
The gdb version is gdb-8.1

Tools for tracing a program abortion

I have a program in C++ on an Ubuntu machine, that contains several threads, every thread is responsible for big amount of functions and sub functions.
The program runs, but every ~30 minutes the code stops running, I'm trying to understand why. So far I tried to:
Put try-catch all over the code: main and every thread - the program stops running without catching:
try
{
//code
}
catch(const std::exception & e)
{
}
catch(...)
{
}
2.Using strace: When the code stops running, the last lines of the output file are:
nanosleep({0, 10000}, NULL) = 0
nanosleep({0, 10000}, NULL) = 0
nanosleep({0, 10000}, NULL) = 0
nanosleep({0, 10000}, NULL) = 0
nanosleep({0, 10000}, NULL) = 0
nanosleep({0, 10000}, NULL) = 0
nanosleep({0, 10000}, NULL) = 0
nanosleep({0, 10000}, NULL) = 0
nanosleep({0, 10000}, <ptrace(SYSCALL):No such process>
+++ killed by SIGABRT +++
I cannot understand what causes the abortion of the program by killed by SIGABRT message or <ptrace(SYSCALL):No such process>
Using gdb: I put
(gdb) catch throw
(gdb) run
the code starts to run but it seems that the gdb stops running:
Starting program: *****
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff2d6a700 (LWP 13305)]
[Thread 0x7ffff2d6a700 (LWP 13305) exited]
[Inferior 1 (process 13304) exited normally]
(gdb)
If I'm doing something wrong here, I'll be happy to know what is wrong and if not, are there some other ways\tools to trace the problem?
I'm starting to think maybe it something external to the program that causes this issue (?).
Thanks.
Put breakpoints on everything that exits
b exit
b _exit
b __exit
b exit_group
And maybe also kill variants, if you don’t use them elsewhere
b kill

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)

ThreadPool using ASIO - Threads Exit, Task not performed

I am writing a ThreadPool Class in C++ using Boost ASIO. The following is the code that I have written so far:
The ThreadPool Class
using namespace std;
using namespace boost;
class ThreadPoolClass {
private:
/* The limit to the maximum number of threads to be
* instantiated within this pool
*/
int maxThreads;
/* Group of threads in the Pool */
thread_group threadPool;
asio::io_service asyncIOService;
void _Init()
{
maxThreads = 0;
}
public:
ThreadPoolClass();
ThreadPoolClass(int maxNumThreads);
ThreadPoolClass(const ThreadPoolClass& orig);
void CreateThreadPool();
void RunTask(JobClass * aJob);
virtual ~ThreadPoolClass();
};
ThreadPoolClass::ThreadPoolClass() {
_Init();
}
ThreadPoolClass::ThreadPoolClass(int maxNumThreads) {
_Init();
maxThreads = maxNumThreads;
}
void ThreadPoolClass::CreateThreadPool() {
asio::io_service::work work(asyncIOService);
for (int i = 0; i < maxThreads; i++) {
cout<<"Pushed"<<endl;
threadPool.create_thread(bind(&asio::io_service::run, &asyncIOService));
}
}
void ThreadPoolClass::RunTask(JobClass * aJob) {
cout<<"RunTask"<<endl;
asyncIOService.post(bind(&JobClass::Run,aJob));
}
ThreadPoolClass::ThreadPoolClass(const ThreadPoolClass& orig) {
}
ThreadPoolClass::~ThreadPoolClass() {
cout<<"Kill ye all"<<endl;
asyncIOService.stop();
threadPool.join_all();
}
The Job Class
using namespace std;
class JobClass {
private:
int a;
int b;
int c;
public:
JobClass() {
//Empty Constructor
}
JobClass(int val) {
a = val;
b = val - 1;
c = val + 1;
}
void Run()
{
cout<<"a: "<<a<<endl;
cout<<"b: "<<b<<endl;
cout<<"c: "<<c<<endl;
}
};
Main
using namespace std;
int main(int argc, char** argv) {
ThreadPoolClass ccThrPool(20);
ccThrPool.CreateThreadPool();
JobClass ccJob(10);
cout << "Starting..." << endl;
while(1)
{
ccThrPool.RunTask(&ccJob);
}
return 0;
}
So, basically I am creating 20 threads, but as of now just posting only one (same) task to be run by ioservice (just to keep things simple here and get to the root cause). The following is the output when I run this program in GDB:
Pushed
[New Thread 0xb7cd2b40 (LWP 15809)]
Pushed
[New Thread 0xb74d1b40 (LWP 15810)]
Pushed
[New Thread 0xb68ffb40 (LWP 15811)]
Pushed
[New Thread 0xb60feb40 (LWP 15812)]
Pushed
[New Thread 0xb56fdb40 (LWP 15813)]
Pushed
[New Thread 0xb4efcb40 (LWP 15814)]
Pushed
[New Thread 0xb44ffb40 (LWP 15815)]
Pushed
[New Thread 0xb3affb40 (LWP 15816)]
Pushed
[New Thread 0xb30ffb40 (LWP 15817)]
Pushed
[New Thread 0xb28feb40 (LWP 15818)]
Pushed
[New Thread 0xb20fdb40 (LWP 15819)]
Pushed
[New Thread 0xb18fcb40 (LWP 15820)]
Pushed
[New Thread 0xb10fbb40 (LWP 15821)]
Pushed
[New Thread 0xb08fab40 (LWP 15822)]
Pushed
[New Thread 0xb00f9b40 (LWP 15823)]
Pushed
[New Thread 0xaf8f8b40 (LWP 15824)]
Pushed
[New Thread 0xaf0f7b40 (LWP 15825)]
Pushed
[New Thread 0xae8f6b40 (LWP 15826)]
Pushed
[New Thread 0xae0f5b40 (LWP 15827)]
Pushed
[New Thread 0xad8f4b40 (LWP 15828)]
Starting...
RunTask
Kill ye all
[Thread 0xb4efcb40 (LWP 15814) exited]
[Thread 0xb30ffb40 (LWP 15817) exited]
[Thread 0xaf8f8b40 (LWP 15824) exited]
[Thread 0xae8f6b40 (LWP 15826) exited]
[Thread 0xae0f5b40 (LWP 15827) exited]
[Thread 0xaf0f7b40 (LWP 15825) exited]
[Thread 0xb56fdb40 (LWP 15813) exited]
[Thread 0xb18fcb40 (LWP 15820) exited]
[Thread 0xb10fbb40 (LWP 15821) exited]
[Thread 0xb20fdb40 (LWP 15819) exited]
[Thread 0xad8f4b40 (LWP 15828) exited]
[Thread 0xb3affb40 (LWP 15816) exited]
[Thread 0xb7cd2b40 (LWP 15809) exited]
[Thread 0xb60feb40 (LWP 15812) exited]
[Thread 0xb08fab40 (LWP 15822) exited]
[Thread 0xb68ffb40 (LWP 15811) exited]
[Thread 0xb74d1b40 (LWP 15810) exited]
[Thread 0xb28feb40 (LWP 15818) exited]
[Thread 0xb00f9b40 (LWP 15823) exited]
[Thread 0xb44ffb40 (LWP 15815) exited]
[Inferior 1 (process 15808) exited normally]
I have two questions:
Why is it so that my threads are exiting, even when I am posting
tasks in a while loop?
Why is the output from JobClass i.e. the values of the variables a,b
and c not getting printed?
I think this happens because you create work object in the CreateThreadPool method, which is automatically destroyed when goes out of scope -> in this case io_service has no active work and does not process your tasks.
Try to make 'work' instance variable of your ThreadPool class, not local one in the method.
class ThreadPoolClass {
private:
thread_group threadPool;
asio::io_service asyncIOService;
std::auto_ptr<asio::io_service::work> work_;
public:
};
ThreadPoolClass::ThreadPoolClass(int maxNumThreads) {
_Init();
maxThreads = maxNumThreads;
}
void ThreadPoolClass::CreateThreadPool() {
work_.reset(new asio::io_service::work(asyncIOService));
for (int i = 0; i < maxThreads; i++) {
cout<<"Pushed"<<endl;
threadPool.create_thread(bind(&asio::io_service::run, &asyncIOService));
}
}
OK, i'll be the first to admit I don't know boost, and more specifically boost::asio from a hole in the ground, but I know a hella-lot about thread pools and work crews.
The threads a supposed to sleep until notified of new work, but if they are not configured to do so they will likely just finish their thread proc and exit, A tell-tale sign that this is the case is to start up a pool, sleep for a reasonable amount of time before posting any work, and if the pool threads are all terminating, they're not properly waiting. A quick perusal of boost docs yielded this and it may be related to your problem.
On that note, is it possible that the destructor of your pool from the main() entry point is, in fact, prematurely killing your work crew? I see the join_all, but that stop() gives me the willies. if it does what its name implies that would explain a lot. According to the description of that stop() call from the docs:
To effect a shutdown, the application will then need to call the
io_service object's stop() member function. This will cause the
io_service run() call to return as soon as possible, abandoning
unfinished operations and without permitting ready handlers to be
dispatched.
That immediate shutdown and abandonment mention seems suspiciously familiar to your current situation.
Again, I don't know boost:asio from Adam, but were I on this I would check the startup configuration for the boost thread objects. they likely require configuration for how to start, how to wait, etc. There must be numerous samples of using boost:asio on the web concerning configuring the very thing you're describing here, namely a work crew paradigm. I see boost::asio a TON on SO, so there is likely many related or near-related questions as well.
Please feel free to downgrade this if it isn't anything useful, and I apologize if that is the case.