Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Building with Valgrind #192

Open
andrewkatson opened this issue Oct 22, 2020 · 3 comments
Open

Building with Valgrind #192

andrewkatson opened this issue Oct 22, 2020 · 3 comments

Comments

@andrewkatson
Copy link

Whenever i run my program I have a lot of memory leaks that come from boost -- i know i cause them but it just says boost. I was reading that Boost Context (a dependency) has to be built with valgrind=on as an option and if not this can cause these erroneous errors.. Is there a possibility that something that could be done here. I looked at the BUILD file and I could not find anything. Sorry if this is the wrong place for this!

@nelhage
Copy link
Owner

nelhage commented Oct 23, 2020

Are you able to share your application or a minimal test case that demonstrates the problem? This would be easier to investigate with a reproducer.

@andrewkatson
Copy link
Author

andrewkatson commented Oct 26, 2020

I am still trying to create an example that produces true leaks. I am only able to create small examples that produce "still reachable" leaks which are not my concern (I think altho i suppose over time they become one).

I will post it though just for completeness.

sample_event.proto

 syntax = "proto3";

 package common.example;

 message SampleEvent {
   int64 checksum = 1;
   bool can_handle_async = 2;
 }

memory_leak.cc

 #include <iostream>
 #include <memory>

 #include <boost/fiber/fiber.hpp>

 #include "Examples/MemoryLeak/sample_event.pb.h"


 void waitOnFibers(std::vector<std::unique_ptr<boost::fibers::fiber>> &fibersRunning) {

   for (auto &fiber : fibersRunning) {
     fiber->join();
   }
   fibersRunning.clear();
 }

 void handleEvent(const common::example::SampleEvent& event) {
   std::cout << "My val " << event.checksum() << std::endl;
 }

 void handleEvents(std::vector<std::unique_ptr<common::example::SampleEvent>>&      events) {

   std::vector<std::unique_ptr<boost::fibers::fiber>> runningFibers;
   for (std::unique_ptr<common::example::SampleEvent> &event : events) {
     if (event->can_handle_async()) {
       std::unique_ptr<boost::fibers::fiber> fb {  new boost::fibers::fiber([&](){      handleEvent(*event); })};
       runningFibers.emplace_back(std::move(fb));
     } else {
       waitOnFibers(runningFibers);
       handleEvent(*event);
     }
   }

   waitOnFibers(runningFibers);
 }

 int main() {

   std::unique_ptr<common::example::SampleEvent> s1 {new      common::example::SampleEvent()};
   s1->set_checksum(10);
   s1->set_can_handle_async(true);

   std::unique_ptr<common::example::SampleEvent> s2 {new common::example::SampleEvent()};
   s2->set_checksum(2);
   s2->set_can_handle_async(true);

   std::unique_ptr<common::example::SampleEvent> s3 {new common::example::SampleEvent()};
   s3->set_checksum(1);
   s3->set_can_handle_async(false);

   std::vector<std::unique_ptr<common::example::SampleEvent>> events;
   events.emplace_back(std::move(s1));
   events.emplace_back(std::move(s2));
   events.emplace_back(std::move(s3));

   handleEvents(events);

   return 0;
 }

@andrewkatson
Copy link
Author

Friendly ping :) did you guys ever determine how boost was being built with boost context. I think my example is still valid. The bytes are eventually lost.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants