c++ - How to fix the Ubuntu 64 bit Linux bus error I get when trying to access the memory mapped address, m_pControl, returned by mmap? -


i fix ubuntu 64 bit linux bus error when trying access memory mapped address, m_pcontrol, returned mmap. tried memset did not prevent bus error on following source code line after memset. windows 7 version of code apparently not have bus error.

bool cdatatransferserver::initialize(int ncameraid,                                       cc_sampletype ndatatype,                                      unsigned int nimagewidth,                                       unsigned int nimageheight,                                      unsigned int nmaxframes) {     bool bokay = true;     if (m_binitialized)         return true;          char buffer[256];         m_strmemoryname = l"global\\smartcammem";                 size_t origsize = strlen(m_szobjnamesuffix) + 1;                 const size_t newsize = 100;                 size_t convertedchars = 0;                 wchar_t wcstring[newsize];                 mbstowcs(wcstring, m_szobjnamesuffix,origsize);                 m_strmemoryname += wcstring;             wcstombs(buffer,m_strmemoryname.c_str(),256);         int fd = open(buffer, o_creat | o_rdwr);         m_pcontrol = fd >= 0 ? (cdatatransfercontrol*)mmap(null, m_nfilemapsize,prot_read|prot_write,map_shared,fd,0) : null;          if (m_pcontrol)                       {                memset(m_pcontrol,0x0,sizeof(m_pcontrol));             m_pcontrol->nconfigurationblock = 0xffffffff;             m_pcontrol->nnewestkeydatablock = 0xffffffff;             m_pcontrol->nnewestdatablock = 0xffffffff;             m_pcontrol->nblockheaders = m_nmaxframes + m_nconfigblocks;             m_pcontrol->ntimeoflastclientaccess = ::gettickcount();         }         else             bokay = false;    return bokay; 

}

my m_pcontrol return value 0x550c6000 , fd 3.

here test c++ driver program.

#include  <stdio.h> #include  <iostream> #include  <dlfcn.h> #include "cameracontroldefs.h" #include "dataserver.h"  using namespace std;   int main(int argc,char** argv) {     bool (*sayhello)(cdatatransferserver* _this, int ncameraid, cc_sampletype ndatatype,                                 unsigned int nimagewidth, unsigned int nimageheight,                                 unsigned int nmaxframes);     unsigned int tmp;     void* handle = dlopen("libdataserver.so", rtld_lazy);   if (!handle)   {     std::cerr << dlerror() << std::endl;     return 1;   }    // load symbols     create_t* create_triangle = (create_t*) dlsym(handle, "create");     const char* dlsym_error = dlerror();     if (dlsym_error) {         cerr << "cannot load symbol create: " << dlsym_error << '\n';         return 1;     }      destroy_t* destroy_triangle = (destroy_t*) dlsym(handle, "destroy");     dlsym_error = dlerror();     if (dlsym_error) {         cerr << "cannot load symbol destroy: " << dlsym_error << '\n';         return 1;     }      // create instance of class     cdatatransferserver* poly = create_triangle();     *(bool **)(&sayhello) = (bool *)dlsym(handle,"_zn19cdatatransferserver10initializeei13cc_sampletypejjj");   if (dlerror())   {     std::cerr << dlerror() << std::endl;     return 2;   }     printf("ywc = %x\n",poly);   // destroy class     (*sayhello)(poly, 5, cc_sampletype_mpeg4,128,256,64);     destroy_triangle(poly);      // unload triangle library   dlclose(handle);      return 0;  } 

please explain bus error. please advise on whether should change of arguments mmap or alternate approach. thank you

i wrote , tested code excerpt morning @ 4 a.m. hope helps other in similar solution. if have question. please enter comment , you.

    if (bokay)     {                  char buffer[256];         m_strmemoryname = l"global\\smartcammem";                 size_t origsize = strlen(m_szobjnamesuffix) + 1;                 const size_t newsize = 100;                 size_t convertedchars = 0;                 wchar_t wcstring[newsize];                 mbstowcs(wcstring, m_szobjnamesuffix,origsize);                 m_strmemoryname += wcstring;             wcstombs(buffer,m_strmemoryname.c_str(),256);             unlink(buffer);         int fd = open(buffer, o_creat | o_rdwr);                 printf("fd = %d\n",fd);                 int page_size = getpagesize();                 printf("result = %d\n",posix_fallocate(fd, 0, page_size + m_nfilemapsize + 1));          m_pcontrol = fd >= 0 ? (cdatatransfercontrol*)mmap(null, m_nfilemapsize,prot_read|prot_write,map_shared,fd,0) : null;               printf("m_pcontrol = %x\n",m_pcontrol);                 if (m_pcontrol)         {             memset(m_pcontrol, 0,sizeof(m_pcontrol));             m_pcontrol->nconfigurationblock = 0xffffffff;             m_pcontrol->nnewestkeydatablock = 0xffffffff;             m_pcontrol->nnewestdatablock = 0xffffffff;             m_pcontrol->nblockheaders = m_nmaxframes + m_nconfigblocks;             m_pcontrol->ntimeoflastclientaccess = ::gettickcount();         }         else             bokay = false;      } 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -