How to clean up memory leak in unmanage code?-VBForums
Results 1 to 7 of 7

Thread: How to clean up memory leak in unmanage code?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    721

    Question How to clean up memory leak in unmanage code?

    Dear All,

    How to clean up memory leak in unmanage code?

    I have unmanage code as below:

    Code:
     //API declaration's
    [System.Runtime.InteropServices.DllImport("kernel32.dll", EntryPoint = "lstrcpyA")]
    private static extern int StringCchCopy(int lpstring1, int lpstring2);
    [System.Runtime.InteropServices.DllImport("kernel32.dll", EntryPoint = "lstrcpyA")]
    private static extern int StringCchCopy(byte[] lpstring1, int lpstring2);
    [System.Runtime.InteropServices.DllImport("kernel32.dll", EntryPoint = "lstrcpyA")]
    private static extern int StringCchCopy(string lpstring1, string lpstring2);
    [System.Runtime.InteropServices.DllImport("kernel32.dll", EntryPoint = "lstrlenA")]
    private static extern int lstrlen(int lpString);
    
    
    public string getStrFromPtr(int StrPointer)
    {
       string EndBuffer = "";
       byte[] StrBuffer;
       try
        {
    	int strLen;
    	strLen = lstrlen(StrPointer);
    	StrBuffer = new byte[strLen + 1];
    	if (strLen > 0)
    	{
    		StringCchCopy(StrBuffer, StrPointer);
    	}
    	for (int i = 0; i < strLen; i++)
    	{
    		EndBuffer += System.Convert.ToChar(StrBuffer[i]);
    	}
         }
    	catch (Exception ex) { SaveErrorLog("ClsMain.getStrFromPtr: " + ex.ToString()); }
    	finally { StrBuffer = null; }
    	return EndBuffer;
    }

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    4,616

    Re: How to clean up memory leak in unmanage code?

    You pass in an unmanaged string pointer to that procedure which means that procedure is not responsible for releasing that string. Do you have the code that creates that unmanaged string ? If so can you post it ?
    Last edited by Niya; Jan 20th, 2012 at 06:42 PM. Reason: Minor edit

  3. #3
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,772

    Re: How to clean up memory leak in unmanage code?

    I also dont see any IDisposible interface/pattern...

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  4. #4
    Junior Member
    Join Date
    Nov 2011
    Posts
    20

    Re: How to clean up memory leak in unmanage code?

    try deleaker or similar debugger...

  5. #5
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: How to clean up memory leak in unmanage code?

    Presumably you can use the Marshal class to clean up the unmanaged memory pointed to by "StrPointer". Either that or use PInvoke to get the "VirtualFree" api, then just call it and pass your pointer in with 0x8000 as the attributes.
    If I helped you out, please take the time to rate me

  6. #6
    Member
    Join Date
    Apr 2012
    Posts
    35

    Re: How to clean up memory leak in unmanage code?

    Also, you can always use programs for catching memory leaks. VLD is popular now.

  7. #7
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    677

    Re: How to clean up memory leak in unmanage code?

    The GC doesn't clean up any unmanaged resources, thus why implementing the IDisposable interface on a class object you have can be very useful for disposing of those objects manually.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    VB.NET Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.