Reputation: 21801
AlertEvent::AlertEvent(const std::string& text) :
IMEvent(kIMEventAlert, alertText.c_str()),
alertText(text)
{
//inspection at time of crash shows alertText is a valid string
}
IMEvent::IMEvent(long eventID, const char* details)
{
//during construction, details==0xcccccccc
}
on a related note, the monospace font looks really terrible in chrome, whats up with that?
Upvotes: 0
Views: 555
Reputation: 7375
alertText may be shown as a string in a debugger, but it has not been constructed yet (and therefore alertText.c_str() will return an indeterminate pointer).
To avoid this, one could initialize use text.c_str() as an argument to the IMEvent ctor.
AlertEvent::AlertEvent(const std::string& text) :
IMEvent(kIMEventAlert, text.c_str()),
alertText(text)
{
//inspection at time of crash shows alertText is a valid string
}
IMEvent::IMEvent(long eventID, const char* details)
{
//during construction, details==0xcccccccc
}
Upvotes: 4
Reputation: 279315
The IMEvent constructor is called before alertText's constructor is called. In particular therefore its argument alertText.c_str()
is evaluated before alertText's constructor is called. This ain't good.
Initializer expressions are called in the order that the things being initialized are declared (not necessarily the order the initializers are listed). So parent classes first, then members. Compilers sometime helpfully warn you if you don't list the initializers in the order they will actually be executed. So provided you get that right, the rule is "don't use anything you haven't initialized". This code uses alertText before it is initialized.
Upvotes: 2
Reputation: 30235
The IMEvent constructor is called before alertText's constructor is called.
Almost. alertText.c_str()
is called before alertText is constructed, that is the real problem. The easiest solution is replacing it with text.c_str()
Upvotes: 1