0

我设计了一个类来处理多个 UNIX 套接字操作,例如发送和接收单个值或数组。关键在于统一服务器和客户端方法,以便在两种情况下使用相同的代码。

我的unix_socket.hpp

#ifndef __SOCKET_HPP
#define __SOCKET_HPP

#ifndef EIGEN_NO_DEBUG
#define EIGEN_NO_DEBUG
#endif

#include <Eigen/Dense>

#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <string>

#define DEFAULT_SOCKET_PATH (std::string(getenv("HOME"))+std::string("/msock")).data()
#define SERVER_MODE 0
#define CLIENT_MODE 1

using Eigen::VectorXd;
using std::string;

struct message{
    char type;    
    double value; 
};

class unix_socket{
public:
    double v_sent,v_recvd;
    char c_sent, c_recvd;

    VectorXd vector_recvd;
    VectorXd vector_sent;

    int sock, client_sock;
    unsigned int t,len;

    int mode; //0 server, 1 client

    struct sockaddr_un local,remote;

    string path;

    unix_socket();
    unix_socket(int sc_mode);
    ~unix_socket();

    void initSocket(const char* sock_path);
    void initSocket(const char* sock_path, const int sc_mode);
    void wait();
    void closeSocket();

    int sendMsg(const char type, const double value);
    int receiveMsg();
    int sendVectorXd(const VectorXd& values);
    int receiveVectorXd();

};
#endif

unix_socket.cpp

#include "unix_socket.hpp"

#include <iostream>
#include <cstdlib>

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

using namespace std;
using Eigen::VectorXd;

unix_socket::unix_socket(){
    v_sent=0;
    v_recvd=0;
    mode=0;
}

unix_socket::unix_socket(int sc_mode){
    v_sent=0;
    v_recvd=0;
    mode=sc_mode;
}

unix_socket::~unix_socket(){
    close(sock);
    close(client_sock);
    //system((std::string("rm ")+path).data());
}

void unix_socket::initSocket(const char* sock_path, const int sc_mode){

    if (sc_mode==0){
            // socket server 
        if (mode != sc_mode) mode=sc_mode;

        if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
            perror("socket");
            exit(1);
        }

        local.sun_family = AF_UNIX;
        strcpy(local.sun_path, sock_path);
        unlink(local.sun_path);
        len = strlen(local.sun_path) + sizeof(local.sun_family);
        if (bind(sock, (struct sockaddr *)&local, len) == -1) {
            close(sock);
            perror("bind"); exit(1);
        }

    } else if(sc_mode==1) {
            // socket client
        if (mode != sc_mode) mode=sc_mode;
        if ((client_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
            perror("socket");
            exit(1);
        }

        remote.sun_family = AF_UNIX;
        strcpy(remote.sun_path, sock_path);
        len = strlen(remote.sun_path) + sizeof(remote.sun_family);
        if (connect(client_sock, (struct sockaddr *)&remote, len) == -1) {
            perror("connect");
            exit(1);
        }
        printf("Connected.\n");
        // ---
    } else printf("Invalid sc_mode argument: %d\n",sc_mode);
    path=string(sock_path);
}

void unix_socket::initSocket(const char* sock_path){
    initSocket(sock_path,mode);
}

void unix_socket::wait(){
    if (mode==0){
        if (listen(sock, 5) == -1) {
            perror("listen");
            exit(1);
        }
        t = sizeof(remote);
        if ((client_sock = accept(sock, (struct sockaddr *)&remote, &t)) == -1) {
            perror("accept");
            exit(1);
        }
        printf("Connected!\n");
    } else printf("Invalid call to wait(): mode %d\n, must be 0",mode);
}

void unix_socket::closeSocket(){
    //close(sock);
    close(client_sock); 
}

int unix_socket::sendMsg(const char type, const double value){
    struct message msg;
    msg.type=type;
    msg.value=value;
    int n = send(client_sock, &msg, sizeof(msg), 0);
    if (n < 0) {
        perror("send");
        return -1;
    } 
    c_sent=msg.type;
    v_sent=msg.value;
    return 0;
}

int unix_socket::receiveMsg(){
    struct message msg;
    int n = recv(client_sock, &msg, sizeof(msg), 0);
    if (n < 0) {
        perror("recv");
        return -1;
    } 
    c_recvd=msg.type;
    v_recvd=msg.value;
    return 0;
}

int unix_socket::sendVectorXd(const VectorXd& values){
    sendMsg('A',values.rows());
    for (int i = 0; i < values.rows(); ++i){
        if (i<(values.rows()-1)) sendMsg('V',values(i));
        else sendMsg('L',values(i));
    }
    return 0;
}

int unix_socket::receiveVectorXd(){
    receiveMsg();
    if (c_recvd=='A' && v_recvd>1){
        vector_recvd=VectorXd::Zero(v_recvd);
        for (int i = 0; i < vector_recvd.rows(); ++i){
            receiveMsg();
            vector_recvd(i)=v_recvd;
        }
    }
    return 0;
};

.cpp总体而言,当使用在两个方向发送大(~1000)VectorXd 的simple 进行测试时,一切似乎都运行良好。但是,当在更复杂的程序中使用时,如下所示,malloc(): memory corruption:每次我通过unix_socket::closeSocket() 在服务器端调用关闭套接字时都会出现错误(上下文:程序正在向运行的 Webots 实例发送和接收数据)充当套接字客户端的机器人控制器):

#include <iostream>
#include <fstream>
#include <string>
#include "../socket/unix_socket.hpp"
#include "asm.hpp"

#include <sstream>

#include <Eigen/Dense>
#include <cstdlib>

using namespace Eigen;    
using namespace std;

int start_webots(const char* world){
    //starting webots as a child process with known PID
    int pid = fork();
    if (pid == 0){
        printf("Starting webots...");
        execlp("/usr/local/bin/webots","webots",world,NULL);
        printf("WEBOTS AUTOCLOSED!");
        exit(1);
    }
    else printf("Webots PID: %i\n", pid);
    return pid;
}    

VectorXd asmParams(bool fall_break, int periods, double delay){
    VectorXd p(5); 
    if (fall_break==true) p << 1,-1,1,periods,delay;
    else p << 1,-1,0,periods,delay;
    return p;
}

int main () {

  int periods=10;
  double delay=25.0;
  string world_path="../../worlds/darwin_cpgs_noise.wbt";

  start_webots(world_path.data());

  unix_socket server;

  server.initSocket(DEFAULT_SOCKET_PATH,0);
  server.wait();

  server.receiveVectorXd();      
  server.sendVectorXd(asmParams(true,periods,delay));
  server.sendVectorXd(VectorXd::Ones(dim)*10);

  asmemory aSm;
  aSm.setMode(0);
  bool end=false;

  aSm.resetTimer();
  while (aSm.isAcquiring()){
    aSm.resetTimer();
    while(aSm.isRunningRollout()){
      server.receiveMsg();

      if (server.c_recvd=='T' && aSm.lastTimeStep()) {
        aSm.resize(NO_CHANGE,aSm.nTimeSteps()+1,NO_CHANGE);
      }
      else if (server.c_recvd=='E' && !aSm.lastTimeStep()) {
        aSm.resize(NO_CHANGE,aSm.index_t()+1,NO_CHANGE);
        end=true;
      }
      else if (server.c_recvd=='E' && aSm.lastTimeStep()) {
        end=true;
      }

      aSm.setCurrentTime(server.v_recvd);
      server.receiveVectorXd();
      if (server.vector_recvd.rows()!=aSm.nSensors()) aSm.resize(server.vector_recvd.rows(),NO_CHANGE,NO_CHANGE);
      aSm.sendToBuffer(server.vector_recvd);

      if (end) break; 
      aSm.incrementTime();
      end=false;
    }
    aSm.incrementSample();
    server.sendMsg('R',-1.0);

    // crashing the program:
    server.closeSocket();

    end=false;
  }

  aSm.learnData();
  server.closeSocket();  
  return 0;
}

什么可能导致内存损坏?我假设unix_socket课堂上有问题,但数据发送/接收正确,但是每次我调用时closeSocket(),即使在循环中间也会发送内存损坏错误。

编辑:我试过使用 Valgrind,虽然程序中似乎有很多内存泄漏,但我真的不知道如何解释输出(第一次使用 Valgrind):

==3874== Syscall param socketcall.sendto(msg) points to uninitialised byte(s)
==3874==    at 0x573DDA2: send (send.c:28)
==3874==    by 0x406E12: unix_socket::sendMsg(char, double) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==    by 0x402DB9: main (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==  Address 0x7fefffe91 is on thread 1's stack
==3874== 
==3874== 
==3874== HEAP SUMMARY:
==3874==     in use at exit: 2,451,739 bytes in 107 blocks
==3874==   total heap usage: 316 allocs, 209 frees, 2,454,518 bytes allocated
==3874== 
==3874== Searching for pointers to 107 not-freed blocks
==3874== Checked 2,640,168 bytes
==3874== 
==3874== 59 bytes in 1 blocks are possibly lost in loss record 1 of 4
==3874==    at 0x4C2C221: operator new(unsigned long) (vg_replace_malloc.c:298)
==3874==    by 0x4EF03B8: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17)
==3874==    by 0x4EF1D94: char* std::string::_S_construct<char const*>(char const*, char const*, std::allocator<char> const&, std::forward_iterator_tag) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17)
==3874==    by 0x4EF1E72: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17)
==3874==    by 0x4026FE: main (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874== 
==3874== LEAK SUMMARY:
==3874==    definitely lost: 0 bytes in 0 blocks
==3874==    indirectly lost: 0 bytes in 0 blocks
==3874==      possibly lost: 59 bytes in 1 blocks
==3874==    still reachable: 2,451,680 bytes in 106 blocks
==3874==         suppressed: 0 bytes in 0 blocks
==3874== Reachable blocks (those to which a pointer was found) are not shown.
==3874== To see them, rerun with: --leak-check=full --show-reachable=yes
==3874== 
==3874== Use --track-origins=yes to see where uninitialised values come from
==3874== ERROR SUMMARY: 1620 errors from 10 contexts (suppressed: 0 from 0)
==3874== 
==3874== 1 errors in context 1 of 10:
==3874== Syscall param socketcall.sendto(msg) points to uninitialised byte(s)
==3874==    at 0x573DDA2: send (send.c:28)
==3874==    by 0x406E12: unix_socket::sendMsg(char, double) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==    by 0x402DB9: main (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==  Address 0x7fefffe91 is on thread 1's stack
==3874== 
==3874== 
==3874== 1 errors in context 2 of 10:
==3874== Syscall param socketcall.sendto(msg) points to uninitialised byte(s)
==3874==    at 0x573DDA2: send (send.c:28)
==3874==    by 0x406E12: unix_socket::sendMsg(char, double) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==    by 0x407100: unix_socket::sendVectorXd(Eigen::Matrix<double, -1, 1, 0, -1, 1> const&) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==    by 0x402965: main (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==  Address 0x7fefffe41 is on thread 1's stack
==3874== 
==3874== 
==3874== 1 errors in context 3 of 10:
==3874== Syscall param socketcall.sendto(msg) points to uninitialised byte(s)
==3874==    at 0x573DDA2: send (send.c:28)
==3874==    by 0x406E12: unix_socket::sendMsg(char, double) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==    by 0x40706E: unix_socket::sendVectorXd(Eigen::Matrix<double, -1, 1, 0, -1, 1> const&) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==    by 0x402965: main (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==  Address 0x7fefffe41 is on thread 1's stack
==3874== 
==3874== 
==3874== 1 errors in context 4 of 10:
==3874== Syscall param socketcall.sendto(msg) points to uninitialised byte(s)
==3874==    at 0x573DDA2: send (send.c:28)
==3874==    by 0x406E12: unix_socket::sendMsg(char, double) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==    by 0x407100: unix_socket::sendVectorXd(Eigen::Matrix<double, -1, 1, 0, -1, 1> const&) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==    by 0x4028D8: main (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==  Address 0x7fefffe41 is on thread 1's stack
==3874== 
==3874== 
==3874== 1 errors in context 5 of 10:
==3874== Syscall param socketcall.sendto(msg) points to uninitialised byte(s)
==3874==    at 0x573DDA2: send (send.c:28)
==3874==    by 0x406E12: unix_socket::sendMsg(char, double) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==    by 0x40706E: unix_socket::sendVectorXd(Eigen::Matrix<double, -1, 1, 0, -1, 1> const&) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==    by 0x4028D8: main (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==  Address 0x7fefffe41 is on thread 1's stack
==3874== 
==3874== 
==3874== 4 errors in context 6 of 10:
==3874== Syscall param socketcall.sendto(msg) points to uninitialised byte(s)
==3874==    at 0x573DDA2: send (send.c:28)
==3874==    by 0x406E12: unix_socket::sendMsg(char, double) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==    by 0x4070CC: unix_socket::sendVectorXd(Eigen::Matrix<double, -1, 1, 0, -1, 1> const&) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==    by 0x4028D8: main (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==  Address 0x7fefffe41 is on thread 1's stack
==3874== 
==3874== 
==3874== 19 errors in context 7 of 10:
==3874== Syscall param socketcall.sendto(msg) points to uninitialised byte(s)
==3874==    at 0x573DDA2: send (send.c:28)
==3874==    by 0x406E12: unix_socket::sendMsg(char, double) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==    by 0x4070CC: unix_socket::sendVectorXd(Eigen::Matrix<double, -1, 1, 0, -1, 1> const&) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==    by 0x402965: main (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==  Address 0x7fefffe41 is on thread 1's stack
==3874== 
==3874== 
==3874== 120 errors in context 8 of 10:
==3874== Invalid write of size 8
==3874==    at 0x408B56: asmemory::sendToBuffer(Eigen::Matrix<double, -1, 1, 0, -1, 1> const&) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==    by 0x402D48: main (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==  Address 0x5a1e360 is 0 bytes after a block of size 24,000 alloc'd
==3874==    at 0x4C2C73C: malloc (vg_replace_malloc.c:270)
==3874==    by 0x40313F: Eigen::internal::aligned_malloc(unsigned long) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==    by 0x4054DA: void* Eigen::internal::conditional_aligned_malloc<true>(unsigned long) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==    by 0x40503C: double* Eigen::internal::conditional_aligned_new_auto<double, true>(unsigned long) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==    by 0x404937: Eigen::DenseStorage<double, -1, -1, -1, 0>::resize(long, long, long) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==    by 0x40C552: Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >::resize(long, long) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==    by 0x40CA07: void Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >::resizeLike<Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, Eigen::Matrix<double, -1, -1, 0, -1, -1> > >(Eigen::EigenBase<Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, Eigen::Matrix<double, -1, -1, 0, -1, -1> > > const&) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==    by 0x40C3F0: void Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >::_resize_to_match<Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, Eigen::Matrix<double, -1, -1, 0, -1, -1> > >(Eigen::EigenBase<Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, Eigen::Matrix<double, -1, -1, 0, -1, -1> > > const&) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==    by 0x40BDD0: Eigen::Matrix<double, -1, -1, 0, -1, -1>& Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >::lazyAssign<Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, Eigen::Matrix<double, -1, -1, 0, -1, -1> > >(Eigen::DenseBase<Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, Eigen::Matrix<double, -1, -1, 0, -1, -1> > > const&) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==    by 0x40B54B: Eigen::internal::assign_selector<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >, false, false>::run(Eigen::Matrix<double, -1, -1, 0, -1, -1>&, Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==    by 0x40AD46: Eigen::Matrix<double, -1, -1, 0, -1, -1>& Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >::_set_noalias<Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, Eigen::Matrix<double, -1, -1, 0, -1, -1> > >(Eigen::DenseBase<Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, Eigen::Matrix<double, -1, -1, 0, -1, -1> > > const&) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==    by 0x40A0C4: void Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >::_set_selector<Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, Eigen::Matrix<double, -1, -1, 0, -1, -1> > >(Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::internal::false_type const&) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874== 
==3874== 
==3874== 1471 errors in context 9 of 10:
==3874== Invalid write of size 8
==3874==    at 0x408FD7: asmemory::setCurrentTime(double) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==    by 0x402B53: main (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==  Address 0x5c641f0 is 0 bytes after a block of size 800 alloc'd
==3874==    at 0x4C2C73C: malloc (vg_replace_malloc.c:270)
==3874==    by 0x40313F: Eigen::internal::aligned_malloc(unsigned long) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==    by 0x4054DA: void* Eigen::internal::conditional_aligned_malloc<true>(unsigned long) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==    by 0x40503C: double* Eigen::internal::conditional_aligned_new_auto<double, true>(unsigned long) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==    by 0x405F05: Eigen::DenseStorage<double, -1, -1, 1, 0>::resize(long, long, long) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==    by 0x405CFC: Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >::resize(long, long) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==    by 0x407605: void Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >::resizeLike<Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, Eigen::Matrix<double, -1, 1, 0, -1, 1> > >(Eigen::EigenBase<Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, Eigen::Matrix<double, -1, 1, 0, -1, 1> > > const&) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==    by 0x4074E0: void Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >::_resize_to_match<Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, Eigen::Matrix<double, -1, 1, 0, -1, 1> > >(Eigen::EigenBase<Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, Eigen::Matrix<double, -1, 1, 0, -1, 1> > > const&) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==    by 0x40749C: Eigen::Matrix<double, -1, 1, 0, -1, 1>& Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >::lazyAssign<Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, Eigen::Matrix<double, -1, 1, 0, -1, 1> > >(Eigen::DenseBase<Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, Eigen::Matrix<double, -1, 1, 0, -1, 1> > > const&) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==    by 0x407477: Eigen::internal::assign_selector<Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >, false, false>::run(Eigen::Matrix<double, -1, 1, 0, -1, 1>&, Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==    by 0x407442: Eigen::Matrix<double, -1, 1, 0, -1, 1>& Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >::_set_noalias<Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, Eigen::Matrix<double, -1, 1, 0, -1, 1> > >(Eigen::DenseBase<Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, Eigen::Matrix<double, -1, 1, 0, -1, 1> > > const&) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874==    by 0x407408: void Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >::_set_selector<Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, Eigen::Matrix<double, -1, 1, 0, -1, 1> > >(Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::internal::false_type const&) (in /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server)
==3874== 
==3874== ERROR SUMMARY: 1620 errors from 10 contexts (suppressed: 0 from 0)
4

1 回答 1

0

对于内存错误

valgrind --track-origins=yes -v [你的程序]

给出未初始化的内存访问的痕迹,这可能会导致您的程序的未定义行为。

对于泄漏,

valgrind --leak-check=full --show-reachable=yes -v [你的程序]

输出是一组trace的形式,每一个trace都是从下往上分析的,

例如。


==3874== 1 个块中的 59 个字节可能在丢失记录 1 of 4 中丢失

==3874== at 0x4C2C221: operator new(unsigned long) (vg_replace_malloc.c:298)

==3874== by 0x4EF03B8: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator const&) (在 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0. 17)

==3874== by 0x4EF1D94: char* std::string::_S_construct(char const*, char const*, std::allocator const&, std::forward_iterator_tag) (在 /usr/lib/x86_64-linux-gnu/ libstdc++.so.6.0.17)

==3874== by 0x4EF1E72: std::basic_string, std::allocator >::basic_string(char const*, std::allocator const&) (在 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0 .17)

==3874== 由 0x4026FE:主要(在 /home/joao/CloudPT/Bolsa/Webots/controllers/darwin-pi2/server 中)


call tr​​ace从main开始,然后调用basic_string,再到上面的函数调用,以此类推,最后到达new call,但是调用new的函数是__S_create函数,表示调用这个函数后没有free执行或者程序终止时没有为此函数调用调用 deinit 方法。

高温高压

于 2013-10-18T10:22:09.643 回答