dlib::timer crashes

I am playing around with the dlib library.
The following code crashes in DEBUG build, but works fine in RELEASE build.
Using VS 2017 CE 15.9.23
Anything I could do about it?
http://dlib.net/timer_ex.cpp.html
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
/*
    This is an example illustrating the use of the timer object from the dlib C++ Library.

    The timer is an object that calls some user specified member function at regular
    intervals from another thread.
*/

#include <dlib/timer.h>
#include <dlib/misc_api.h> // for dlib::sleep
#include <iostream>

using namespace dlib;
using namespace std;

// ----------------------------------------------------------------------------------------

class timer_example
{
public:
  void action_function()
  {
    // print out a message so we can see that this function is being triggered
    cout << "action_function() called" << endl;
  }
};

// ----------------------------------------------------------------------------------------

int main()
{
  timer_example e;

  // Here we construct our timer object.  It needs two things.  The second argument is
  // the member function it is going to call at regular intervals and the first argument
  // is the object instance it will call that member function on.
  timer<timer_example> t(e, &timer_example::action_function);

  // Set the timer object to trigger every second
  t.set_delay_time(1000);

  // Start the timer.  It will start calling the action function 1 second from this call
  // to start.
  t.start();

  // Sleep for 10 seconds before letting the program end.  
  dlib::sleep(10000);

  // The timer will destruct itself properly and stop calling the action_function.
}

Output:
action_function() called
action_function() called
action_function() called
action_function() called
action_function() called
action_function() called
action_function() called
action_function() called
action_function() called
action_function() called


Error detected at line 38.
Error detected in file d:\code\c++\dlib\threads\threaded_object_extension.cpp.
Error detected in function __thiscall dlib::threaded_object::~threaded_object(void).

Failing expression was is_alive() == false.
        threaded_object::~threaded_object()
        You have let a threaded object destruct itself before terminating its thread
        this: 01898150

Assertion failed: false, file d:\code\c++\dlib\threads\threaded_object_extension.cpp, line 43
Last edited on
There's this juicy comment in timer.cpp

// The only time this destructor is called is when
//
// a) the process terminates
// b) the dynamic library(.so/.dll) is unloaded (could be a part of a))
//
// in case of a)
// windows: the process termination is especially painful, since threads are killed
// before destructors of the process image .dll's are called.
// Thus, for the windows platform, there is no threads running, so the only thing
// to do here is just let the standard memberwise destructors run
// linux: it's ok to just signal shutdown and wait for the running thread, to exit
//
// in case of b)
// windows:
// if it's part of the termination process, a) applies
// if its part of user doing manual load_library/unload_library
// there is no (safe/robust)solution, but best practices are described here
// https://msdn.microsoft.com/en-us/library/windows/desktop/dn633971.aspx
// to support such a clean shutdown, you are required to make a call prior to
// unload dll, that shutdown all the threads in the contained dll.
// This could be done in this module by providing a global_delete_clock()
//
// linux: the destructor for linux will do it's usual job regardless.
//

If you're falling foul of windows messy threads, then perhaps you need an explicit
t.stop();
before letting everything fall off the edge of the world.

I added t.stop() at the end of main, but still the same behavior.
Topic archived. No new replies allowed.