Advertisement

Statics and globals loose their values?

Started by March 27, 2001 06:29 AM
6 comments, last by gimp 23 years, 10 months ago
I''m having a number of annoying troubles tonight sa you might have seen... This time it''s a static member vairable that seems to reset to 0 every time a new class is created. Imagine a reference counter for the class...
  

class CTrace
{
public:

	CTrace();
	CTrace(const char *a_Data);
	~CTrace();

private:

	std::string		m_FunctionName;
	std::string		m_Indent;

	static int		s_Indent;
};

inline CTrace::CTrace(const char* a_Data)
{

	for(int i=0; i<s_Indent; i++)
	{
		m_Indent += "  ";		
	}

	Log << s_Indent << m_Indent << "Entering : " << a_Data << "\n";
	m_FunctionName = a_Data;
	
	++s_Indent;
}


//in cpp


int	CTrace::s_Indent=0;

  
Every time I create a new Trace class (which is at the beginning of every function) the s_Indent seems be 0.Whay coudl cause this? Many thanks Chris
Chris Brodie
Your code seems to look okay. My guess is it could be your initialisation of the static variable. It should be done before the program starts (i.e. before the main/WinMain function).

==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
Advertisement
Try again without making your method inline.
- thomas
Make sure that in your class''s ctor you''re not resetting s_Indent to 0.. Sorry if that insults your intelligence.. .. I''ve been known to do dumb shit like that on occasion..



--------------------------
I guess this is where most people put a famous quote...
"Everything is funnier with monkey''''s" - Unknown
--------------------------I guess this is where most people put a famous quote..."Everything is funnier with monkey''s" - Unknown
Ok, I think I''ve tracked the problem down more specifically, seems as though my debugging was thourough enough...

Apparently the destructor is getting called right after the constructor, not at the end of the function call when it goes out of scope...

Example:

//The above code when tested as follows:

  #include "Trace.h"void func1(void){	TRACE(func1);}void main(void){	TRACE(main);	func1();}  


What seems to happen in main is the object gets destroyed -prior- to func1 being called. Of course the destructor decrements the static so it was appearing to be to stay at 0 all the time. Can anyone tell me what going on, or more importantly how to make this work...

Many thanks

Chris
Chris Brodie
That makes a lot more sense.

You''re not creating a permanent object; you''re creating a temporary CTrace object. I''m not sure what your TRACE macro does, so I''ll just use your class instead.

I think to get the intended behavior, you can do it this way:
void func1 (){  CTrace msg ("func1"); // calls CTrace ctor} // calls CTrace dtorint main (){  CTrace msg ("main"); // calls CTrace ctor  func1 ();  return 0;} // calls CTrace dtor 

If you use temporary objects like you have in your example, they are constructed and then immediately destroyed before the next line of code is executed. Instead, I used stack variables, which means they will exist (not be destroyed) until they leave scope.

Hope this helps.
Advertisement
My guess is that you are doing that :


#define TRACE(x) CTrace( "x" );


do this instead

#define CTrace(x) CTrace tracer( "x" );

that way you'll be creating a named object. An unamed object scope is implementation dependant. But named object ALWAYS died after their scope => { }

which is all what Stoffel said

Edited by - Gorg on March 27, 2001 7:58:30 PM
Thanks again guys, I should have realised that....
Chris Brodie

This topic is closed to new replies.

Advertisement