Finding the calling constructor object in C ++

Looking for a quick and dirty way to identify the calling constructor object (or any function, for that matter) I am writing macros to help identify memory leaks by resetting the pointers thisto OutputDebugString.

Knowing where ctor and dtor were called from will help identify the problem.

Th \ 0

+3
source share
12 answers

If you are using visual studio, you can attach a debugger and, instead of having a breakpoint, has a trace point. You do this by right-clicking the breakpoint and selecting When Hit.... Then select to print the message, including the stack trace. This message will be sent to the output panel, and you can analyze all calls in your free time.

hit-point http://lanzkron.googlepages.com/hit-point.jpg

+8
source

The best way I can come up with is to run your program in the debugger and set a breakpoint in the constructor. Then view the call stack.

, , . .


, , , , , , win32 API. , / , , std::vector<std::string>. ( RAII , push_back pop_back)

+6

, (OutputDebugString). , api StackWalk64 stacktrace. . ++ (MSVC).

(BoundsChecker ..).

+3

, ++ . , valgrind , . RAII ( ).

+2

gcc? ?

+1

Linux, Valgrind , , . ++.

+1
0

ctor dtor? ++, , , , , , .

0

, / , /.

. Win32; ?

.

0

, , , . , , .

, ? , ( ), . std Win32, , , , (.. - ). , , , , , . , , ... , , eip , , , - ():

call label
label:
pop eax
mov [address of next array entry], eax
0

g++, . , , gcov.

, , .

, , , , . . , lcov, !

0

Thank you all for your feedback. setting a breakpoint in ctor is not an option due to hundreds of calls to new objects even during the short program life cycle.

Tracing macros in ctor and dtor did the trick.

Visual leak detector and Stackwalk64 look very promising

also found AfxDumpStack (AFX_STACK_DUMP_TARGET_ODS); // OutputDebugString
but it is VERY noisy

0
source

Source: https://habr.com/ru/post/1702686/


All Articles