-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,6 +48,9 @@ const string MQMessage::PROPERTY_TRANSACTION_CHECK_TIMES = "TRANSACTION_CHECK_TI | |
| const string MQMessage::PROPERTY_CHECK_IMMUNITY_TIME_IN_SECONDS = "CHECK_IMMUNITY_TIME_IN_SECONDS"; | ||
|
|
||
| const string MQMessage::KEY_SEPARATOR = " "; | ||
|
|
||
| static const string EMPTY_STRING = ""; | ||
|
|
||
| //<!************************************************************************ | ||
| MQMessage::MQMessage() { Init("", "", "", 0, "", true); } | ||
|
|
||
|
|
@@ -109,9 +112,9 @@ void MQMessage::setPropertyInternal(const string& name, const string& value) { | |
|
|
||
| const string & MQMessage::getProperty(const string& name) const { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think both of them are fine. |
||
| map<string, string>::const_iterator it = m_properties.find(name); | ||
| if(it == m_properties.end()){ | ||
| return ""; | ||
| }else{ | ||
| if (it == m_properties.end()) { | ||
| return EMPTY_STRING; | ||
| } else { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice catch for the code style. |
||
| return it->second; | ||
| } | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.hlike this: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 usereturn "";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.
@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.