Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6154509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:16:30+00:00 2026-05-23T20:16:30+00:00

I have a chunk of code that I’m using to get the UNC path

  • 0

I have a chunk of code that I’m using to get the UNC path of a mapped drive in a CLR DLL, but when I’m freeing memory at the end, a char array causes an invalid heap pointer assertion failure, and I’m assuming it has to do with it being allocated by InteropServices, but I want to make sure it doesn’t turn into a memory leak as this function gets called repeatedly.

Code:

DWORD MAX_DEVICE_LENGTH = 1000;
TCHAR* szDeviceName = new TCHAR[MAX_DEVICE_LENGTH];
memset(szDeviceName, '\0', MAX_DEVICE_LENGTH); 
DWORD dwResult; 


char* charpath = (char*)   (void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(path->Substring(0,2));
wchar_t* tpath = new wchar_t[MAX_DEVICE_LENGTH];

memset(tpath, '\0', MAX_DEVICE_LENGTH);

DWORD dwNum = MultiByteToWideChar (CP_ACP, 0, charpath, -1, NULL, 0);
MultiByteToWideChar (CP_ACP, 0, charpath, -1, tpath, dwNum );


dwResult = WNetGetConnection(
    tpath,
    szDeviceName, &MAX_DEVICE_LENGTH); 

System::String ^ str = gcnew System::String(szDeviceName);

str += path->Substring(2, path->Length-2);

delete(szDeviceName);
free(charpath); //This is where it assert-fails
delete(tpath);

return str;

It’s probably something basic about memory de-allocation that I don’t understand, but either way it’s worth figuring out. If it helps, if I skip over that line tpath deletes fine, but if the charpath assertion fails then tpath will fail as well.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T20:16:32+00:00Added an answer on May 23, 2026 at 8:16 pm

    The relevant comment on MSDN is

    StringToHGlobalAnsi is useful for custom marshaling or when mixing
    managed and unmanaged code. Because this method allocates the
    unmanaged memory required for a string, always free the memory by
    calling FreeHGlobal. StringToHGlobalAnsi provides the opposite
    functionality of Marshal.PtrToStringAnsi.

    So, no delete/free but FreeHGlobal.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a chunk of code where it appears that a variable is changing
I have a very simple chunk of code that is designed to simulate keyboard
I have a decent sized chunk of .NET code that now needs to be
I have a chunk of code that on page load with populates some of
I have a large chunk of legacy php code that I need to interface
I have a the following chunk of code that's adding up to six Player
Imagine I have a chunk of initialisation code at the top of a stored
Let's say you have the following chunk of code: <div id=container> <someelement>This is any
I'm looking for a better way to this. I have a chunk of code
I have this code snippets that generates a signature for POSTs. The detail of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.