Make a piece of heap memory read-only
<p>If I have a database application in C++. When the data size is small, it's easy to debug for bugs.
But when the data size becomes huge, and relationship becomes complex, the bug is very hard to debug, or hard to find the root cause of it. Sometimes the callstack can't tell you enough information to find the root cause, because the memory has been collapsed. It's very easy the memory of one component is written by other component. Only when the first component visit that memory, we met the crash, but it's too late.</p>

<p>The difficulty to find the root cause is proportional to the size of the handling data.</p>

<p>I am wondering if there is any tricks to predicate the risk, and protection code for avoid illegal memory written.</p>

<p>C++ pointer is a powerful tool, but it's also the high risk to use it. Any code could use the pointer to write/update the memory anytime. When one code write a memory, no other class object know it unless the class visit that memory,then find the value is already invalid, crashed.</p>

<p>If A hold a pointer to B, then some code write that memory to make the value to C.
Then when A use the B pointer to do something, invalid pointer.</p>

<p>I want to know, maybe it's impossible, if there some way to avoid this situation. Could we add some protection code there.</p>

<p>One method I thought could be is I could make the memory to be writable before change, after change make it readable. So other code can't write it, otherwise crash. This would help me to find the issue earlier.</p>

<p>Is there some way to make a piece of heap memory read-only?</p>

<p>Platform is windows. I hope I could use some switch in the code to control it's access right.</p>
