> For the complete documentation index, see [llms.txt](https://docs.bugsplat.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.bugsplat.com/introduction/getting-started/posting-a-test-crash/myconsolecrasher-c-plus-plus/address-sanitizer-reports.md).

# Address Sanitizer Reports

BugSplat integrates with the Windows Address Sanitizer (Asan) complier option allowing you to capture memory error reports and send them to your BugSplat database. Here are some instructions for modifying myConsoleCrasher (or your application) so that it captures Address Sanitizer exceptions.

## Modify Visual Studio Compiler Options

Enable the Address Sanitizer by choosing the Enable Address Sanitizer (/fsanitize=address) C/C++ General option:

![Visual Studio Enable Address Sanitizer](/files/-MgDI0B7Ol5GznfWeRa6)

Modify the Visual Studio Include Directories so that the Address Sanitizer header files can be located:

![](/files/-MgDKvHRDfEl9SEHMn5Q)

## Modify Initialization Code

To hook into the Address Sanitizer you will call **\_\_asan\_set\_error\_report\_callback** with the BugSplat **CreateAsanReport** method. To do this, we'll need a global instance of the BugSplat object and a new asanCallback function. See below for how you would modify our myConsoleCrasher sample program:

```cpp
...
#include "sanitizer/asan_interface.h"
...

// Global BugSplat initialization. 
BugSplat g_BugSplat(L"YourDatabase", L"YourAppName", L"YourAppVersion");

void asanCallback(const char* message)
{
   g_BugSplat.CreateAsanReport(message);
}

int wmain(int argc, wchar_t** argv)
{
	if (IsDebuggerPresent())
	{
		exit(0);
	}

	__asan_set_error_report_callback(asanCallback);
	
	...
}
```

## Create Memory Corruption Test

The only step left is to generate a memory corruption that Address Sanitizer will catch. Here's one way to do that:

```
void HeapCorruption() {
	void* pointerArray[20];
	struct simple_struct {
		double b;
		double c;
		char d;
	};

	{
		auto a = new simple_struct;
		auto b = new simple_struct;

		pointerArray[0] = a;
		pointerArray[1] = b;
	}

	// Function attempts to use pointer from array.
	// Encounters an error and deletes the pointer without removing it from array
	{
		auto item = reinterpret_cast<simple_struct*>(pointerArray[1]);
		delete item;
	}

	// Program continues, address is reused
	constexpr auto numInts = 40;
	auto intArray = reinterpret_cast<int*>(pointerArray[1]);
	for (int i = 0; i < numInts; i++) {
		intArray[i] = 10;
	}

	// Function called again, encounters the same error and tries to delete the same memory again
	{
		auto item = reinterpret_cast<simple_struct*>(pointerArray[1]);

		__try {
			delete item;
		}
		__except (EXCEPTION_EXECUTE_HANDLER) {
			wprintf(L"exception handling test successful.\n");

		}		
	}
```

Calling HeapCorruption (or your own memory error code) will generate the Address Sanitizer report and send a crash report to BugSplat.

## Crash Report Result

You'll see the crash report show up just like other crash exceptions. Here's what our modified version of the myConsoleCrasher sample program produced:

![](/files/-MgG6iGah6a52I3V6tug)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.bugsplat.com/introduction/getting-started/posting-a-test-crash/myconsolecrasher-c-plus-plus/address-sanitizer-reports.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
