.NET Runtime Error 80131506 - Passing Lambda to Native Function

.NET Runtime Error 80131506 - Passing Lambda to Native Function



So I'm getting this error that looks as though it's a corrupted garbage collection:



Application Crashes With "Internal Error In The .NET Runtime"



The full error is:



The process was terminated due to an internal error in the .NET Runtime at IP 71C571C8 (71B20000) with exit code 80131506.



It's running on:



Framework Version: v4.0.30319



This occurs inconsistently when running this function repeatedly:


public static int GetMdiTitledChildWindows(IntPtr parentWindow)

IntPtr mdiClient = FindWindowEx(parentWindow, IntPtr.Zero, MdiClient, "");
List<IntPtr> handles = new List<IntPtr>();
EnumChildWindows(mdiClient, (hwnd, param) =>

handles.Add(hwnd);
return true;
, IntPtr.Zero);
int counter = 0;
foreach (IntPtr handle in handles)

StringBuilder builder = new StringBuilder();
GetWindowText(handle, builder, GetWindowTextLength(handle)+1);
if (builder.Length > 0)

counter++;


return counter;



Where FindWindowEx(), EnumChildWindows() and GetWindowText() are all p/invoke signatures defined similarly to this:


FindWindowEx()


EnumChildWindows()


GetWindowText()


[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);



The error only seems to occur after I've run the method many times, however, this doesn't happen consistently. Sometimes it works, sometimes it doesn't.



Any suggestions on how to fix this?






Isn't this a duplicate of the question you've linked?

– OrangeDog
Sep 24 '18 at 11:05






@orangedog I don't believe so because as far as I can tell the issue in the question I've linked was caused by concurrent garbage collection not by passing a lamda

– Persistence
Sep 24 '18 at 11:07




1 Answer
1



So I solved my issue with the help of a generous benefactor on Discord.



The problem was that I was passing a Lamda to a p/invoke as a delegate:


Lamda


[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);



So every time the unmanaged WinAPI call called-back into my delegate, the GC had the opportunity to run, if it did, it collected my lamda causing this crash. This wouldn't necessarily happen, hence why my method worked most of the time and crashed inconsistently.


unmanaged


WinAPI


GC



The solution was to add a reference to the lamda that would prevent the GC from collecting it (although I went whole-hog and made it a local function because belt and braces):


public static int GetMdiTitledChildWindows(IntPtr parentWindow)

IntPtr mdiClient = FindWindowEx(parentWindow, IntPtr.Zero, MdiClient, "");
List<IntPtr> handles = new List<IntPtr>();
bool addToList(IntPtr hwnd, IntPtr param)

handles.Add(hwnd);
return true;

EnumWindowsProc gcHolder = addToList;
EnumChildWindows(mdiClient, gcHolder, IntPtr.Zero);
int counter = 0;
foreach (IntPtr handle in handles)

int textLength = GetWindowTextLength(handle) + 1;
StringBuilder builder = new StringBuilder(textLength);
GetWindowText(handle, builder, textLength);
if (builder.Length > 0)

counter++;


return counter;



The application now works as expected.






This code was not present in the question. As it stands, there is no way for anybody to answer the question as written. If you wish to persist with the question then you must edit it so that the defect can be seen in the question. If you do that we can close it as a duplicate. Personally I'd remove both answer and question because there is little value to future visitors.

– David Heffernan
Sep 17 '18 at 7:43






So there's little value in showing that that error can be caused by passing a lamda to a native function with a good explanation of why?

– Persistence
Sep 17 '18 at 16:55






We have a question and answer model here. The question needs to describe the problem. It's no good to describe the problem in an answer. That's back to front.

– David Heffernan
Sep 17 '18 at 17:24






The problem is the error... Explaining the cause of the error and the solution is pretty clearly useful information. I'll edit the question to have the extract of the problem code in the question rather than in the answer but I really don't see why it matters if its "back to front" as long as the information is there.

– Persistence
Sep 17 '18 at 17:36






This is just how we do things here. Question and answer. Explained by the close notice.

– David Heffernan
Sep 17 '18 at 18:16



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)