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

rowbearto

  • Senior Community Member
  • Posts: 589
  • Hero Points: 16
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: 1249
  • Hero Points: 91
  • 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

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 2111
  • Hero Points: 265
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: 589
  • Hero Points: 16
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: 1249
  • Hero Points: 91
  • 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/