Memory leak while calling .net method

The Help appears to be wrong. C allows VarArgs a variable number of parameters so they must be on the stack Right to Left since the Right side can have a variable about. I see this is Bruce’s Driver code but cannot recall where.

Asking AI about if C and Pascal are the same responded:


No, the C calling convention (specifically cdecl) is not the same as the Pascal calling convention, although they are related. While both involve pushing arguments onto the stack, the key difference lies in who is responsible for cleaning up the stack after the function call and the order in which arguments are pushed. In cdecl (the C calling convention), the caller is responsible for cleaning the stack, while in Pascal, the callee cleans the stack. Additionally, cdecl pushes arguments onto the stack from right to left, whereas Pascal pushes them from left to right.

Here’s a more detailed breakdown:

  • C calling convention (cdecl):

    • The caller pushes arguments onto the stack from right to left.
    • The caller is responsible for cleaning up the stack after the function returns (removing the arguments).
    • This convention allows for functions with a variable number of arguments (varargs).
  • Pascal calling convention:

    • The caller pushes arguments onto the stack from left to right.
    • The callee (the function being called) is responsible for cleaning up the stack before returning.
    • This convention does not support varargs.
  • Related Conventions:

    • stdcall is another calling convention, similar to Pascal, but arguments are pushed from right to left (like cdecl).
    • fastcall is yet another convention where some arguments are passed in registers and others on the stack.

In essence, while both cdecl and Pascal are calling conventions that involve pushing arguments onto the stack, the crucial distinction is who manages the stack after the call, and the order in which arguments are pushed.

Clarion help in IDE.You can search for pascal in help

Finally found the root cause of memory leak..It has nothing to do with .net method.I just disabled it.It is only bcoz of BSTRING variables(lcData,sKey) which I used in clarion method..It seems like memory assigned for BString variables are not released even though I called clear()..During performance testing, I found that because of those two variable memory consumption shoots upto 426 Mb.

Thanks for the detail explanation.

Thanks Carlo…I will definitely try in test application

Here’s the famous quote

Try it without the CLEAR.

The RTL should automatically dispose the BStrings.

Also be sure and try with first 2 passed as *BSTRING.

A BStr type is a bit complicated. It is a LONG that is an address to an 8 byte Group with a Pointer to the actual Wide String and its Capacity. CLEAR should change the Wide String to <0,0> but maybe it’s setting the pointer or group address to zero which would leak

I tried both now. Leak still exists

What happens if you do bstringVar = ‘’ instead of CLEAR(bstringVar )?

Calling convention discrepancies aside, have you tried calling your DLL from another language other than Clarion?

Maybe it would be good to rule out whether the memory leak is a different issue than by calling it from Clarion alone.

I tried now..Still same problem

Real problem is with usage of Bstring in clarion code. Yesterday I found that there is no issue with .net dll

That’s odd. CLEAR() calls SysFreeString api, blank string assignment doesn’t.

Ok..I will once again check and confirm

That’s the way BStr work. They do not shrink when assigned a value shorter than the Capacity. Assigning Blank just writes <0,0> to the first 2 bytes.

If you want them to Shrink then CLEAR before an assignment.