Skip to content
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

FolderNameEditor leaks buffer on cancel #768

Closed
hughbe opened this Issue Apr 13, 2019 · 0 comments

Comments

Projects
None yet
1 participant
@hughbe
Copy link
Contributor

hughbe commented Apr 13, 2019

Here https://github.com/dotnet/winforms/blob/master/src/System.Windows.Forms.Design.Editors/src/System/Windows/Forms/Design/FolderNameEditor.cs#L140-L176 we allocate a buffer but only free it if SHGetPathFromIDList succeeds

// Construct a BROWSEINFO
UnsafeNativeMethods.BROWSEINFO bi = new UnsafeNativeMethods.BROWSEINFO();

IntPtr buffer = Marshal.AllocHGlobal(MAX_PATH);

bi.pidlRoot = pidlRoot;
bi.hwndOwner = hWndOwner;
bi.pszDisplayName = buffer;
bi.lpszTitle = descriptionText;
bi.ulFlags = mergedOptions;
bi.lpfn = IntPtr.Zero;
bi.lParam = IntPtr.Zero;
bi.iImage = 0;

// And show the dialog
pidlRet = UnsafeNativeMethods.Shell32.SHBrowseForFolder(bi);

if (pidlRet == IntPtr.Zero) return DialogResult.Cancel;

// Then retrieve the path from the IDList
UnsafeNativeMethods.Shell32.SHGetPathFromIDList(pidlRet, buffer);

// Convert to a string
DirectoryPath = Marshal.PtrToStringAuto(buffer);

// Then free all the stuff we've allocated or the SH API gave us
Marshal.FreeHGlobal(buffer);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.