# Memory leak while calling .net method

**URL:** <https://clarionhub.com/t/memory-leak-while-calling-net-method/8409>\
**Category:** questions\
**Tags:** interop, memory, dotnet\
**Created:** [July 23, 2025, 2:08pm UTC](https://clarionhub.com/t/memory-leak-while-calling-net-method/8409 "2025-07-23T14:08:11Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![raghu](https://clarionhub.com/letter_avatar_proxy/v4/letter/r/b2d939/32.png) [@raghu](https://clarionhub.com/u/raghu)\
**Post date:** [July 23, 2025, 2:08pm UTC](https://clarionhub.com/t/memory-leak-while-calling-net-method/8409/1 "2025-07-23T14:08:11Z")

</div>

Hello everyone,

I got memory leak issue when I call .net method from clarion application. I have used libmaker to convert .net DLL to lib file and used in clarion project. I am passing BSTRING parameters to the .net method and receives BSTRING result in reference variable. Check the below code

Clarion side:

```clarion
MAP
    MODULE('NetApp')  
      ApiValidate (BSTRING pData, BSTRING pSoftwareKey, *BSTRING pResult), LONG, PASCAL, NAME('Validate')
    END
  END   

ApiClass.Run Procedure()
lcData BSTRING
sKey BSTRING
result BSTRING
retval long

CODE

lcData= GETREG(REG_LOCAL_MACHINE, RegKey ,'Data') //retrieve some data from registry
sKey =’test data’
retval= ApiValidate (lcData, sKey , result)
clear(lcData)
clear(sKey)
clear(result)

```

* * *

.Net side

```csharp
namespace MLNetToolbox
{
    public enum MLError
    {      
        ML_OK = 0
    }
    public sealed class WinAPI
    {

      [DllExport("Validate", CallingConvention = CallingConvention.StdCall)]
    public static MLError Validate ([MarshalAs(UnmanagedType.BStr)] string data, [MarshalAs(UnmanagedType.BStr)] string softwareKey, [MarshalAs(UnmanagedType.BStr)] out string result)
   {
     result =”test”;               
     return MLError.ML_OK;
   }
 }
}

```

We are creating web application where on each request the application will call the above clarion method. While running the performance test, we have noticed significant increase in memory consumption. It shoots up to 500 mb. I used .Net framework 4.8 to build .net dll. Couldn’t figure out the potential cause of memory leak. It would be great if someone can help me on this.

---

<div class="post-metadata">

**Author:** ![Bruce](https://clarionhub.com/letter_avatar_proxy/v4/letter/b/ccd318/32.png) [@Bruce](https://clarionhub.com/u/Bruce)\
**Post date:** [July 23, 2025, 3:00pm UTC](https://clarionhub.com/t/memory-leak-while-calling-net-method/8409/2 "2025-07-23T15:00:22Z")

</div>

It goes upto 500mb, does it keep going up beyond that?

---

<div class="post-metadata">

**Author:** ![raghu](https://clarionhub.com/letter_avatar_proxy/v4/letter/r/b2d939/32.png) [@raghu](https://clarionhub.com/u/raghu)\
**Post date:** [July 23, 2025, 3:19pm UTC](https://clarionhub.com/t/memory-leak-while-calling-net-method/8409/3 "2025-07-23T15:19:15Z")

</div>

Sorry Bruce I forgot to mention one thing. Actually the method which I mentioned in the post consumes around 274 MB. But if I call other .Net methods then certainly it can go beyond 500 MB

---

<div class="post-metadata">

**Author:** ![CarlBarnes](https://clarionhub.com/user_avatar/clarionhub.com/carlbarnes/32/1859_2.png) [@CarlBarnes](https://clarionhub.com/u/CarlBarnes)\
**Post date:** [July 23, 2025, 3:49pm UTC](https://clarionhub.com/t/memory-leak-while-calling-net-method/8409/4 "2025-07-23T15:49:58Z")

</div>

Sys Internals VMMap can help see memory use and track leaks. Are you saying after 1 call your memory goes up 274 MB or is it many calls.

> **[VMMap - Sysinternals](https://learn.microsoft.com/en-us/sysinternals/downloads/vmmap)**
>
> VMMap is a process virtual and physical memory analysis utility.

---

<div class="post-metadata">

**Author:** ![jslarve](https://clarionhub.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@jslarve](https://clarionhub.com/u/jslarve)\
**Post date:** [July 23, 2025, 3:58pm UTC](https://clarionhub.com/t/memory-leak-while-calling-net-method/8409/5 "2025-07-23T15:58:35Z")

</div>

I’m no .net expert, but don’t StdCall and Pascal have different porpoises?

---

<div class="post-metadata">

**Author:** ![jslarve](https://clarionhub.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@jslarve](https://clarionhub.com/u/jslarve)\
**Post date:** [July 23, 2025, 4:16pm UTC](https://clarionhub.com/t/memory-leak-while-calling-net-method/8409/6 "2025-07-23T16:16:54Z")

</div>

OIC, there’s no pascal in .net. (blush)

---

<div class="post-metadata">

**Author:** ![raghu](https://clarionhub.com/letter_avatar_proxy/v4/letter/r/b2d939/32.png) [@raghu](https://clarionhub.com/u/raghu)\
**Post date:** [July 23, 2025, 4:20pm UTC](https://clarionhub.com/t/memory-leak-while-calling-net-method/8409/7 "2025-07-23T16:20:55Z")

</div>

No Carl. After running around 2365 times memory goes upto 274 MB. Its like sending 2365 simultaneous request. I will look into VMMap

---

<div class="post-metadata">

**Author:** ![raghu](https://clarionhub.com/letter_avatar_proxy/v4/letter/r/b2d939/32.png) [@raghu](https://clarionhub.com/u/raghu)\
**Post date:** [July 23, 2025, 4:33pm UTC](https://clarionhub.com/t/memory-leak-while-calling-net-method/8409/8 "2025-07-23T16:33:25Z")

</div>

Yes. There is difference in the calling convention. Not sure whether that could be the reason for the leak. Need to investigate more on this

---

<div class="post-metadata">

**Author:** ![CarlBarnes](https://clarionhub.com/user_avatar/clarionhub.com/carlbarnes/32/1859_2.png) [@CarlBarnes](https://clarionhub.com/u/CarlBarnes)\
**Post date:** [July 23, 2025, 7:51pm UTC](https://clarionhub.com/t/memory-leak-while-calling-net-method/8409/9 "2025-07-23T19:51:44Z")

</div>

You’ll want to change your .Net to `CallingConvention = CallingConvention.Pascal` since Clarion is prototyped as PASCAL.

It seems like your code would not work because the parameters are in a different order i.e. Pascal pushed Left to Right versus Right to Left for StdCall.

---

<div class="post-metadata">

**Author:** ![CarlBarnes](https://clarionhub.com/user_avatar/clarionhub.com/carlbarnes/32/1859_2.png) [@CarlBarnes](https://clarionhub.com/u/CarlBarnes)\
**Post date:** [July 23, 2025, 8:01pm UTC](https://clarionhub.com/t/memory-leak-while-calling-net-method/8409/10 "2025-07-23T20:01:53Z")

</div>

> [@CarlBarnes](#):
>
> CallingConvention.Pascal

There’s this .Net DllExport tool that defaults to the Cdecl Convention which Clarion can specific “C”

> **[GitHub - 3F/DllExport: .NET DllExport with .NET Core support (aka...](https://github.com/3F/DllExport)**
>
> .NET DllExport with .NET Core support (aka 3F/DllExport aka DllExport.bat)

---

<div class="post-metadata">

**Author:** ![raghu](https://clarionhub.com/letter_avatar_proxy/v4/letter/r/b2d939/32.png) [@raghu](https://clarionhub.com/u/raghu)\
**Post date:** [July 24, 2025, 7:09am UTC](https://clarionhub.com/t/memory-leak-while-calling-net-method/8409/11 "2025-07-24T07:09:45Z")

</div>

I just got confused with explanation of pascal attribute in clarion help. There it says “In 32-bit programs, both C and PASCAL conventions pass the parameters to the stack from right to left.”

---

<div class="post-metadata">

**Author:** ![raghu](https://clarionhub.com/letter_avatar_proxy/v4/letter/r/b2d939/32.png) [@raghu](https://clarionhub.com/u/raghu)\
**Post date:** [July 24, 2025, 7:25am UTC](https://clarionhub.com/t/memory-leak-while-calling-net-method/8409/12 "2025-07-24T07:25:27Z")

</div>

I have changed the calling convention in both .net and clarion. But leak didn’t disappear

---

<div class="post-metadata">

**Author:** ![jkuijt](https://clarionhub.com/user_avatar/clarionhub.com/jkuijt/32/6168_2.png) [@jkuijt](https://clarionhub.com/u/jkuijt)\
**Post date:** [July 24, 2025, 8:05am UTC](https://clarionhub.com/t/memory-leak-while-calling-net-method/8409/13 "2025-07-24T08:05:53Z")

</div>

Perhaps your problem has to do with the memory leak in C11.1?

See:  
[https://clarionhub.com/t/alternative-debug-rtl-that-checks-the-heap-for-memory-leaks/6287](https://clarionhub.com/t/alternative-debug-rtl-that-checks-the-heap-for-memory-leaks/6287)

Best regards  
Jeffrey

---

<div class="post-metadata">

**Author:** ![raghu](https://clarionhub.com/letter_avatar_proxy/v4/letter/r/b2d939/32.png) [@raghu](https://clarionhub.com/u/raghu)\
**Post date:** [July 24, 2025, 8:51am UTC](https://clarionhub.com/t/memory-leak-while-calling-net-method/8409/14 "2025-07-24T08:51:01Z")

</div>

May be..But I couldn’t access the link which you shared

---

<div class="post-metadata">

**Author:** ![jkuijt](https://clarionhub.com/user_avatar/clarionhub.com/jkuijt/32/6168_2.png) [@jkuijt](https://clarionhub.com/u/jkuijt)\
**Post date:** [July 24, 2025, 9:28am UTC](https://clarionhub.com/t/memory-leak-while-calling-net-method/8409/15 "2025-07-24T09:28:23Z")

</div>

Hmm, that’s weird. I have attached a pdf of the conversation here:

> **[STACK](https://jefkuijt.stackstorage.com/s/TjOT2qtosvWsitDi)**

---

<div class="post-metadata">

**Author:** ![raghu](https://clarionhub.com/letter_avatar_proxy/v4/letter/r/b2d939/32.png) [@raghu](https://clarionhub.com/u/raghu)\
**Post date:** [July 24, 2025, 10:29am UTC](https://clarionhub.com/t/memory-leak-while-calling-net-method/8409/16 "2025-07-24T10:29:30Z")

</div>

Thanks Jkuit..I will check

---

<div class="post-metadata">

**Author:** ![raghu](https://clarionhub.com/letter_avatar_proxy/v4/letter/r/b2d939/32.png) [@raghu](https://clarionhub.com/u/raghu)\
**Post date:** [July 24, 2025, 1:49pm UTC](https://clarionhub.com/t/memory-leak-while-calling-net-method/8409/17 "2025-07-24T13:49:43Z")

</div>

Might be. Because I am using Clarion 11.1.13815

---

<div class="post-metadata">

**Author:** ![CarlBarnes](https://clarionhub.com/user_avatar/clarionhub.com/carlbarnes/32/1859_2.png) [@CarlBarnes](https://clarionhub.com/u/CarlBarnes)\
**Post date:** [July 24, 2025, 3:38pm UTC](https://clarionhub.com/t/memory-leak-while-calling-net-method/8409/18 "2025-07-24T15:38:40Z")

</div>

> [@raghu](#):
>
> `ApiValidate (BSTRING pData, BSTRING pSoftwareKey, *BSTRING pResult), LONG`

You might try passing all 3 BSTRING by address as \*BSTRING

Passing by Value the RTL would have to duplicate them and maybe it’s not disposing. It also will be faster.

AFAIK BString support in Clarion is not complete. Like you cannot put them in a Group or Queue it will crash or leak. It might not really be passing them by value.

---

<div class="post-metadata">

**Author:** ![CarlosGtrz](https://clarionhub.com/user_avatar/clarionhub.com/carlosgtrz/32/5870_2.png) [@CarlosGtrz](https://clarionhub.com/u/CarlosGtrz)\
**Post date:** [July 24, 2025, 6:03pm UTC](https://clarionhub.com/t/memory-leak-while-calling-net-method/8409/19 "2025-07-24T18:03:19Z")

</div>

If you have the time, you can try to rewrite the Clarion/.NET parameters exchange using `HeapAlloc/HeapFree`.

For example, to return a string, the .NET side allocates heap memory, sets its value as an ANSI C string, and returns the heap address.

The Clarion side can read the string using `st.SetValueByCstringAddress(addr)` and then release the memory with `HeapFree(GetProcessHeap(),0,addr)`. Of course, it will be better to write a front-end class.

I did this recently and it worked great. I used Claude Code to write the .NET side. I asked it to create a native AOT 32-bit DLL (requires .NET 9) to be called from C programs.

---

<div class="post-metadata">

**Author:** ![Bruce](https://clarionhub.com/letter_avatar_proxy/v4/letter/b/ccd318/32.png) [@Bruce](https://clarionhub.com/u/Bruce)\
**Post date:** [July 24, 2025, 6:29pm UTC](https://clarionhub.com/t/memory-leak-while-calling-net-method/8409/20 "2025-07-24T18:29:00Z")

</div>

Eh? Where does it say Pascal and C parameter passing are the same?

[Next page](https://clarionhub.com/t/memory-leak-while-calling-net-method/8409.md?page=2)
