shelinej Posted January 12, 2011 Share Posted January 12, 2011 I am trying to develop an HMI using VB.Net and have written a webservice that resides on the PPmac to call methods from the C library "libppmac.so" from a remote web application. What appears to be happening is that when I call a method (from a web app or directly from the service) the amount of memory used by the service increases. After a while the C library cannot initialize. Here is how I have written the calls: P/Invoke statements to call the C function from the library: Friend Declare Function _InitLibrary Lib "libppmac" Alias "InitLibrary" () As Integer Friend Declare Sub _CloseLibrary Lib "libppmac" Alias "CloseLibrary" () Friend Declare Ansi Function _GetResponse Lib "libppmac" Alias "GetResponse" (<[In](), MarshalAs(UnmanagedType.LPStr)> ByVal q As String, ByVal a As StringBuilder, ByVal maxlen As Int32, ByVal echomode As [Byte]) As Integer Web service method: _ Public Function GetResponse(ByVal command As String, ByVal echomode As [Byte]) As String Dim myresponse As New StringBuilder("A", 4096) Dim mystatus As Integer Dim initstatus initstatus = _InitLibrary() If initstatus = 0 Then mystatus = _GetResponse(command, myresponse, 255, echomode) Else Return "Library did not initalize" End If _CloseLibrary() If mystatus = 0 Then Return myresponse.ToString Else Return GetErrorStr(Math.Abs(mystatus)) End If End Function I ran one test where all I did was call InitLibrary() and CloseLibrary() and still had the same issue. I will admit that calling unmanaged code from managed code is new to me so I may just be missing something really simple but I can't figure out what it is. Could I not be closing the library properly? Thanks. Update: The same thing will happen if I use The attribute instead of the P/Invoke method above and each call consumes approx 16M of memory. Link to comment Share on other sites More sharing options...
bradp Posted January 12, 2011 Share Posted January 12, 2011 I have to have some other people help me with this who are not available right now. Hopefully tomorrow. Sorry for the delay. Link to comment Share on other sites More sharing options...
KEJR Posted January 13, 2011 Share Posted January 13, 2011 I'm running a C#.net program on that PPMAC using mono that queries variables every 100ms currently. I'm not experiencing the memory growth. I'm using DllImport though. While I don't know a solution to your problem exactly, let me make a few suggestions. I'm not 100% familiar with VB.net syntax, so some suggestions might not be spot on: - Don't initialize the PPMAC library from your method. While I can't say this is a problem, it is certainly inefficient to be calling those functions every time you want to run a Pmac method. I created a static "Pmac" class that gets initialized at program start and closed at program end using methods in that class. Then create wrappers so you can do "Pmac.GetVar()" and "Pmac.GetResponse()" and that kind of thing. - Check to see if the string that you return every gets cleaned up. I am not a .net or PPMAC expert, these are just my observations. KEJR Link to comment Share on other sites More sharing options...
bradp Posted January 14, 2011 Share Posted January 14, 2011 We have this example for C# http://forums.deltatau.com/showthread.php?tid=166&highlight=mono I have not tested it extensively and I have not heard any complaints. Perhaps looking at it will you figure out what is going wrong since we do not see anything obvious in your code. Link to comment Share on other sites More sharing options...
shelinej Posted January 17, 2011 Author Share Posted January 17, 2011 Yeah, that was where I started. Maybe Mono and xsp2 don't like the web service side of things. I'm looking into a telnet option too so maybe that will prove to be more fruitful. Thanks for digging into it. Link to comment Share on other sites More sharing options...
bradp Posted January 17, 2011 Share Posted January 17, 2011 The other thing to try is not to initialize and close the library each time you want to use a method. Initialize it once at the start of the application, and close it when the application closes. You can see if that helps. Maybe the act of repeatedly and quickly opening and closing is causing something to go wrong. Link to comment Share on other sites More sharing options...
shelinej Posted January 17, 2011 Author Share Posted January 17, 2011 I tried that as well and it actually uses more memory per function call than opening and closing the library for each call (approx 80MB). I'm going to give telnet a shot. It night work out better if I can get the connection figured out. Thanks, Scott Link to comment Share on other sites More sharing options...
KEJR Posted January 18, 2011 Share Posted January 18, 2011 Have you drilled down to the simplest test example where no dynamic memory allocation is performed? I would do this to start. If you are dealing with an application with all static global classes there really shouldn't be any glaring possibility for memory leaks. KEJR Link to comment Share on other sites More sharing options...
Recommended Posts