2015-09-25 51 views
0

我是新來提升。 我使用「提升管理的共享內存」下面的示例中,但同時分配在以下行中的共享段的內存一個實例被撞壞:如果在Windows上使用managed_shared_memory,則會崩潰

char_string key_object(keyHashStr.c_str(), alloc_inst3); 

的崩潰發生時我的樣品只有一個以上的實例應用程序正在同時運行。 如果我使用「提升管理的Windows共享內存」,那麼沒有崩潰。

有人能讓我知道我在做什麼錯嗎?

#include <boost/interprocess/sync/interprocess_mutex.hpp> 
    #include <boost/interprocess/sync/scoped_lock.hpp> 
    #include <boost/interprocess/managed_shared_memory.hpp> 
    #include <boost/interprocess/shared_memory_object.hpp> 
    #include <boost/interprocess/allocators/allocator.hpp> 
    #include <boost/interprocess/containers/flat_map.hpp> 
    #include <boost/interprocess/containers/set.hpp> 
    #include <boost/interprocess/containers/string.hpp> 
    #include <string> 
    #include <Windows.h> 
    #include <iostream> 

    #define AMMSRCUNITLIMIT 0x0100000 /* 1M bytes */ 
    #define RESERVED_BUFFER_SIZE_WRITE (8 * AMMSRCUNITLIMIT) 
    #define SHM_SIZE_FOR_TRACKING 65536 
    #define MAX_PATH 260 

    using namespace boost::interprocess; 

    //Typedefs of allocators and containers 
    typedef allocator<void, managed_shared_memory::segment_manager>  void_allocator; 
    typedef allocator<char, managed_shared_memory::segment_manager>  char_allocator; 
    typedef boost::interprocess::basic_string<char, std::char_traits<char>, char_allocator> char_string; 

    int main(int argc, char *argv[]) 
    { 
      managed_shared_memory *m_sharedMemUsage = NULL; 
      string keyHashStr; 

      try 
      { 
        m_sharedMemUsage = new managed_shared_memory(create_only, "MyBookkeeper", 2 * RESERVED_BUFFER_SIZE_WRITE); 
        keyHashStr = "AAAAAAAAAAAAAAAAAAAAAAA"; 
      } 
      catch (...) 
      { 
        while (true) 
        { 
         try{ 
           m_sharedMemUsage = new managed_shared_memory(open_only, "MyBookkeeper"); 
           break; 
         } 
         catch (...) 
         { 
           std::cout << "Some problem, trying again" << std::endl; 
         } 
        } 
        keyHashStr = "AAAAAAAAAAAAAAAAAAAAAAB"; 
      } 

      { 
        char_allocator alloc_inst3 = m_sharedMemUsage->get_segment_manager()->get_allocator<char>(); 
        int count = 0; 
        while (count < 100000) 
        { 
         char_string key_object(keyHashStr.c_str(), alloc_inst3); 
         ++count; 
        } 
      } 
    } 
+0

定義'crash'。它是否會拋出異常?這個過程是否會死亡? –

+0

如果拋出異常,可能有其背後的原因,反覆嘗試同樣的事情不會有太大的幫助。 「瘋狂的定義是做同樣的事情,但期待不同的結果」。相反,試着去理解*爲什麼會拋出異常,並且可以採取什麼措施來防止它。 –

+0

@MarkJansen:通過崩潰我的意思是這個過程正在消失 – rban

回答

1

我也有類似的問題。這個過程在提升內部崩潰。在我的情況下,當兩個進程試圖同時創建或打開一個特定的共享內存區域時,其中一個進程在之後的「for」循環中崩潰。 我在創建共享內存區域時使用了「提升命名互斥體」。這對我有效。如果我們拿sehe的代碼,那麼我認爲下面的修改會解決問題。

#include <boost/interprocess/managed_shared_memory.hpp> 
#include <boost/interprocess/allocators/allocator.hpp> 
#include <boost/interprocess/containers/string.hpp> 
#include <boost/interprocess/sync/named_mutex.hpp> 
#include <string> 
#include <iostream> 

#define RESERVED_BUFFER_SIZE_WRITE (8 * 0x0100000) 

namespace bip = boost::interprocess; 

//Typedefs of allocators and containers 
typedef bip::allocator<char, bip::managed_shared_memory::segment_manager> char_allocator; 
typedef bip::basic_string<char, std::char_traits<char>, char_allocator> char_string; 


int main() 
{ 
    boost::interprocess::named_mutex MyBookkeeperMutex(boost::interprocess::open_or_create, "MyBookkeeperMutex"); 

    MyBookkeeperMutex.lock(); 
    bip::managed_shared_memory m_sharedMemUsage(bip::open_or_create, "MyBookkeeper", 2 * RESERVED_BUFFER_SIZE_WRITE); 
    MyBookkeeperMutex.unlock(); 

    char_allocator alloc_inst3(m_sharedMemUsage.get_segment_manager()); 
    for (int count = 0; count < 100000; ++count) { 
     char_string key_obect("AAAAAAAAAAAAAAAAAAAAAAA", alloc_inst3); 
    } 
    boost::interprocess::named_mutex::remove("MyBookkeeperMutex"); 
} 
+0

問題解決了。這個真的對我有用。 – rban

+0

@rban參見http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – sehe

0

我建議使用open_or_create標誌代替。

#include <boost/interprocess/managed_shared_memory.hpp> 
#include <boost/interprocess/allocators/allocator.hpp> 
#include <boost/interprocess/containers/string.hpp> 
#include <string> 
#include <iostream> 

#define RESERVED_BUFFER_SIZE_WRITE (8 * 0x0100000) 

namespace bip = boost::interprocess; 

//Typedefs of allocators and containers 
typedef bip::allocator<char, bip::managed_shared_memory::segment_manager> char_allocator; 
typedef bip::basic_string<char, std::char_traits<char>, char_allocator> char_string; 

int main() 
{ 
    bip::managed_shared_memory m_sharedMemUsage(bip::open_or_create, "MyBookkeeper", 2 * RESERVED_BUFFER_SIZE_WRITE); 

    char_allocator alloc_inst3(m_sharedMemUsage.get_segment_manager()); 
    for (int count = 0; count < 100000; ++count) { 
     char_string key_obect("AAAAAAAAAAAAAAAAAAAAAAA", alloc_inst3); 
    } 
} 

注意這如何不泄漏資源,因爲類的析構函數實際運行...

+0

嘗試使用open_or_create,但仍然崩潰。 – rban

相關問題