0

I have a C++ dll that I am calling from C# code. the dll takes in couple params and returns an int..

C++ code.

extern "C" __declspec(dllexport) int DoSomething(char* input1, char* buffer)
{
    gss_buffer_desc token;
    std::string encodedTokenStr = base64_encode((unsigned char *)token.value, token.length).c_str();

    std::copy(encodedTokenStr.begin(), encodedTokenStr.end(), buffer);
    return value;
}

C#

public sealed class MyClass
{    
     public int DoSomething(string input1, out StringBuilder buffer)
     {
         buffer = new StringBuilder(10000);
         return DoSomething(input1, buffer)
     }

    [DllImport("mycppcode.dll")]
    private static extern int DoSomething(string input1, StringBuilder buffer)
}

sometimes I see that a lot of memory is being used by this application and my first thought was about memory leaks. Does garbage collector takes care of all the objects that are initialized in the C++ code? does C++ code initialize some memory for the string builder ("buffer") even if it is initialized in C#. I cannot dispose this in the C++ because i need to collect the data from the string builder.

I have never worked on C++ but I see that few objects that were declared in C++ dll are being cleared.

I might be doing something wrong in the way that I am calling the C++ code. can this string builder cause memory leaks??

Rasm
  • 23
  • 5
  • What is `encodedTokenStr`? – The Vivandiere Apr 27 '15 at 18:31
  • it does some operations and the final encoded value is stored in that string which is then copied to the string "buffer". I added that line coz buffer is an out variable in the C# method. – Rasm Apr 27 '15 at 18:34
  • I meant define it in code, specifically, I wanna see the type, and prior memory allocations – The Vivandiere Apr 27 '15 at 18:34
  • it is declared like this std::string encodedTokenStr = some string – Rasm Apr 27 '15 at 18:37
  • Please add all the code. What is `some string`? – The Vivandiere Apr 27 '15 at 18:40
  • gss_buffer_desc token; std::string encodedTokenStr = base64_encode((unsigned char *)token.value, token.length).c_str(); I am not sure about the bae64_encode method. I dont have access to it. – Rasm Apr 27 '15 at 18:41
  • @Rasm please don't put code in comments - that's hard to read. Edit your post instead. Also, why do you think the leak comes from interop code and not from your `//Do Something` code? We may be able to spot it if you post the full method code. – Lucas Trzesniewski Apr 27 '15 at 18:44
  • Sorry about that. I have added the code in the post. I dont have access to all the code. – Rasm Apr 27 '15 at 18:50

1 Answers1

0

C# doesn't free any memory allocated by C++ (with some small exceptions if you use COM allocators/BSTR).

In your specific case

I don't see any problem in your case. There is no memory allocation in C++ code, and the StringBuilder is "built" C#-side, and being 10000 characters big, I do hope it is enough (note that normally you would pass to C++ the size of the buffer, or you would first ask to another C++ method the size of the needed buffer).

On a tangential note, I would suggest using wchar_t in C++, to maintain full compatibility with C# strings/chars/...

xanatos
  • 109,618
  • 12
  • 197
  • 280
  • Thanks for the answer. my major concern was regarding the string builder. the cpu usage spikes and the app almost uses 10gb. – Rasm Apr 27 '15 at 18:41
  • @Rasm the fact that 10 lines of C#+C++ don't leak don't mean that the other 1000 lines don't leak :-) – xanatos Apr 27 '15 at 19:01
  • yes, I understand that. I just wanted to check if im handling my code right. the C++ was developed by some third party. we will have to request to review and edit that code. which most probably will be my next task, but before doing that I just wanted to make sure that I am not doing anything wrong regarding how I invoke the dll. – Rasm Apr 27 '15 at 19:06