Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   atexit problems and questions.... (http://www.velocityreviews.com/forums/t525277-atexit-problems-and-questions.html)

MathWizard 07-27-2007 07:36 PM

atexit problems and questions....
 
Hi all,

This question relates to an earlier thread I started (static object as
member of a class).

I have the following (illustrative) code. Again, I hope to make no typos:

class A {
... //whatever here
};
class B {
... // omitted ctors, dtor, ....
A& get_static_A();
void cleanup();
};

A& B::get_static_A()
{
static A* A_ptr;

if (A_ptr == NULL) {
A_ptr = new A;
atexit(cleanup);
}
return *A_ptr;
}

This code is a result of the advice I got in my revious thread. Class B
needs to share one single copy of class A, and this is the way it should
all work correctly.

Problem: my compiler does not like the atexit call, it says: "cannot
find a match for atexit(void)" I also tried things like
"atexit(B::cleanup);", but that doesn't help. Any ideas?

Furthermore, I stumbled on an article at
http://www.petebecker.com/js/js199904.html

I cite: "Be very, very careful if you mix |atexit| functions and static
objects with destructors. The rules are pretty clear: functions
registered with |atexit| are interleaved with destructors for static
objects in the reverse order of their registration. If you create a
static object, then register an |atexit| function, then create another
static object, the compiler should generate code that invokes the
destructor for the second object, calls the |atexit| function, then
invokes the destructor for the first object. If your code relies on this
sequence being handled correctly it is very likely to crash. If you’re
concerned about writing portable code (as we are in The Journeyman’s
Shop) don’t rely on intermixing |atexit| functions with static destructors."

If I look at the code I wrote above, then I also rely on a strict
sequence: the cleanup method must be called before the static A is
destroyed. Comments on this?

Thanks for any pointers,

Jeroen

Pete Becker 07-27-2007 07:53 PM

Re: atexit problems and questions....
 
On 2007-07-27 15:36:34 -0400, MathWizard <no_mail@please.com> said:

>
> class A {
> .. //whatever here
> };
> class B {
> .. // omitted ctors, dtor, ....
> A& get_static_A();
> void cleanup();
> };
>
> atexit(cleanup);
>
> Problem: my compiler does not like the atexit call, it says: "cannot
> find a match for atexit(void)" I also tried things like
> "atexit(B::cleanup);", but that doesn't help. Any ideas?


atexit takes a pointer to an ordinary function, not a pointer to a
member function. There's no way to pass &B::cleanup to it. After all,
where is the B object that cleanup should be applied to?

More generally, though, I'd expect both get_static_A() and cleanup() to
be static member functions. With that change, the code as written ought
to work.

>
> Furthermore, I stumbled on an article at
> http://www.petebecker.com/js/js199904.html
>
> I cite: "Be very, very careful if you mix |atexit| functions and static
> objects with destructors. The rules are pretty clear: functions
> registered with |atexit| are interleaved with destructors for static
> objects in the reverse order of their registration. If you create a
> static object, then register an |atexit| function, then create another
> static object, the compiler should generate code that invokes the
> destructor for the second object, calls the |atexit| function, then
> invokes the destructor for the first object. If your code relies on
> this sequence being handled correctly it is very likely to crash. If
> you’re concerned about writing portable code (as we are in The
> Journeyman’s Shop) don’t rely on intermixing |atexit| functions with
> static destructors."
>
> If I look at the code I wrote above, then I also rely on a strict
> sequence: the cleanup method must be called before the static A is
> destroyed. Comments on this?
>
>


Well, the text you quote was written in 1999, and back then, there were
quirky C++ implementations that didn't sequence atexit functions and
destructors correctly. It's probably better these days, but I haven't
looked recently. I wouldn't trust it. Especially if there's a chance
that your code will be used with an older compiler.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)


Victor Bazarov 07-27-2007 07:56 PM

Re: atexit problems and questions....
 
MathWizard wrote:
> I have the following (illustrative) code. Again, I hope to make no
> typos:


Don't type the code into the message. Type it into your code editor,
compile it, make sure there are no typos to speak of, then copy-and-
paste it into the message.

>
> class A {
> .. //whatever here
> };
> class B {
> .. // omitted ctors, dtor, ....
> A& get_static_A();


Why isn't this 'static'?

> void cleanup();


What does it do? Maybe it should be 'static'?

> };
>
> A& B::get_static_A()
> {
> static A* A_ptr;
>
> if (A_ptr == NULL) {
> A_ptr = new A;
> atexit(cleanup);
> }
> return *A_ptr;
> }
>
> This code is a result of the advice I got in my revious thread. Class
> B needs to share one single copy of class A, and this is the way it
> should all work correctly.
>
> Problem: my compiler does not like the atexit call, it says: "cannot
> find a match for atexit(void)" I also tried things like
> "atexit(B::cleanup);", but that doesn't help. Any ideas?


Make 'cleanup' static. Read the FAQ about callbacks and member
functions.

> Furthermore, I stumbled on an article at
> http://www.petebecker.com/js/js199904.html
>
> I cite: "[snip - VB] If
> you’re concerned about writing portable code (as we are in The
> Journeyman’s Shop) don’t rely on intermixing |atexit| functions with
> static
> destructors."
> If I look at the code I wrote above, then I also rely on a strict
> sequence: the cleanup method must be called before the static A is
> destroyed.


There is no static A in your code. There is a static A*, and there
is a dynamic A.

> Comments on this?


None.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask



terminator 07-28-2007 11:17 AM

Re: atexit problems and questions....
 
On Jul 27, 10:36 pm, MathWizard <no_m...@please.com> wrote:

> class B {
> .. // omitted ctors, dtor, ....
> A& get_static_A();
> void cleanup();
>
> };
>
> A& B::get_static_A()
> {
> static A* A_ptr;
>

...
> atexit(cleanup);



your functions must be declared as to be 'static'(for the same purpose
that you declare some objects as 'static'):

class B {
.. // omitted ctors, dtor, ....
static A& get_static_A();
static void cleanup();

};

and if the compiler compligns on 'atexit' again, the try declaring
'cleanup' with 'cdecl' or '_cdecl' specifiers or some resembling
stuff.

regards,
FM.


MathWizard 07-29-2007 11:51 AM

Re: atexit problems and questions....
 
Everybody thanks for all the answers. I got everything to work.

Jeroen

Victor Bazarov 07-29-2007 01:49 PM

Re: atexit problems and questions....
 
MathWizard wrote:
> Everybody thanks for all the answers. I got everything to work.


Right, everybody thanks, but what about you?



MathWizard 07-29-2007 05:03 PM

Re: atexit problems and questions....
 
Victor Bazarov wrote:

>MathWizard wrote:
>
>
>>Everybody thanks for all the answers. I got everything to work.
>>
>>

>
>Right, everybody thanks, but what about you?
>
>

I'm afraid I don't understand the question entirely... About me:

* I got my piece of code to work.
* These threads point me to aspects of C++ I have to look into further.


=?ISO-8859-1?Q?Erik_Wikstr=F6m?= 07-29-2007 06:01 PM

Re: atexit problems and questions....
 
On 2007-07-29 19:03, MathWizard wrote:
> Victor Bazarov wrote:
>
>>MathWizard wrote:
>>
>>
>>>Everybody thanks for all the answers. I got everything to work.
>>>
>>>

>>
>>Right, everybody thanks, but what about you?
>>
>>

> I'm afraid I don't understand the question entirely... About me:
>
> * I got my piece of code to work.
> * These threads point me to aspects of C++ I have to look into further.


English grammar is a bit off-topic here but anyway:
What he meant was that the sentence "Everybody thanks for all the
answers." means that everybody says thanks for the answers. To give it
the meaning you wanted you need to insert a comma after the word
'Everyone' so that the sentence reads "Everybody, thanks for all the
answers."

--
Erik Wikström

BobR 07-29-2007 06:19 PM

Re: atexit problems and questions....
 

Victor Bazarov <v.Abazarov@comAcast.net> wrote in message...
> MathWizard wrote:
> > Everybody thanks for all the answers. I got everything to work.

>
> Right, everybody thanks, but what about you?


TV newspeople influence.

news commercial: "A horrific car crash tonight at eleven!"

I think, "Well, if they know it's going to happen at eleven, why don't they
prevent it?".

> MathWizard *should have* wrote:

Everybody, thanks for all the answers.

But then, english/grammer *ain't* my strong point! <G>
--
Bob <G> R
POVrookie



MathWizard 07-29-2007 07:50 PM

Re: atexit problems and questions....
 
Erik Wikström wrote:

> On 2007-07-29 19:03, MathWizard wrote:
>
>> Victor Bazarov wrote:
>>
>>> MathWizard wrote:
>>>
>>>
>>>> Everybody thanks for all the answers. I got everything to work.
>>>>
>>>
>>>
>>> Right, everybody thanks, but what about you?
>>>
>>>

>> I'm afraid I don't understand the question entirely... About me:
>>
>> * I got my piece of code to work.
>> * These threads point me to aspects of C++ I have to look into
>> further.

>
>
> English grammar is a bit off-topic here but anyway:
> What he meant was that the sentence "Everybody thanks for all the
> answers." means that everybody says thanks for the answers. To give it
> the meaning you wanted you need to insert a comma after the word
> 'Everyone' so that the sentence reads "Everybody, thanks for all the
> answers."
>

OK, I just thought there was more behind Victor's question. Not being a
native speaker sometimes results in sloppy sentences with respect to
English grammar :-) But I meant what you wrote...


All times are GMT. The time now is 07:13 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


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 51 52 53 54 55 56 57