# Call function before termination

**URL:** https://discourse.libcinder.org/t/call-function-before-termination/2040
**Category:** Uncategorized
**Created:** [September 1, 2022, 8:04am UTC](https://discourse.libcinder.org/t/call-function-before-termination/2040 "2022-09-01T08:04:43Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![takti](https://avatars.discourse-cdn.com/v4/letter/t/65b543/32.png) [@takti](https://discourse.libcinder.org/u/takti)
#### Post date: [September 1, 2022, 8:04am UTC](https://discourse.libcinder.org/t/call-function-before-termination/2040/1 "2022-09-01T08:04:43Z")

</div>

So, i am trying to handle my own ini files in cinder and I would like to save them right before I close the window. I was wondering if there is a built in cinder function that runs before i close the program I can use. I have tried `atexit` without success because of my file structure.

---

<div class="post-metadata">

### Author: ![lithium](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/lithium/32/38_2.png) [@lithium](https://discourse.libcinder.org/u/lithium)
#### Post date: [September 1, 2022, 12:45pm UTC](https://discourse.libcinder.org/t/call-function-before-termination/2040/2 "2022-09-01T12:45:48Z")

</div>

`ci::app::App::cleanup()` will be called if your app is exited deliberately but if you write your ini class using RAII techniques you shouldn’t have to manually write them on exit. E.g

```cpp
class IniReader
{
public:
	
	void Load ( ) { ... }
	void Save ( ) { ... }

	~IniReader ( )
	{
		Save ( );
	}
};

```

---

<div class="post-metadata">

### Author: ![dvh](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/dvh/32/296_2.png) [@dvh](https://discourse.libcinder.org/u/dvh)
#### Post date: [November 30, 2022, 4:36pm UTC](https://discourse.libcinder.org/t/call-function-before-termination/2040/3 "2022-11-30T16:36:59Z")

</div>

I wanted to implement a confirmation popup when the user tries to close the application. To achieve this I made some changes in my Cinder fork, so that if `mQuitOnLastWindowClosed == true` the last window will not be closed. Instead a signal is triggered, allowing the app to respond by showing a confirmation popup and then either quitting the app or not doing anything at all.

Here are the changes to the Window and App classes

> <https://github.com/daanvanhasselt/Cinder/commit/31d98a67bbacdc221b038cbe7d2d287f812479d9>

And then in my custom Application I can do something like this

```auto
	Application::Application() 
	{
		...

		// setup quit handler
		ci::app::App::get()->getWindow()->getSignalPreventedClose().connect([this]() {
			if(projectHasUnsavedChanges()) {
				confirmPopup("Are you sure?", "The current project has unsaved changes.", [this]() {
					quit();
				});
			} else {
				quit();
			}
		});

		...
	}

	void Application::quit()
	{
		CI_LOG_I("Closing application");
		ci::app::App::get()->quit();
	}

```

---

<div class="post-metadata">

### Author: ![rich.e](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/rich.e/32/1025_2.png) [@rich.e](https://discourse.libcinder.org/u/rich.e)
#### Post date: [February 6, 2023, 11:33pm UTC](https://discourse.libcinder.org/t/call-function-before-termination/2040/4 "2023-02-06T23:33:34Z")

</div>

Hi,

Are you aware of [this callback](https://github.com/cinder/Cinder/blob/8998b3f51ea4ef33a5b0e89567fbacd22b76a444/include/cinder/app/AppBase.h#L241) that you can register for? from cinder/AppBase.h:

```cpp
//! Signal that emits before the app quit process begins. If any slots return false then the app quitting is canceled.
EventSignalShouldQuit& getSignalShouldQuit() { return mSignalShouldQuit; }

```

If you wanted to implement your confirmPopup() using this, you could return false and then wait for youre popup’s OK button to be tapped and then issue an `App::quit()` yourself from that.

Cheers,  
Rich
