Hi,
Program works fine,but memory validator reports UNINITIALIZED READ on CProfiles profile;
Could you please help me with this?
Here is my code:

inside profiles.cpp

#include "profiles.h"

CProfiles profile;	// Here it reports the problem
KeyProfile* keyProfile=&profile;

//-------------------------------------------------------------
int CProfiles::Init()
{	
// other code

inside profiles.h

class CProfiles: public KeyProfile
{
public:	
	CProfiles();
	~CProfiles();
	int Init();
};

extern CProfiles profile;

I just found that in other .cpp is used:

if (profile.Init()==1) {some unreleated code}

with any prior use of profile. Just include profiles.h
Could that cause a troubles ? Do I have to do something with the object before it is used this way ?

You have fell pray to the "static initialization order fiasco". Read the linked article, and you will understand the problem and read about the solution.

Ok I will change the code to avoid init order fiasco:

// in profiles.cpp
CProfiles& profile()	
  {
    static CProfiles* ans = new CProfiles();
    return *ans;
  }
KeyProfile* keyProfile=profile();

How do I define it in profiles.h ?
Should I still use extern ?

In the profiles.h, you declare the class CProfiles, of course, and the function to get the global instance, as so:

CProfiles& profile();

And, you access the instance through the profile() function everywhere you need it, but always through the function.

In the profiles.h, you declare the class CProfiles, of course, and the function to get the global instance, as so:

CProfiles& profile();

And, you access the instance through the profile() function everywhere you need it, but always through the function.

Thank you for your advice. I am having trouble with an Implementation. I am getting Error: left of '.GetKeyAction()' must have class/struct/union

// This one was working fine
if(diwrap::input.KeyJustPressed(profile.GetKeyAction()))
// This new one gives me an Error
if(diwrap::input.KeyJustPressed(profile().GetKeyAction()))

Inside Profiles.h is definied
UINT GetKeyAction() { return keyAction; }


Inside input class is:

bool CInput::KeyJustPressed(unsigned char dik_key)
{
	if (pImpl)
		return pImpl->key[dik_key]==JUST_PRESSED;
	else
		return false;
}

I see that KeyJustPressed is unsigned char and GetKeyAction() returns UINT (and it has to stay UINT because other usage for that). I am not sure why it was working originally at the first place ?
And how do I get rid of that Error message?

Edited 5 Years Ago by Peter4n31: n/a

Your last post is completely incoherent. I would suggest you describe one problem, and provide all the code related to it, and do so coherently.

If profile.GetKeyPressed() works, it means that "profile" is declared incorrectly, so the declaration of it is the relevant code.

Your last post is completely incoherent. I would suggest you describe one problem, and provide all the code related to it, and do so coherently.

If profile.GetKeyPressed() works, it means that "profile" is declared incorrectly, so the declaration of it is the relevant code.

I wrote it confusing, sorry for that. This code is used:

if(diwrap::input.KeyJustPressed(profile().GetKeyAction()))

And it gives me the error message. Error: left of '.GetKeyAction()' must have class/struct/union
Also I have used a declaration which you suggested in profiles.h

Edited 5 Years Ago by Peter4n31: n/a

This article has been dead for over six months. Start a new discussion instead.