My application has an IRC module that essentially is a normal client. Since this is heavily threaded, I stand the risk of a plug-in retrieving, for example, a users nickname - it is valid at the time, but the parser triggers an update, changing said nickname. Once the other thread has execution again, it's dealing with a pointer to now-invalid memory, since it'll be impossible to have the return + copy as an atomic operation.
Is my assumption about this correct, based on the code below? As such, I guess I'd have to use the usual mutex lock/unlock method, unless someone can confirm or suggest otherwise (I'd rather not have to convert and return a shared_ptr, but I guess it is a valid option, it's just I intend on SWIG'ing this and don't know if it wouldn't like them).
IrcUser.h
class IrcUser : public IrcSubject
{
private:
...
std::shared_ptr<std::string> _nickname;
std::shared_ptr<std::string> _ident;
std::shared_ptr<std::string> _hostmask;
public:
...
const c8*
Ident() const
{ return _ident.get()->c_str(); }
const c8*
Hostmask() const
{ return _hostmask.get()->c_str(); }
const u16
Modes() const
{ return _modes; }
const c8*
Nickname() const
{ return _nickname.get()->c_str(); }
bool
Update(
const c8 *new_nickname,
const c8 *new_ident,
const c8 *new_hostmask,
const mode_update *new_modes
);
};
IrcUser.cc
bool
IrcUser::Update(
const c8 *new_nickname,
const c8 *new_ident,
const c8 *new_hostmask,
const mode_update *new_modes
)
{
if ( new_nickname != nullptr )
{
if ( _nickname == nullptr )
{
*_nickname = std::string(new_nickname);
}
else
{
_nickname.reset();
*_nickname = std::string(new_nickname);
}
Notify(SN_NicknameChange, new_nickname);
}
...
}