Unexpected results when dereferencing ScopedPointer

Howdy,

My class PCCBeta has a member ScopedPointer<PCCBeta::User> user .

According to the debugger, user looks like this:

13%20pm

As you can see, isAnonymous == true . However the following expressions all evaluate to false:

bool a = user->isAnonymous;
bool b = (*user).isAnonymous;
bool c = user->isAnonymous == true;
bool d = (*user).isAnonymous == true;

As the debugger shows:

24%20pm

All the debugger output above is from the same breakpoint.

I’ve tried to replicate this but to no avail. PCCBeta::User is a nested class and the above code is being called within a function of the class PCCBeta. Wondering if the class nesting (which I haven’t played with much before) may be an interference. Also: isAnonymous is a public member of PCCBeta::User.

Thanks in advance for any thoughts you may have!

Cheers :slight_smile:

Do you initialize it properly to a value? If you don’t initialize the value, the compiler is free to generate code that behaves nonsensically at run time.

Thanks Xenakios,

Yes, I have initialized it properly (I believe!). Here are the constructors for PCCBeta::User :

PCCBeta::User::User() {
    isAnonymous = true;
    email = "";
    name = "";
}

PCCBeta::User::User(String email, String name) {
    isAnonymous = false;
    this->email = email;
    this->name = name;
}

Here is the actual initialization:

XmlElement* userXML = pf.getXmlValue("user");
PCCBeta::User userObj = PCCBeta::User::fromXML(*userXML);
ScopedPointer<PCCBeta::User> userPtr = &userObj;
user = userPtr;

And here is PCCBeta::User::fromXML() :

PCCBeta::User PCCBeta::User::fromXML(XmlElement xml) {
    
    bool isAnonymous = xml.getBoolAttribute("isAnonymous", true);
    String email = xml.getStringAttribute("email", "");
    String name = xml.getStringAttribute("name", "");
    
    PCCBeta::User user = PCCBeta::User(email, name);
    user.isAnonymous = isAnonymous;
    return user;
    
} 

I do know that this code is messy, the constructors should really just be one constructor, etc. But can’t see what would be wrong with initialisation?

Thanks!

This looks very very unhealthy, why are you doing it like this?

ScopedPointer<PCCBeta::User> userPtr = &userObj;

Agreed it’s badly written. That was a workaround for an error which I didn’t understand. So yes lots of ugly patching. Is there reason to think that would mess things up this way?

ScopedPointer is for managing heap allocated ("new"ed) objects. If I read the code correctly you are initializing the ScopedPointer from the address of a local variable, which doesn’t make any sense.

1 Like

That’s a good lead, thank you -> didn’t realise there could be a distinction that way. I’ll try removing any code that ties a ScopedPointer to stack allocated objects. Cheers :slight_smile: