Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to call a function outside of main()?

I guess my question is stupid, but nevertheless:

In my C++ code I use some legacy C library(XLib). In order to use this library a connection to X server has to be opened first:

::Display* const display = ::XOpenDisplay(nullptr);

This display structure is widely used across the vast majority of the XLib functions, including the functions for allocating and freeing memory and system resources such as fonts, colormaps etc. In my code I use objects’ constructors and destructors to allocate and free resources by calling these functions. And here is the problem:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

int main()
{
    ::Display* const display = ::XOpenDisplay(nullptr);
    // ...
    Object1 object1(display, ...); // uses display inside the destructor
    Object2 object2(display, ...); // uses display inside the destructor
    Object3 object3(display, ...); // uses display inside the destructor
    // ...
    ::XCloseDisplay(display); // invalidates the display structure
    return 0;
}

This example leads to segmentation fault, because the display structure had been invalidated by XCloseDisplay() before any destructor using it was called. To avoid this issue I can embrace all the code before XCloseDisplay() in curly braces to limit the scope of objects, but it makes the code to be shifted to right which looks pretty ugly.

Is there any way to somehow call XCloseDisplay() after the main()?

>Solution :

It’s possible, but unnecessary.

Instead, wrap it in a class that closes it in the destructor, like you did with the other objects.

Destructors are called in the reverse order, which means that if you create the display first, it’ll die last.


The way you would’ve called it after main is, similarily, from a destructor of a global or function-local static object. A function-local static is better than a global variable because it avoids the static init order fiasco.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading