-
Notifications
You must be signed in to change notification settings - Fork 167
[ISSUE #71]Fixed the issue that dangling reference returned when getting message property . #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| m_properties[name] = value; | ||
| } | ||
|
|
||
| const string & MQMessage::getProperty(const string& name) const { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ifplusor hi, if we can change getProperty return string but not string& ? because i don't think there is a need to return reference string for getProperty
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jonnxu I think 'const string&' is ok. Here, we need return a read-only object or a new one, and return a reference don't allocate a new object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think both of them are fine.
It will not affect efficiency too much.
jovany-wang
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I left a few comments.
| }else{ | ||
| if (it == m_properties.end()) { | ||
| return EMPTY_STRING; | ||
| } else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice catch for the code style.
|
|
||
| const string MQMessage::KEY_SEPARATOR = " "; | ||
|
|
||
| static const string EMPTY_STRING = ""; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think defining an empty str as a static global var is the best approach.
Maybe we could add a new file constant.h like this:
class Constants {
public:
static const string EMPTY_STRING = "";
};Any way, we should avoid to use global variables and global static variables.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jovany-wang the visibility of global static variables is only in this file. i think depend on other file is unnecessary for only this one. if we use EMPTY_STRING in other files, it's ok.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you don't want to move it to constant.h, let's use return ""; directly?
Because I don't think we should use a global static variable in such case.:)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you don't want to move it to
constant.h, let's usereturn "";directly?
Because I don't think we should use a global static variable in such case.:)
@jovany-wang now, we return a reference, and return a temp object is a bug.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ifplusor Thanks, I see.
So if the method may be return a temp variable, it is pretty make sense to declare it as string MQMessage::getProperty(const string& name);.
If the method always return his member variables, we could return a const ref.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jovany-wang i don't change method signature, we can discuss it.
|
Thanks. Let's merge this PR as a workaround approach first. |
ISSUE #71 ,MQMessage::getProperty return a dangling reference when returning an empty string.