[ 
https://issues.apache.org/jira/browse/GEODE-10098?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17501532#comment-17501532
 ] 

ASF GitHub Bot commented on GEODE-10098:
----------------------------------------

pdxcodemonkey commented on a change in pull request #943:
URL: https://github.com/apache/geode-native/pull/943#discussion_r819897701



##########
File path: cppcache/src/TcrConnection.cpp
##########
@@ -686,30 +682,27 @@ char* TcrConnection::readMessage(size_t* recvLen,
       reinterpret_cast<uint8_t*>(msg_header), HEADER_LENGTH);
   // ignore msgType
   input.readInt32();
-  msgLen = input.readInt32();
+  auto msgLen = input.readInt32();
   //  check that message length is valid.
-  if (!(msgLen > 0) && request == TcrMessage::GET_CLIENT_PR_METADATA) {
-    char* fullMessage;
-    *recvLen = HEADER_LENGTH + msgLen;
-    _GEODE_NEW(fullMessage, char[HEADER_LENGTH + msgLen]);
-    std::memcpy(fullMessage, msg_header, HEADER_LENGTH);
-    return fullMessage;
-    // exit(0);
+  if (msgLen <= 0 && request == TcrMessage::GET_CLIENT_PR_METADATA) {
+    auto fullMessage = make_unique<char[]>(sizeof(msg_header));
+    std::memcpy(fullMessage.get(), msg_header, sizeof(msg_header));
+    return {std::move(fullMessage), sizeof(msg_header), CONN_NOERR};
   }
 
-  // user has to delete this pointer
-  char* fullMessage;
-  *recvLen = HEADER_LENGTH + msgLen;
-  _GEODE_NEW(fullMessage, char[HEADER_LENGTH + msgLen]);
-  std::memcpy(fullMessage, msg_header, HEADER_LENGTH);
+  auto recvLen = HEADER_LENGTH + msgLen;
+  auto fullMessage = make_unique<char[]>(recvLen);
+  std::memcpy(fullMessage.get(), msg_header, HEADER_LENGTH);
 
   std::chrono::microseconds mesgBodyTimeout = receiveTimeoutSec;
   if (isNotificationMessage) {
     mesgBodyTimeout = receiveTimeoutSec * DEFAULT_TIMEOUT_RETRIES;
   }
-  error = receiveData(fullMessage + HEADER_LENGTH, msgLen, mesgBodyTimeout);
+  error =
+      receiveData(fullMessage.get() + HEADER_LENGTH, msgLen, mesgBodyTimeout);
   if (error != CONN_NOERR) {
-    delete[] fullMessage;
+    fullMessage.reset();

Review comment:
       Technically not necessary - dunno how attached you are to this line, I 
suppose it goes some small way towards documenting what we're doing here...

##########
File path: cppcache/src/CacheXmlParser.cpp
##########
@@ -494,8 +491,7 @@ void CacheXmlParser::setAttributes(Cache *) {}
  *
  */
 void CacheXmlParser::create(Cache *cache) {
-  // use DeleteObject class to delete cacheCreation_ in case of exceptions
-  DeleteObject<CacheXmlCreation> delCacheCreation(cacheCreation_);
+  std::unique_ptr<CacheXmlCreation> delCacheCreation(cacheCreation_);

Review comment:
       Seems like we're doing a lot of extra work here.  Shouldn't we just make 
cacheCreation_ a unique_ptr, and do away with this local variable altogether?

##########
File path: cppcache/src/TcrConnection.cpp
##########
@@ -531,11 +532,9 @@ ConnErrType TcrConnection::sendData(const char* buffer, 
size_t length,
   return CONN_NOERR;
 }
 
-char* TcrConnection::sendRequest(const char* buffer, size_t len,
-                                 size_t* recvLen,
-                                 std::chrono::microseconds sendTimeoutSec,
-                                 std::chrono::microseconds receiveTimeoutSec,
-                                 int32_t request) {
+std::tuple<std::unique_ptr<char[]>, std::size_t> TcrConnection::sendRequest(

Review comment:
       I find C++ tuple syntax to be incredibly clumsy, but surprisingly I 
don't hate this change :).  Nice work!




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@geode.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> TcrConnection::readMessage should not be explicitly allocating memory
> ---------------------------------------------------------------------
>
>                 Key: GEODE-10098
>                 URL: https://issues.apache.org/jira/browse/GEODE-10098
>             Project: Geode
>          Issue Type: Improvement
>          Components: native client
>            Reporter: Blake Bender
>            Priority: Major
>              Labels: pull-request-available
>
> This method calls new to read an array of bytes, then returns it to the 
> caller, whose responsibility is to delete it (what the heck???).  Even 
> better, the memory is deleted in a call to TcrMessage::setData, so not even 
> in the same class.  If this memory was a std::vector<int8_t>, we could 
> probably take advantage of move semantics and maybe even improve performance 
> a bit, in addition to avoiding potential leaks and weirdness...



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to