Linux sockets & threading





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I'm writing some TCP server code on linux, it's being cross-compiled for a Pi with buildroot tool chain. Basically I'm accepting a socket then creating a thread to listen for data and process it. For testing I'm using telnet as a client. Occasionally when the socket closes the program crashes during thread clean-up and I can't figure out why. My best guess is file-descriptor weirdness but that's mostly a guess at this point.



Here's the code:



void NetworkControl::startListening()
{
mListenThread = std::thread(listener);
mListenThread.detach();
}

void NetworkControl::listener()
{
int sockfd, newsockfd, clilen;
struct sockaddr_in serv_addr, cli_addr;

int portno = 612;

std::thread mWorkThread;

sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{} //print error

bzero((char *) &serv_addr, sizeof(serv_addr));

serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
{ }//print error

listen(sockfd,5);

clilen = sizeof(cli_addr);

while (1) {
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr, (socklen_t*) &clilen);
if (newsockfd < 0) {
//print error
}else{
mWorkThread = std::thread(worker,newsockfd);
mWorkThread.detach();
}
}
}

void NetworkControl::worker(int sock)
{
uint32_t n;

char *buf = new char(200);

while(1){
n = read(sock,&buf[0],199);;
if (n <= 0){
goto exit;
}
buf[n] = 'x00';
printf("%sn",buf);
}
exit:
free(buf);
close(sock);
}


Here's the backtrace from the crash:



#0  0x767c4e0c in raise () from /home/david/Desktop/BR-Pi2/output/host/arm-buildroot-linux-gnueabihf/sysroot/lib/libc.so.6
#1 0x767c60e4 in abort () from /home/david/Desktop/BR-Pi2/output/host/arm-buildroot-linux-gnueabihf/sysroot/lib/libc.so.6
#2 0x767fe2c8 in __libc_message () from /home/david/Desktop/BR-Pi2/output/host/arm-buildroot-linux-gnueabihf/sysroot/lib/libc.so.6
#3 0x76804790 in malloc_printerr () from /home/david/Desktop/BR-Pi2/output/host/arm-buildroot-linux-gnueabihf/sysroot/lib/libc.so.6
#4 0x76805850 in _int_free () from /home/david/Desktop/BR-Pi2/output/host/arm-buildroot-linux-gnueabihf/sysroot/lib/libc.so.6
#5 0x768a3c78 in tcache_thread_freeres () from /home/david/Desktop/BR-Pi2/output/host/arm-buildroot-linux-gnueabihf/sysroot/lib/libc.so.6
#6 0x768a3cdc in __libc_thread_freeres () from /home/david/Desktop/BR-Pi2/output/host/arm-buildroot-linux-gnueabihf/sysroot/lib/libc.so.6
#7 0x76f01f3c in start_thread () from /home/david/Desktop/BR-Pi2/output/host/arm-buildroot-linux-gnueabihf/sysroot/lib/libpthread.so.0
#8 0x768683b8 in ?? () from /home/david/Desktop/BR-Pi2/output/host/arm-buildroot-linux-gnueabihf/sysroot/lib/libc.so.6
Backtrace stopped: previous frame identical to this frame (corrupt stack?)


The console output:



free() invalid next size (fast)


If anyone has any ideas I'd appreciate it!










share|improve this question























  • I assume that NetworkControl::listener is a static member function? Can you please try to create an Minimal, Complete, and Verifiable example to show us?

    – Some programmer dude
    Nov 22 '18 at 7:34











  • As for the message you get, it's typical when you write out of bounds of allocated memory. Use e.g. Valgrind to help you find out more.

    – Some programmer dude
    Nov 22 '18 at 7:35






  • 3





    A hint: new char(200) allocates a single char, and initializes it to the value 200.

    – Some programmer dude
    Nov 22 '18 at 7:36






  • 2





    Furthermore: Always match new with delete, and new with delete. Perhaps you should take a few steps back, find a good book or two and read more about dynamic memory allocation (and quite a few things more IMO)? Or better yet, use std::string for all strings.

    – Some programmer dude
    Nov 22 '18 at 7:37













  • Lastly, never use goto and labels. If you need to break out of a loop use break.

    – Some programmer dude
    Nov 22 '18 at 7:37


















0















I'm writing some TCP server code on linux, it's being cross-compiled for a Pi with buildroot tool chain. Basically I'm accepting a socket then creating a thread to listen for data and process it. For testing I'm using telnet as a client. Occasionally when the socket closes the program crashes during thread clean-up and I can't figure out why. My best guess is file-descriptor weirdness but that's mostly a guess at this point.



Here's the code:



void NetworkControl::startListening()
{
mListenThread = std::thread(listener);
mListenThread.detach();
}

void NetworkControl::listener()
{
int sockfd, newsockfd, clilen;
struct sockaddr_in serv_addr, cli_addr;

int portno = 612;

std::thread mWorkThread;

sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{} //print error

bzero((char *) &serv_addr, sizeof(serv_addr));

serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
{ }//print error

listen(sockfd,5);

clilen = sizeof(cli_addr);

while (1) {
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr, (socklen_t*) &clilen);
if (newsockfd < 0) {
//print error
}else{
mWorkThread = std::thread(worker,newsockfd);
mWorkThread.detach();
}
}
}

void NetworkControl::worker(int sock)
{
uint32_t n;

char *buf = new char(200);

while(1){
n = read(sock,&buf[0],199);;
if (n <= 0){
goto exit;
}
buf[n] = 'x00';
printf("%sn",buf);
}
exit:
free(buf);
close(sock);
}


Here's the backtrace from the crash:



#0  0x767c4e0c in raise () from /home/david/Desktop/BR-Pi2/output/host/arm-buildroot-linux-gnueabihf/sysroot/lib/libc.so.6
#1 0x767c60e4 in abort () from /home/david/Desktop/BR-Pi2/output/host/arm-buildroot-linux-gnueabihf/sysroot/lib/libc.so.6
#2 0x767fe2c8 in __libc_message () from /home/david/Desktop/BR-Pi2/output/host/arm-buildroot-linux-gnueabihf/sysroot/lib/libc.so.6
#3 0x76804790 in malloc_printerr () from /home/david/Desktop/BR-Pi2/output/host/arm-buildroot-linux-gnueabihf/sysroot/lib/libc.so.6
#4 0x76805850 in _int_free () from /home/david/Desktop/BR-Pi2/output/host/arm-buildroot-linux-gnueabihf/sysroot/lib/libc.so.6
#5 0x768a3c78 in tcache_thread_freeres () from /home/david/Desktop/BR-Pi2/output/host/arm-buildroot-linux-gnueabihf/sysroot/lib/libc.so.6
#6 0x768a3cdc in __libc_thread_freeres () from /home/david/Desktop/BR-Pi2/output/host/arm-buildroot-linux-gnueabihf/sysroot/lib/libc.so.6
#7 0x76f01f3c in start_thread () from /home/david/Desktop/BR-Pi2/output/host/arm-buildroot-linux-gnueabihf/sysroot/lib/libpthread.so.0
#8 0x768683b8 in ?? () from /home/david/Desktop/BR-Pi2/output/host/arm-buildroot-linux-gnueabihf/sysroot/lib/libc.so.6
Backtrace stopped: previous frame identical to this frame (corrupt stack?)


The console output:



free() invalid next size (fast)


If anyone has any ideas I'd appreciate it!










share|improve this question























  • I assume that NetworkControl::listener is a static member function? Can you please try to create an Minimal, Complete, and Verifiable example to show us?

    – Some programmer dude
    Nov 22 '18 at 7:34











  • As for the message you get, it's typical when you write out of bounds of allocated memory. Use e.g. Valgrind to help you find out more.

    – Some programmer dude
    Nov 22 '18 at 7:35






  • 3





    A hint: new char(200) allocates a single char, and initializes it to the value 200.

    – Some programmer dude
    Nov 22 '18 at 7:36






  • 2





    Furthermore: Always match new with delete, and new with delete. Perhaps you should take a few steps back, find a good book or two and read more about dynamic memory allocation (and quite a few things more IMO)? Or better yet, use std::string for all strings.

    – Some programmer dude
    Nov 22 '18 at 7:37













  • Lastly, never use goto and labels. If you need to break out of a loop use break.

    – Some programmer dude
    Nov 22 '18 at 7:37














0












0








0








I'm writing some TCP server code on linux, it's being cross-compiled for a Pi with buildroot tool chain. Basically I'm accepting a socket then creating a thread to listen for data and process it. For testing I'm using telnet as a client. Occasionally when the socket closes the program crashes during thread clean-up and I can't figure out why. My best guess is file-descriptor weirdness but that's mostly a guess at this point.



Here's the code:



void NetworkControl::startListening()
{
mListenThread = std::thread(listener);
mListenThread.detach();
}

void NetworkControl::listener()
{
int sockfd, newsockfd, clilen;
struct sockaddr_in serv_addr, cli_addr;

int portno = 612;

std::thread mWorkThread;

sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{} //print error

bzero((char *) &serv_addr, sizeof(serv_addr));

serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
{ }//print error

listen(sockfd,5);

clilen = sizeof(cli_addr);

while (1) {
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr, (socklen_t*) &clilen);
if (newsockfd < 0) {
//print error
}else{
mWorkThread = std::thread(worker,newsockfd);
mWorkThread.detach();
}
}
}

void NetworkControl::worker(int sock)
{
uint32_t n;

char *buf = new char(200);

while(1){
n = read(sock,&buf[0],199);;
if (n <= 0){
goto exit;
}
buf[n] = 'x00';
printf("%sn",buf);
}
exit:
free(buf);
close(sock);
}


Here's the backtrace from the crash:



#0  0x767c4e0c in raise () from /home/david/Desktop/BR-Pi2/output/host/arm-buildroot-linux-gnueabihf/sysroot/lib/libc.so.6
#1 0x767c60e4 in abort () from /home/david/Desktop/BR-Pi2/output/host/arm-buildroot-linux-gnueabihf/sysroot/lib/libc.so.6
#2 0x767fe2c8 in __libc_message () from /home/david/Desktop/BR-Pi2/output/host/arm-buildroot-linux-gnueabihf/sysroot/lib/libc.so.6
#3 0x76804790 in malloc_printerr () from /home/david/Desktop/BR-Pi2/output/host/arm-buildroot-linux-gnueabihf/sysroot/lib/libc.so.6
#4 0x76805850 in _int_free () from /home/david/Desktop/BR-Pi2/output/host/arm-buildroot-linux-gnueabihf/sysroot/lib/libc.so.6
#5 0x768a3c78 in tcache_thread_freeres () from /home/david/Desktop/BR-Pi2/output/host/arm-buildroot-linux-gnueabihf/sysroot/lib/libc.so.6
#6 0x768a3cdc in __libc_thread_freeres () from /home/david/Desktop/BR-Pi2/output/host/arm-buildroot-linux-gnueabihf/sysroot/lib/libc.so.6
#7 0x76f01f3c in start_thread () from /home/david/Desktop/BR-Pi2/output/host/arm-buildroot-linux-gnueabihf/sysroot/lib/libpthread.so.0
#8 0x768683b8 in ?? () from /home/david/Desktop/BR-Pi2/output/host/arm-buildroot-linux-gnueabihf/sysroot/lib/libc.so.6
Backtrace stopped: previous frame identical to this frame (corrupt stack?)


The console output:



free() invalid next size (fast)


If anyone has any ideas I'd appreciate it!










share|improve this question














I'm writing some TCP server code on linux, it's being cross-compiled for a Pi with buildroot tool chain. Basically I'm accepting a socket then creating a thread to listen for data and process it. For testing I'm using telnet as a client. Occasionally when the socket closes the program crashes during thread clean-up and I can't figure out why. My best guess is file-descriptor weirdness but that's mostly a guess at this point.



Here's the code:



void NetworkControl::startListening()
{
mListenThread = std::thread(listener);
mListenThread.detach();
}

void NetworkControl::listener()
{
int sockfd, newsockfd, clilen;
struct sockaddr_in serv_addr, cli_addr;

int portno = 612;

std::thread mWorkThread;

sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{} //print error

bzero((char *) &serv_addr, sizeof(serv_addr));

serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
{ }//print error

listen(sockfd,5);

clilen = sizeof(cli_addr);

while (1) {
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr, (socklen_t*) &clilen);
if (newsockfd < 0) {
//print error
}else{
mWorkThread = std::thread(worker,newsockfd);
mWorkThread.detach();
}
}
}

void NetworkControl::worker(int sock)
{
uint32_t n;

char *buf = new char(200);

while(1){
n = read(sock,&buf[0],199);;
if (n <= 0){
goto exit;
}
buf[n] = 'x00';
printf("%sn",buf);
}
exit:
free(buf);
close(sock);
}


Here's the backtrace from the crash:



#0  0x767c4e0c in raise () from /home/david/Desktop/BR-Pi2/output/host/arm-buildroot-linux-gnueabihf/sysroot/lib/libc.so.6
#1 0x767c60e4 in abort () from /home/david/Desktop/BR-Pi2/output/host/arm-buildroot-linux-gnueabihf/sysroot/lib/libc.so.6
#2 0x767fe2c8 in __libc_message () from /home/david/Desktop/BR-Pi2/output/host/arm-buildroot-linux-gnueabihf/sysroot/lib/libc.so.6
#3 0x76804790 in malloc_printerr () from /home/david/Desktop/BR-Pi2/output/host/arm-buildroot-linux-gnueabihf/sysroot/lib/libc.so.6
#4 0x76805850 in _int_free () from /home/david/Desktop/BR-Pi2/output/host/arm-buildroot-linux-gnueabihf/sysroot/lib/libc.so.6
#5 0x768a3c78 in tcache_thread_freeres () from /home/david/Desktop/BR-Pi2/output/host/arm-buildroot-linux-gnueabihf/sysroot/lib/libc.so.6
#6 0x768a3cdc in __libc_thread_freeres () from /home/david/Desktop/BR-Pi2/output/host/arm-buildroot-linux-gnueabihf/sysroot/lib/libc.so.6
#7 0x76f01f3c in start_thread () from /home/david/Desktop/BR-Pi2/output/host/arm-buildroot-linux-gnueabihf/sysroot/lib/libpthread.so.0
#8 0x768683b8 in ?? () from /home/david/Desktop/BR-Pi2/output/host/arm-buildroot-linux-gnueabihf/sysroot/lib/libc.so.6
Backtrace stopped: previous frame identical to this frame (corrupt stack?)


The console output:



free() invalid next size (fast)


If anyone has any ideas I'd appreciate it!







c++ linux multithreading sockets






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 7:27









DavidDavid

132




132













  • I assume that NetworkControl::listener is a static member function? Can you please try to create an Minimal, Complete, and Verifiable example to show us?

    – Some programmer dude
    Nov 22 '18 at 7:34











  • As for the message you get, it's typical when you write out of bounds of allocated memory. Use e.g. Valgrind to help you find out more.

    – Some programmer dude
    Nov 22 '18 at 7:35






  • 3





    A hint: new char(200) allocates a single char, and initializes it to the value 200.

    – Some programmer dude
    Nov 22 '18 at 7:36






  • 2





    Furthermore: Always match new with delete, and new with delete. Perhaps you should take a few steps back, find a good book or two and read more about dynamic memory allocation (and quite a few things more IMO)? Or better yet, use std::string for all strings.

    – Some programmer dude
    Nov 22 '18 at 7:37













  • Lastly, never use goto and labels. If you need to break out of a loop use break.

    – Some programmer dude
    Nov 22 '18 at 7:37



















  • I assume that NetworkControl::listener is a static member function? Can you please try to create an Minimal, Complete, and Verifiable example to show us?

    – Some programmer dude
    Nov 22 '18 at 7:34











  • As for the message you get, it's typical when you write out of bounds of allocated memory. Use e.g. Valgrind to help you find out more.

    – Some programmer dude
    Nov 22 '18 at 7:35






  • 3





    A hint: new char(200) allocates a single char, and initializes it to the value 200.

    – Some programmer dude
    Nov 22 '18 at 7:36






  • 2





    Furthermore: Always match new with delete, and new with delete. Perhaps you should take a few steps back, find a good book or two and read more about dynamic memory allocation (and quite a few things more IMO)? Or better yet, use std::string for all strings.

    – Some programmer dude
    Nov 22 '18 at 7:37













  • Lastly, never use goto and labels. If you need to break out of a loop use break.

    – Some programmer dude
    Nov 22 '18 at 7:37

















I assume that NetworkControl::listener is a static member function? Can you please try to create an Minimal, Complete, and Verifiable example to show us?

– Some programmer dude
Nov 22 '18 at 7:34





I assume that NetworkControl::listener is a static member function? Can you please try to create an Minimal, Complete, and Verifiable example to show us?

– Some programmer dude
Nov 22 '18 at 7:34













As for the message you get, it's typical when you write out of bounds of allocated memory. Use e.g. Valgrind to help you find out more.

– Some programmer dude
Nov 22 '18 at 7:35





As for the message you get, it's typical when you write out of bounds of allocated memory. Use e.g. Valgrind to help you find out more.

– Some programmer dude
Nov 22 '18 at 7:35




3




3





A hint: new char(200) allocates a single char, and initializes it to the value 200.

– Some programmer dude
Nov 22 '18 at 7:36





A hint: new char(200) allocates a single char, and initializes it to the value 200.

– Some programmer dude
Nov 22 '18 at 7:36




2




2





Furthermore: Always match new with delete, and new with delete. Perhaps you should take a few steps back, find a good book or two and read more about dynamic memory allocation (and quite a few things more IMO)? Or better yet, use std::string for all strings.

– Some programmer dude
Nov 22 '18 at 7:37







Furthermore: Always match new with delete, and new with delete. Perhaps you should take a few steps back, find a good book or two and read more about dynamic memory allocation (and quite a few things more IMO)? Or better yet, use std::string for all strings.

– Some programmer dude
Nov 22 '18 at 7:37















Lastly, never use goto and labels. If you need to break out of a loop use break.

– Some programmer dude
Nov 22 '18 at 7:37





Lastly, never use goto and labels. If you need to break out of a loop use break.

– Some programmer dude
Nov 22 '18 at 7:37












2 Answers
2






active

oldest

votes


















1














As already told be others the crash is caused by pairing new() with free() while you actually needed new/delete.



However, using a std::vector, you don't even have to do the memory allocation yourself. (example code not compiled/tested)



void NetworkControl::worker(int sock)
{
uint32_t n;

std::vector<char> buf(200);

while(1){
n = read(sock, buf.data(), 199);
if (n <= 0){
break;
}
buf[n] = 'x00';
printf("%sn",buf.data());
}
close(sock);
}


For buf you could also use a C-style array or std::array<char>, but then you have to make sure you don't get a stackoverflow.






share|improve this answer

































    1














    The free() function frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(), calloc() or realloc(). Other‐
    wise, or if free(ptr) has already been called before, undefined behavior occurs. If ptr is NULL, no operation is performed.



    but in your code context:



    char *buf = new char(200);



    the pointer is getted by new function, it's a C++ function, That's why your program crashed.






    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425831%2flinux-sockets-threading%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      As already told be others the crash is caused by pairing new() with free() while you actually needed new/delete.



      However, using a std::vector, you don't even have to do the memory allocation yourself. (example code not compiled/tested)



      void NetworkControl::worker(int sock)
      {
      uint32_t n;

      std::vector<char> buf(200);

      while(1){
      n = read(sock, buf.data(), 199);
      if (n <= 0){
      break;
      }
      buf[n] = 'x00';
      printf("%sn",buf.data());
      }
      close(sock);
      }


      For buf you could also use a C-style array or std::array<char>, but then you have to make sure you don't get a stackoverflow.






      share|improve this answer






























        1














        As already told be others the crash is caused by pairing new() with free() while you actually needed new/delete.



        However, using a std::vector, you don't even have to do the memory allocation yourself. (example code not compiled/tested)



        void NetworkControl::worker(int sock)
        {
        uint32_t n;

        std::vector<char> buf(200);

        while(1){
        n = read(sock, buf.data(), 199);
        if (n <= 0){
        break;
        }
        buf[n] = 'x00';
        printf("%sn",buf.data());
        }
        close(sock);
        }


        For buf you could also use a C-style array or std::array<char>, but then you have to make sure you don't get a stackoverflow.






        share|improve this answer




























          1












          1








          1







          As already told be others the crash is caused by pairing new() with free() while you actually needed new/delete.



          However, using a std::vector, you don't even have to do the memory allocation yourself. (example code not compiled/tested)



          void NetworkControl::worker(int sock)
          {
          uint32_t n;

          std::vector<char> buf(200);

          while(1){
          n = read(sock, buf.data(), 199);
          if (n <= 0){
          break;
          }
          buf[n] = 'x00';
          printf("%sn",buf.data());
          }
          close(sock);
          }


          For buf you could also use a C-style array or std::array<char>, but then you have to make sure you don't get a stackoverflow.






          share|improve this answer















          As already told be others the crash is caused by pairing new() with free() while you actually needed new/delete.



          However, using a std::vector, you don't even have to do the memory allocation yourself. (example code not compiled/tested)



          void NetworkControl::worker(int sock)
          {
          uint32_t n;

          std::vector<char> buf(200);

          while(1){
          n = read(sock, buf.data(), 199);
          if (n <= 0){
          break;
          }
          buf[n] = 'x00';
          printf("%sn",buf.data());
          }
          close(sock);
          }


          For buf you could also use a C-style array or std::array<char>, but then you have to make sure you don't get a stackoverflow.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 '18 at 8:28

























          answered Nov 22 '18 at 8:23









          stefaanvstefaanv

          11k12341




          11k12341

























              1














              The free() function frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(), calloc() or realloc(). Other‐
              wise, or if free(ptr) has already been called before, undefined behavior occurs. If ptr is NULL, no operation is performed.



              but in your code context:



              char *buf = new char(200);



              the pointer is getted by new function, it's a C++ function, That's why your program crashed.






              share|improve this answer




























                1














                The free() function frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(), calloc() or realloc(). Other‐
                wise, or if free(ptr) has already been called before, undefined behavior occurs. If ptr is NULL, no operation is performed.



                but in your code context:



                char *buf = new char(200);



                the pointer is getted by new function, it's a C++ function, That's why your program crashed.






                share|improve this answer


























                  1












                  1








                  1







                  The free() function frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(), calloc() or realloc(). Other‐
                  wise, or if free(ptr) has already been called before, undefined behavior occurs. If ptr is NULL, no operation is performed.



                  but in your code context:



                  char *buf = new char(200);



                  the pointer is getted by new function, it's a C++ function, That's why your program crashed.






                  share|improve this answer













                  The free() function frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(), calloc() or realloc(). Other‐
                  wise, or if free(ptr) has already been called before, undefined behavior occurs. If ptr is NULL, no operation is performed.



                  but in your code context:



                  char *buf = new char(200);



                  the pointer is getted by new function, it's a C++ function, That's why your program crashed.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 '18 at 10:04









                  L.HaoL.Hao

                  71




                  71






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425831%2flinux-sockets-threading%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      How to change which sound is reproduced for terminal bell?

                      Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                      Can I use Tabulator js library in my java Spring + Thymeleaf project?