Hello,

using OpenMPI 1.10.2 and valgrind 3.11.0 I try to use the code below to
send a c++ string.

It works fine, but running through valgrind gives a lot of memory errors, 
invalid read of size...

What is going wrong there?

Valgrind output, see below.

Thanks!
Florian


// Compile with: mpicxx -std=c++11 -g -O0 -Wall -Wextra mpi.cpp
#include <mpi.h>
#include <iostream>
#include <string>

using namespace std;


void receive() {
  int length = 0;
  MPI_Status status;
  MPI_Probe(MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, &status);
  MPI_Get_count(&status, MPI_CHAR, &length);
  cout << "Stringlength = " << length << endl;
  char cstr[length];
  MPI_Recv(cstr,
           length,
           MPI_CHAR,
           MPI_ANY_SOURCE,
           MPI_ANY_TAG,
           MPI_COMM_WORLD,
           MPI_STATUS_IGNORE);
  cout << cstr << endl;
}

void send(int rankReceiver) {
  std::string s = "Hallo";
  MPI_Send(s.c_str(),
           s.size()+1,
           MPI_CHAR,
           rankReceiver,
           0,
           MPI_COMM_WORLD);
}

int main(int argc, char* argv[])
{
  int rank;
  MPI_Init(&argc, &argv);

  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  if (rank == 0)
    send(1);
  else {
    receive();
  }
  MPI_Finalize();
  return 0;
}


VALGRIND OUTPUT

% mpicxx -std=c++11 -g -O0 -Wall -Wextra mpi.cpp && mpirun -n 2 ./a.out         
          
Stringlength = 6
Hallo
florian@asaru ~/scratch (git)-[master] % 
LD_PRELOAD=/usr/lib/valgrind/libmpiwrap-amd64-linux.so mpirun -n 2 valgrind 
./a.out
==9290== Memcheck, a memory error detector
==9290== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==9290== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==9290== Command: ./a.out
==9290== 
==9291== Memcheck, a memory error detector
==9291== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==9291== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==9291== Command: ./a.out
==9291== 
valgrind MPI wrappers  9290: Active for pid 9290
valgrind MPI wrappers  9291: Active for pid 9291
valgrind MPI wrappers  9290: Try MPIWRAP_DEBUG=help for possible options
valgrind MPI wrappers  9291: Try MPIWRAP_DEBUG=help for possible options
Stringlength = 6
==9291== Invalid read of size 1
==9291==    at 0x4C2DBA2: strlen (in 
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9291==    by 0x56852D8: length (char_traits.h:267)
==9291==    by 0x56852D8: std::basic_ostream<char, std::char_traits<char> >& 
std::operator<< <std::char_traits<char> >(std::basic_ostream<char, 
std::char_traits<char> >&, char const*) (ostream:562)
==9291==    by 0x408A39: receive() (mpi.cpp:22)
==9291==    by 0x408B61: main (mpi.cpp:46)
==9291==  Address 0xffefff870 is on thread 1's stack
==9291==  in frame #2, created by receive() (mpi.cpp:8)
==9291== 
==9291== Invalid read of size 1
==9291==    at 0x4C2DBB4: strlen (in 
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9291==    by 0x56852D8: length (char_traits.h:267)
==9291==    by 0x56852D8: std::basic_ostream<char, std::char_traits<char> >& 
std::operator<< <std::char_traits<char> >(std::basic_ostream<char, 
std::char_traits<char> >&, char const*) (ostream:562)
==9291==    by 0x408A39: receive() (mpi.cpp:22)
==9291==    by 0x408B61: main (mpi.cpp:46)
==9291==  Address 0xffefff871 is on thread 1's stack
==9291==  in frame #2, created by receive() (mpi.cpp:8)
==9291== 
==9291== Invalid read of size 1
==9291==    at 0x60A0FF1: _IO_file_xsputn@@GLIBC_2.2.5 (in 
/usr/lib/libc-2.23.so)
==9291==    by 0x6096D1A: fwrite (in /usr/lib/libc-2.23.so)
==9291==    by 0x5684F75: sputn (streambuf:451)
==9291==    by 0x5684F75: __ostream_write<char, std::char_traits<char> > 
(ostream_insert.h:50)
==9291==    by 0x5684F75: std::basic_ostream<char, std::char_traits<char> >& 
std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, 
std::char_traits<char> >&, char const*, long) (ostream_insert.h:101)
==9291==    by 0x56852E6: std::basic_ostream<char, std::char_traits<char> >& 
std::operator<< <std::char_traits<char> >(std::basic_ostream<char, 
std::char_traits<char> >&, char const*) (ostream:561)
==9291==    by 0x408A39: receive() (mpi.cpp:22)
==9291==    by 0x408B61: main (mpi.cpp:46)
==9291==  Address 0xffefff874 is on thread 1's stack
==9291==  in frame #4, created by receive() (mpi.cpp:8)
==9291== 
==9291== Invalid read of size 1
==9291==    at 0x60A100D: _IO_file_xsputn@@GLIBC_2.2.5 (in 
/usr/lib/libc-2.23.so)
==9291==    by 0x6096D1A: fwrite (in /usr/lib/libc-2.23.so)
==9291==    by 0x5684F75: sputn (streambuf:451)
==9291==    by 0x5684F75: __ostream_write<char, std::char_traits<char> > 
(ostream_insert.h:50)
==9291==    by 0x5684F75: std::basic_ostream<char, std::char_traits<char> >& 
std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, 
std::char_traits<char> >&, char const*, long) (ostream_insert.h:101)
==9291==    by 0x56852E6: std::basic_ostream<char, std::char_traits<char> >& 
std::operator<< <std::char_traits<char> >(std::basic_ostream<char, 
std::char_traits<char> >&, char const*) (ostream:561)
==9291==    by 0x408A39: receive() (mpi.cpp:22)
==9291==    by 0x408B61: main (mpi.cpp:46)
==9291==  Address 0xffefff873 is on thread 1's stack
==9291==  in frame #4, created by receive() (mpi.cpp:8)
==9291== 
==9291== Invalid read of size 2
==9291==    at 0x4C2F9C0: __GI_memcpy (in 
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9291==    by 0x60A0F3A: _IO_file_xsputn@@GLIBC_2.2.5 (in 
/usr/lib/libc-2.23.so)
==9291==    by 0x6096D1A: fwrite (in /usr/lib/libc-2.23.so)
==9291==    by 0x5684F75: sputn (streambuf:451)
==9291==    by 0x5684F75: __ostream_write<char, std::char_traits<char> > 
(ostream_insert.h:50)
==9291==    by 0x5684F75: std::basic_ostream<char, std::char_traits<char> >& 
std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, 
std::char_traits<char> >&, char const*, long) (ostream_insert.h:101)
==9291==    by 0x56852E6: std::basic_ostream<char, std::char_traits<char> >& 
std::operator<< <std::char_traits<char> >(std::basic_ostream<char, 
std::char_traits<char> >&, char const*) (ostream:561)
==9291==    by 0x408A39: receive() (mpi.cpp:22)
==9291==    by 0x408B61: main (mpi.cpp:46)
==9291==  Address 0xffefff870 is on thread 1's stack
==9291==  in frame #5, created by receive() (mpi.cpp:8)
==9291== 
==9291== Invalid read of size 1
==9291==    at 0x4C2F9F8: __GI_memcpy (in 
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9291==    by 0x60A0F3A: _IO_file_xsputn@@GLIBC_2.2.5 (in 
/usr/lib/libc-2.23.so)
==9291==    by 0x6096D1A: fwrite (in /usr/lib/libc-2.23.so)
==9291==    by 0x5684F75: sputn (streambuf:451)
==9291==    by 0x5684F75: __ostream_write<char, std::char_traits<char> > 
(ostream_insert.h:50)
==9291==    by 0x5684F75: std::basic_ostream<char, std::char_traits<char> >& 
std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, 
std::char_traits<char> >&, char const*, long) (ostream_insert.h:101)
==9291==    by 0x56852E6: std::basic_ostream<char, std::char_traits<char> >& 
std::operator<< <std::char_traits<char> >(std::basic_ostream<char, 
std::char_traits<char> >&, char const*) (ostream:561)
==9291==    by 0x408A39: receive() (mpi.cpp:22)
==9291==    by 0x408B61: main (mpi.cpp:46)
==9291==  Address 0xffefff874 is on thread 1's stack
==9291==  in frame #5, created by receive() (mpi.cpp:8)
==9291== 
Hallo
==9291== 
==9291== HEAP SUMMARY:
==9291==     in use at exit: 96,351 bytes in 247 blocks
==9291==   total heap usage: 15,020 allocs, 14,773 frees, 13,362,383 bytes 
allocated
==9291== 
==9291== LEAK SUMMARY:
==9291==    definitely lost: 9,154 bytes in 39 blocks
==9291==    indirectly lost: 4,008 bytes in 22 blocks
==9291==      possibly lost: 0 bytes in 0 blocks
==9291==    still reachable: 83,189 bytes in 186 blocks
==9291==         suppressed: 0 bytes in 0 blocks
==9291== Rerun with --leak-check=full to see details of leaked memory
==9291== 
==9291== For counts of detected and suppressed errors, rerun with: -v
==9291== ERROR SUMMARY: 14 errors from 6 contexts (suppressed: 0 from 0)
==9290== 
==9290== HEAP SUMMARY:
==9290==     in use at exit: 96,351 bytes in 247 blocks
==9290==   total heap usage: 15,018 allocs, 14,771 frees, 13,370,045 bytes 
allocated
==9290== 
==9290== LEAK SUMMARY:
==9290==    definitely lost: 9,154 bytes in 39 blocks
==9290==    indirectly lost: 4,008 bytes in 22 blocks
==9290==      possibly lost: 0 bytes in 0 blocks
==9290==    still reachable: 83,189 bytes in 186 blocks
==9290==         suppressed: 0 bytes in 0 blocks
==9290== Rerun with --leak-check=full to see details of leaked memory
==9290== 
==9290== For counts of detected and suppressed errors, rerun with: -v
==9290== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Reply via email to