Memory allocation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andy Baker

    Memory allocation

    I have recently written a .NET wrapper for a C++ DLL file and it seems to be
    working well, but I do have some concerns about memory. From my limited C
    programming experience (college 15+ years ago), I seem to remember that when
    using structures etc in C, you had to allocate memory to them then
    deallocate them when you had finished using them. My C# code creates a
    variable of type structure, passes it to the C++ DLL and then returns
    whatever data is necessary, without deallocating the structure reference. I
    tried setting it to null, but the compiler wouldn't accept that as it said
    it was a value type. Do I actually need to do anything or does the automatic
    garbage collection take care of it for me? One thing I have to avoid is a
    memory leak every time I make a call to the DLL. I am using VS 2005 and the
    compact framework (2.0). Thanks in advance.

    Andy Baker


  • Pavel Minaev

    #2
    Re: Memory allocation

    On Jul 21, 1:04 pm, "Andy Baker" <aba...@NOSPAMv anputer.comwrot e:
    I have recently written a .NET wrapper for a C++ DLL file and it seems tobe
    working well, but I do have some concerns about memory. From my limited C
    programming experience (college 15+ years ago), I seem to remember that when
    using structures etc in C, you had to allocate memory to them then
    deallocate them when you had finished using them. My C# code creates a
    variable of type structure, passes it to the C++ DLL and then returns
    whatever data is necessary, without deallocating the structure reference.I
    tried setting it to null, but the compiler wouldn't accept that as it said
    it was a value type. Do I actually need to do anything or does the automatic
    garbage collection take care of it for me? One thing I have to avoid is a
    memory leak every time I make a call to the DLL. I am using VS 2005 and the
    compact framework (2.0). Thanks in advance.
    First of all, C# structs are not allocated on the heap (except when a
    struct is a field of a class), but on the stack. You no more need to
    free that than you need to free a local variable of struct type in C/C+
    +. You may be confused by having to use the keyword "new" to
    initialize the variable, but in C#, it does not perform heap
    allocation when applied to value types - rather, it is equivalent to
    creating a stack-allocated temporary in C++.

    Anyway, if you pass a struct as an argument to an unmanaged function,
    you should not need to do any memory management. The only case where
    it may be necessary is when the unmanaged function allocates some
    memory, and returns pointer to it, assuming the caller will deallocate
    - then you'll need to handle this manually.

    Comment

    • Bob Powell [MVP]

      #3
      Re: Memory allocation

      If you're concerned that memory leaks may be a problem. For example if you
      see rising memory allocation in TaskManager while your program is running,
      Microsof Research have a new tool LeakDiak that will assist in finding them.

      I have recently used this to good effect.

      Here's a good blog article.



      --
      --
      Bob Powell [MVP]
      Visual C#, System.Drawing

      Ramuseco Limited .NET consulting


      Find great Windows Forms articles in Windows Forms Tips and Tricks


      Answer those GDI+ questions with the GDI+ FAQ


      All new articles provide code in C# and VB.NET.
      Subscribe to the RSS feeds provided and never miss a new article.


      "Andy Baker" <abaker@NOSPAMv anputer.comwrot e in message
      news:m-idnY92PbRzzRnVn Z2dneKdnZydnZ2d @plusnet...
      >I have recently written a .NET wrapper for a C++ DLL file and it seems to
      >be working well, but I do have some concerns about memory. From my limited
      >C programming experience (college 15+ years ago), I seem to remember that
      >when using structures etc in C, you had to allocate memory to them then
      >deallocate them when you had finished using them. My C# code creates a
      >variable of type structure, passes it to the C++ DLL and then returns
      >whatever data is necessary, without deallocating the structure reference. I
      >tried setting it to null, but the compiler wouldn't accept that as it said
      >it was a value type. Do I actually need to do anything or does the
      >automatic garbage collection take care of it for me? One thing I have to
      >avoid is a memory leak every time I make a call to the DLL. I am using VS
      >2005 and the compact framework (2.0). Thanks in advance.
      >
      Andy Baker
      >
      >

      Comment

      Working...