0

I've tried switching some C++Builder 2010 code using new to use boost::make_shared<>, as below.

Old:

boost::shared_ptr<TStringList> l(new TStringList());

New:

boost::shared_ptr<TStringList> l(boost::make_shared<TStringList>());

l->Add("foo"); //dies here

The old code works, but the new code dies when I try and use the pointer (AV, or just hangs). I've used make_shared and shared_ptr successfully before, but never with TObject descendants. Is this a known problem - perhaps something to do withe the way make_shared uses placement new() ??

Roddy
  • 66,617
  • 42
  • 165
  • 277

1 Answers1

1

I imagine the placement new is indeed problematic because TObject has expectations about memory allocations which new satisfies (because C++Builder's RTL internally routes C/C++ memory routines to Delphi RTL routines in VCL projects), but which placement new does not.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Odd how things come back to bite you. Three years on, I trip over the exact same problem, and google - and StackOverflow - lead me to a question I'd forgotten I even asked ;) – Roddy Feb 10 '16 at 19:46