Share via


how can i set a breakpoint on a default constructor

Question

Tuesday, February 24, 2015 8:15 PM

I'd like to catch all instances where a specific class is constructed but only the default constructor exists and it is implicitly defined.  How can I break in the default constructor?  

thanks,

All replies (2)

Tuesday, February 24, 2015 8:50 PM ✅Answered

Just define a default constructor explicitly and put the break point in there:

public YourClass()
        {
            //put breakpoint here.
        }

A default constructor is only added to the class unless you define one yourself.

Please remember to mark helpful posts as answer to close your threads and then start a new thread if you have a new question.


Tuesday, February 24, 2015 9:23 PM ✅Answered

There is no code where you want a breakpoint, so I'm not sure what instruction you'd like the debugger to break your program at.  ???

Since your goal is only to "catch" all instances where a specific class is constructed I think the only thing is to modify your code to do so.  I can't think of an approach that doesn't require a code modification and recompile.  Especially since it's not clear where such classes were inlined in already compiled code.  And because of that alone, I'm fairly certain you have to instrument your code (modify your code by inserting something special) to catch it.  

So can you modify the class you want to catch?  Just add a default constructor and set your breakpoint in there.

Alternatively if you really want to try to do what you are doing, you can step into any code that creates an instance of your class in disassembly and set a breakpoint in the disassembled code for the constructor (assuming you can locate it, and it even exists), then rerun your program.