Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What are you trying to do exactly? Locals are on stack and arguments are too depending on the calling convention. Storing or returning the address of a local or argument is not good because it will get overriden. There is no way to prevent them from being overriden besides like not calling methods.</p> <p>If you turn on unmanaged debugging you can use the memory debugger and register window to see what is going on. </p> <p>Here is simpler to understand C example. Why does print not display the correct value. Because when the print function gets invoked its stack frame overwrites the value.</p> <pre><code>int* bad(int x, int y) { int sum = x + y; return &amp;sum; }; int* bad2(int x, int y) { x += y; return &amp;x; } int _tmain(int argc, _TCHAR* argv[]) { int* sum1 = bad(10, 10); int* sum2 = bad(100, 100); printf("%d bad", *sum1); // prints 200 instead of 20 sum1 = bad2(10, 10); sum2 = bad2(100, 100); printf("%d bad", *sum1); // prints 200 instead of 20 return 0; }; </code></pre> <p>Cant get the clr to hold on for it longer. One thing you can do is get the variable on the stack pushed further out. Below is an example. This is all bad though :(</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Linq.Expressions; using System.Xml.Linq; using System.Runtime.InteropServices; namespace Bad { class Program { static void Main(string[] args) { Action a = () =&gt; Console.WriteLine("test"); Horror h = new Horror(); h.Fix(new Big(), ref a, new Big()); h.Clear(); Console.WriteLine(); } } [StructLayout(LayoutKind.Sequential, Size = 4096)] struct Big { } unsafe class Horror { void* _ptr; static void Handler() { Console.WriteLine("Hello world."); } public void Fix(Big big, ref Action action, Big big2) { action += Handler; var tr = __makeref(action); _ptr = (void*)&amp;tr; } public void Clear() { var tr = *(TypedReference*)_ptr; __refvalue(tr, Action) -= Handler; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload