Boost logo

Boost :

From: Seth (bugs_at_[hidden])
Date: 2024-09-30 19:01:41


You still leak instances, because you do a redundant `construct<>` call before emplacing. Always simplify:

[Live On Coliru](https://coliru.stacked-crooked.com/a/4d1a689fc03f3910)

```c++
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/managed_mapped_file.hpp> // FOR COLIRU
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/unordered_map.hpp>
#include <iostream>
namespace bip = boost::interprocess;

// Define your types
using Id = int;
struct Timer { int id; };

// Define allocators for shared memory
// using Segment = bip::managed_shared_memory;
using Segment = bip::managed_mapped_file; // FOR COLIRU

template <typename T> using Allocator = bip::allocator<T, Segment::segment_manager>;
template <typename K, typename V, typename H = std::hash<K>, typename Eq = std::equal_to<K>>
using Map = boost::unordered_map<K, V, H, Eq, Allocator<std::pair<K const, V>>>;
using Timers = Map<Id, Timer>;

int main() {
    // Create managed shared memory
    Segment segment(bip::open_or_create, "MySharedMemory", 65536);

    // Construct the unordered map in shared memory
    Timers* timers = segment.find_or_construct<Timers>("TimerInstanceMap") //
                     (10, segment.get_segment_manager());

    timers->emplace(100, Timer{100});
    timers->emplace(200, Timer{200});

    // Accessing the instance
    for (Id id : {100, 150, 200})
        if (auto it = timers->find(id); it != timers->end())
            std::cout << "Found timer instance with id: " << it->second.id << std::endl;
        else
            std::cout << "Timer instance not found." << std::endl;

    for (auto const& pair : *timers)
        std::cout << "Timer Id: " << pair.first << ", Timer Instance Id: " << pair.second.id << std::endl;

    // Cleanup: You may want to remove the shared memory segment
    std::remove("MySharedMemory"); // FOR COLIRU
    // Segment::remove("MySharedMemory");
}
```

On Sat, Sep 28, 2024, at 11:47 AM, Murali Kishore via Boost wrote:
> Thanks, it worked.
>
> On Sat, Sep 28, 2024 at 2:36 PM Joaquin M López Muñoz via Boost <
> boost_at_[hidden]> wrote:
>
>> El 28/09/2024 a las 9:49, Murali Kishore via Boost escribió:
>> > [...]
>> >
>> > // Define the unordered_map type
>> > typedef boost::unordered_map<timer_id_t, timer_instance_t,
>> > std::hash<timer_id_t>, std::less<timer_id_t>, MapAllocator>
>> > timer_instance_map_t;
>>
>> You have to use std::equal_to<timer_id_t> rather than
>> std::less<timer_id_t>. Please try and let us know if that solves your
>> issue.
>>
>> Joaquin M Lopez Munoz
>>
>> _______________________________________________
>> Unsubscribe & other changes:
>> http://lists.boost.org/mailman/listinfo.cgi/boost
>>
>
>
> --
> Regards,
> Murali Kishore
>
> _______________________________________________
> Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk