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
#include // FOR COLIRU
#include
#include
#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;
template
using Map = boost::unordered_map>>;
using Timers = Map;
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@lists.boost.org> wrote:
El 28/09/2024 a las 9:49, Murali Kishore via Boost escribió:
[...]
// Define the unordered_map type
typedef boost::unordered_map, std::less, MapAllocator>
timer_instance_map_t;
You have to use std::equal_to rather than
std::less. 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