Skip to content

crtdbg: fix MSVC CRTDBG memory leak reporting #1460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

jeffhostetler
Copy link

@jeffhostetler jeffhostetler commented Jan 25, 2018

Teach git to report memory leaks both to a file and to the
VS debug output window.

From the SDK command line, build Git as follows:

make MSVC=1 DEBUG=1 CRTDBG=1

When built this way, leaks will always be reported to the
Visual Studio debug output window.

Enable GIT_TRACE_CRTDBG and the leaks will also be reported
to the trace target. This works from both the command line
and from Visual Studio.

Signed-off-by: Jeff Hostetler [email protected]

@jeffhostetler
Copy link
Author

@dscho This is self-contained and Windows-specific. Let me know if you'd rather I send this upstream first. I wasn't sure about how much of the low level windows stuff is upstream.

Teach git to report memory leaks both to a file and to the
VS debug output window.

From the SDK command line, build Git as follows:

	make MSVC=1 DEBUG=1 CRTDBG=1

When built this way, leaks will always be reported to the
Visual Studio debug output window.

Enable GIT_TRACE_CRTDBG and the leaks will also be reported
to the trace target.  This works from both the command line
and from Visual Studio.

Signed-off-by: Jeff Hostetler <[email protected]>
@jeffhostetler
Copy link
Author

The last 2 commits are probably duplicate too much code. These changes let us get the file/line of the
actual call to x*alloc() rather than the file/line of the code within wrapper.c that calls the actual CRT
alloc routine. Without these all leaks are attributed to the body of xcalloc/xmalloc/xrealloc themselves
and is not that helpful. To be useful, we need to capture the data for the caller of xcalloc and friends.

I only converted the most common routines. I didn't really like how I had to do this (lots of duplicated
code), but we can talk about alternatives if we want to pursue this patch.

Modify xcalloc(), xmalloc(), and xrealloc() macros to pass
the file/line of the original caller to CRTDBG routines
rather than the file/line of the x* routine itself.

Signed-off-by: Jeff Hostetler <[email protected]>
@jeffhostetler
Copy link
Author

Here is a version that tries to eliminate the duplicated code blocks and make minimal changes in upstream code sections. It might be possible to make this even skinnier, but I think this is pretty close.

#undef malloc
#undef realloc

#define strdup(s) (_strdup_dbg((s), _NORMAL_BLOCK, caller_fn, caller_ln))

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

@jeffhostetler
Copy link
Author

BTW, it's OK if you just want to say "no" on this PR. I understand. Memory leaks aren't taken that serious, since exit() works just fine for most uses.

@dscho
Copy link
Member

dscho commented Feb 20, 2018

BTW, it's OK if you just want to say "no" on this PR

Thanks. I think I'd like the changes, though... I just need to understand the intricacies, and I need to come up with a way that would be acceptable by core Git. The less platform-specific stuff we can introduce outside git-compat-util.h/compat/*, the better.

return crtdbg_xmalloc(size, "kwset.c", -1);
}
#endif

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

the-j0k3r added a commit to StylishThemes/GitHub-Dark that referenced this pull request Jun 13, 2019
@git-for-windows-ci git-for-windows-ci changed the base branch from master to main June 17, 2020 18:11
@dscho
Copy link
Member

dscho commented Oct 6, 2020

@jeffhostetler I'll close this for now, okay?

If we need to resurrect it, we will probably have to redo it, what with pcre2_malloc(), xdl_malloc() and similar instances where we wrap malloc().

@dscho dscho closed this Oct 6, 2020
@jeffhostetler
Copy link
Author

that's fine. and yes, with all the changes to the build system since then, we'd need to retool it anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants