1

I am trying to debug possible memory leak caused by following line in my code:

 DeserializeRegex["Grp1"][strPtr] =
 std::shared_ptr<void>(operator new(10), [](void *pi) { delete pi; }); 

I think one possible cause can be using shared_ptr instead of more efficient make_shared. If so, how can I use make_shared in the above context? I checked few posts which mention custom deleter not possible to write with make_shared. Can anyone guide me to handle this memory leak?

Thanks in advance.

user1228352
  • 569
  • 6
  • 21
  • Also note that in this case you are also trying to delete an object that wasn't created with `new`. – user7860670 Mar 15 '19 at 07:32
  • Note that you may not `delete` a `void` pointer: http://eel.is/c++draft/expr.delete#1.sentence-4. You likely wanted to write `operator delete(pi);` instead. – Daniel Langr Mar 15 '19 at 07:39
  • A duplicate? This question is not at all about deleting a void pointer. It's about preventing a memory leak. Suggest to reopen. – Daniel Langr Mar 15 '19 at 07:43
  • Thanks Daniel. The question is about handling possible memory leak using make_shared. Please reopen. – user1228352 Mar 15 '19 at 08:39
  • The problem here is mismatching calls to `new` allocation function / `delete` operator. Attempt to delete a void pointer causes UB as described in dup question. Therefore memory leaks crashes or other nasty things may happen. You should either allocate memory and create objects with `new` expression and then destroy them and deallocate memory using `delete` expression or allocate memory with `new` allocation function and then deallocate memory using `delete` deallocation function. But not mix them together. @DanielLangr UB (memory leak) happens because this code tries to delete void pointer. – user7860670 Mar 15 '19 at 09:20
  • @VTT The problem is that the (yours and mine) suggested solution, i.e., to replace `delete` expression by `operator delete`, is not a subject of the duplicate. There is a different scenario, since there is no `operator new`. What you wrote in the comment should be an answer to this question in my opinion. – Daniel Langr Mar 15 '19 at 09:28
  • @user1228352 The solution is simple, as we already pointed out, you cannot mix `operator new` and `delete` _expression_. Use `operator delete` instead. Then, there is no memroy leak and you don't need `std::make_shared`. Note that the constructor of `std::shared_ptr` will call the deleter if an exception occurs: [If an exception is thrown, d(p) is called.](http://eel.is/c++draft/util.smartptr.shared.const#10.sentence-4). – Daniel Langr Mar 15 '19 at 09:30
  • Thanks @DanielLangr Sorry being newbie to advanced C++, might be asking stupid question. In order to use custom delete, what argument should I pass to operator delete(??). I mean in previous case, void pointer is passed and deleted the same. So now in case of custom delete, what should it delete as we are not passing anything? – user1228352 Mar 15 '19 at 10:26
  • 1
    @user1228352 Simply pass `pi` to `operator delete`: `... = std::shared_ptr(operator new(10), [](void *pi) { operator delete(pi); });`. – Daniel Langr Mar 15 '19 at 10:49

0 Answers0