Results 1 to 15 of 15

Thread: [ abandoned]Weird memory-leak when using a (unmanaged) DLL

  1. #1

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Angry [ abandoned]Weird memory-leak when using a (unmanaged) DLL

    Hi, I'm using a DLL written in c++ (unmanaged).
    Using this DLL I'm observing a massive memory-leak, which could be located to the unmanaged HeapMemory (using .Net Memory Profiler).

    After doing some tests I figured:
    Using the DLL from a console-application, which is just console-printing the information recieved via the CallBack-Functions the memory usage is constant.
    Changing it to an WindowForm application and just registering empty CallBack functions the memory usage is constant.

    When using the CallBack-functions to create/update objects the meory-leak is present.

    Does anybody have an idea/solution on such a problem.
    Last edited by opus; Sep 12th, 2013 at 01:55 AM.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Weird memory-leak when using a (unmanaged) DLL

    Quote Originally Posted by opus View Post
    When using the CallBack-functions to create/update objects the meory-leak is present.

    Does anybody have an idea/solution on such a problem.
    Can you show this version of the code??

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Weird memory-leak when using a (unmanaged) DLL

    In a module
    Code:
    <UnmangedFunctionPointer(CallingConvention.Cdecl)> Public Delegate DelEntityCallback (ByVal Site As Integer, Byval Application As Integer, ByVal ID As Integer, ByVal Name As String, ByVal Entity_Change As enEntityChange, ByRef UpdateInfo As strucUpdateInfo)
    
    <DLLImport("DIS_LIB.DLL), Entrypoint:= "Set_Entity_Callback", setlasterror:=True, CharSet:= CharSet.ANSI, exactspelling:=True,  CallingConvention:=CallingConvention.CDecl)> _
    Public Sub Set_Entity_Callback (ByVal Active as Boolean, ByVal CallBackAddr as DelEntityCallback)
    End sub
    in the "Mainform"- class
    Code:
    Dim cb As DelEntityCallback
    Public MyEntities As List (of clEntities)
    
    Private Sub Mainform_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    ...
    cb= AddressOf  EntityUpdate
    ...
    End Sub
    
    
    Public Sub EntityUpdate (ByVal Site As Integer, Byval Application As Integer, ByVal ID As Integer, ByVal Name As String, ByVal Entity_Change As enEntityChange, ByRef UpdateInfo As strucUpdateInfo)
    Select Case Entity_Change 'Entity_Change is an Enum with 0= New, 1= Update, 2=Delete
       Case is = enEntityChange.New
          MyEntities.Add(New clEntity(Site,Application,ID,UpdateInfo.Position)
       Case Is = enEntityChange.Update
          Dim Index As Integer = MyEntites.FindIndex(Function(f As clEntity) f.ID = ID AndAlso f.Site = Site AndAlso f.Application = Application)
          If Not Index = -1 then
             With MyEntities(Index)
                 .Mutex.WaitOne()
                 .Position=UpdateInfo.Position
                 .Mutex.ReleaseMutex()
             End With
          End If     
       Case Is = enEntityChange.Delete
          Dim Index As Integer = MyEntites.FindIndex(Function(f As clEntity) f.ID = ID AndAlso f.Site = Site AndAlso f.Application = Application)
          If Not Index = -1 then
              MyEntities(Index).Dispose()
              MyEntities(Index)= Nothing
              MyEntities.Remove(MyEntities(Index))
          End If 
    End Select    
    End Sub
    Last edited by opus; Aug 27th, 2013 at 10:20 AM.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  4. #4

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Weird memory-leak when using a (unmanaged) DLL

    Forgot one needed line:

    Code:
    Private Sub mnuStartStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuStartStop.Click
    ..
    ..
    Set_Entity_Callback(True, cb)
    End Sub
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  5. #5
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Weird memory-leak when using a (unmanaged) DLL

    A .Net Mutex wraps the OS Mutex which is an unmanaged resource. If that call back is being invoked for many additions then it could be all the Mutexes that are being created for all the new clEntity objects being instantiated. Comment out the code in the Select Case that creates new clEntity objects and see if you're still getting memory leaks. Or if you have the source for the clEntity class, comment out the code that creates the Mutex. We need to eliminate or confirm the Mutexes as the cause.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  6. #6

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Weird memory-leak when using a (unmanaged) DLL

    Thanks for the input.
    First trials seems to show the it isn't the Mutex.
    I'll continue to search.

    Found one mistake in my initial posting, the window application shows a stable memory usage if the .DLL isn't set to an "active state". In that case the Callbacks are not raised. When "active" the .DLL is reading and processing UDP-messages from "somewhere".
    Last edited by opus; Aug 29th, 2013 at 06:24 AM.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  7. #7

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Weird memory-leak when using a (unmanaged) DLL

    I have done more testing and now I'm lost.

    The situation:
    The .DLL
    When "active" the .DLL is reading,sending and processing UDP-messages holding Entity-Information from "somewhere". Entity-Information coming from "somwhere" is sent via CallBacks to my connected application as well as it is recieving Entity Information from my Entites.

    My Application is recieving those Remote Entities, is creating own Objects on them or Updating existing ones if they match. Using those Objects a simulation for a number of different sensors is done.

    When using the .DLL with my Application I observe an extreme memory leak.

    When using the DLL with all the Object-Handing in a Console-Applcation, No Memory Leak. (BTW: using the Mutex as posted above)

    When using my Application without the DLL, creating the same number of "Remote Entites", calling the same Functions to update those Entities that were used by the DLL-CallBacks, No Memory Leak.

    Where could theoretically be the problem since I covered the use of the unmanaged DLL with a managed ConsoleApplication and I covered the handling and display of the number of Objects (always 1000) in the Forms-Application?
    Should I try to "insert" the Forms into the ConsoleApplication (is that possible at all)?
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  8. #8
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Weird memory-leak when using a (unmanaged) DLL

    Do you have the code for this unmanaged DLL by any chance ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  9. #9

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Weird memory-leak when using a (unmanaged) DLL

    Yes, I have access to it.
    What should I look for?
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  10. #10
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Weird memory-leak when using a (unmanaged) DLL

    Well, I mean you can look at what it does for any clues. Do you know C/C++ ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  11. #11

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Weird memory-leak when using a (unmanaged) DLL

    Do you know C/C++ ?
    Not enough in order to find a mistake in the code. Again, since the use of the DLL in a Console application is showing no memory leak, I'd believe to problem isn't the DLL.
    Knowing that a Console Application doesn't show the leak I did put all my code (including the Forms) into such an application and using a "Main" just do .Show my MainForm and keep the console running until this MainForm is closed. Guess what, the isn't gone, however the mmeory increase (for example of Private Bytes) is increasing in 10 minutes "only" by 50% intead of 300%.
    What is the difference between an WinForm and a Console Application?
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  12. #12
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Weird memory-leak when using a (unmanaged) DLL

    Quote Originally Posted by opus View Post
    What is the difference between an WinForm and a Console Application?
    Well there are a lot of pluming differences but one of the main differences is that a WinForms app has a message loop and a console app does not. There is also significantly less plumbing in a console app.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  13. #13

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Weird memory-leak when using a (unmanaged) DLL

    Sorry for late reply.
    I think I'm giving up on this one.
    Using bthe .DLL in a Window Application I get the MemoryLeak even if I only Declare two Methods that do take just a Bool. What mistake could be there?
    The workaround using a Console Application that opens my Forms did show a smaller leak, however it was still a leak. The application is supposed to be running for houres, not knowing how much "traffic" is observed.

    I'll probably try another way to connect to this "traffic".

    Thanks for all the help.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  14. #14
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: [ abandoned]Weird memory-leak when using a (unmanaged) DLL

    Sorry to hear that. I wish I could have been of more help to you. But I will say this, there's always the risk that using unmanaged code will result in memory leaks.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  15. #15

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: [ abandoned]Weird memory-leak when using a (unmanaged) DLL

    Don't blame yourself. It is as it is, looking back to the beginning of my project (I joined VBF back then), I wouldn't have dremt of such a stage.
    today I have succesfully managed to incooperated a (commercial) interface for a "Tactical Data Link". I learned a lot about implementing a DLL doing that.
    The present problem (incooperate the interface to DIS [Distributed Interactive Simulation]) has other possible ways to go along, I'll have a closer look into interfaces that would allow to connect via DIS or HLA(High Level Architecture).

    So be assured I'll have more questions to come.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

Posting Permissions

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



Click Here to Expand Forum to Full Width