Author Topic: Find references for when destructors are called in C++  (Read 2899 times)

rowbearto

  • Senior Community Member
  • Posts: 2335
  • Hero Points: 132
Find references for when destructors are called in C++
« on: June 30, 2017, 03:00:38 PM »
When browsing C++ code, it can be tricky to know when destructors are called.

So a feature that would be useful for me is if I can do a cross reference search on a the calls to a destructor, such as:

ClassName::~ClassName()

If I can highlight the ~ClassName and press Ctrl+/ to find all the instances of when the destructor is called, not just when it is declared.

For example, if there is a block of code like this:

Code: [Select]
{
  ClassName classInstance;


}  <----- The destructor is called here

So when showing all the cross-references of the destructor, the closing brace of the above block would be shown as the compiler will add a call to the destructor here.

Similarly if we have a "delete ClassInstancePointer"

then we know the destructor will get called upon this deletion. Now this one is tricky because we don't know if base class or derived class destructor is called, so maybe all classes that derive from the type of ClassInstancePointer could be listed in the cross-reference results?

I realize this can be a tricky feature, and maybe won't catch everything, such as when a destructor is called when shared_ptr or templates are used, but I think it would still be very useful to have this feature for the simple destructor calling on the stack, or when "delete" is used.
« Last Edit: June 30, 2017, 03:07:09 PM by rowbearto »

jporkkahtc

  • Senior Community Member
  • Posts: 2620
  • Hero Points: 210
  • Text
Re: Find references for when destructors are called in C++
« Reply #1 on: July 06, 2017, 05:45:43 PM »
Slick does fairly sophisticated parsing of C++ code to find symbols, but it is far less than what an actual compiler does - with quite a bit of heuristics/guessing involved.
I don't think it would be able to reliably pick out where destructors are called.

OTOH, if the code can be compiled and is compiled, Slick could, in theory, get all this information from the debug symbols.

That would be a really nice enhancement to Slicks symbol analysis features

Dennis

  • Senior Community Member
  • Posts: 3961
  • Hero Points: 517
Re: Find references for when destructors are called in C++
« Reply #2 on: July 10, 2017, 09:14:27 PM »
Isn't this type of search typically more related to debugging?  It would probably be more logical to set a breakpoint in the destructor and let things get worked out at runtime.

rowbearto

  • Senior Community Member
  • Posts: 2335
  • Hero Points: 132
Re: Find references for when destructors are called in C++
« Reply #3 on: July 10, 2017, 09:16:56 PM »
I wanted this in the context of code browsing. I was trying to see how a particular function (which is called from inside a destructor and did a commit on a database transaction) gets called. I gave up on code browsing and then set a breakpoint in a debugger when I eventually found out how it is called.

So I'd really like this in terms of code browsing. Sure a debugger can be used, but that is much more overhead.

jporkkahtc

  • Senior Community Member
  • Posts: 2620
  • Hero Points: 210
  • Text
Re: Find references for when destructors are called in C++
« Reply #4 on: July 10, 2017, 09:38:58 PM »
Also the debugger won't find all references this way - only those you happen to hit while running.

Microsoft Research created "Vulcan" to let you do pretty sophisticated editing of binary executables.
It would either do this cross referencing, or let you build a tool that could do it.
https://blogs.msdn.microsoft.com/reiley/2011/08/06/microsoft-binary-technologies-and-debugging/