in reply to Re^2: Win32::API Memory Exception with GetCommandLine() (which returns a static string)
in thread Win32::API Memory Exception with GetCommandLine() (which returns a static string)
Nothing I've tried has caused so much as a murmer of trouble:
C:\test>junkjunkjunkjunkjunkjunkxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.bat string[178] = 'perl -x -S junkjunkjunkjunkjunkjunkxxxxxxxxxxxxxxxxxxxx +xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.bat '
I'll have a go at building the latest Win32::API later today. You failed to mention which version of perl?
Do you think that garbage collection of a returned static string is incorrect?
I don't know for sure, but looking at the source code, it appears to do the right thing by copying the return value into a perl SV before returning it:
case T_POINTER: ApiFunctionPointer = (ApiPointer *) ApiFunction; pReturn = ApiFunctionPointer(); /* #### only works with strings... #### */ cReturn = (char *) safemalloc(strlen(pReturn)); strcpy(cReturn, pReturn); break; ... XSRETURN_PV(cReturn);
Which looks right as far as my nascent XS skills allow me to tell.
The one thing I didn't see was any attempt to free up the memory for the string passed--which constitutes a memory leak if you call it huge numbers of times. The following script terminates having grown to 35MB. Not that there is any point in calling this particular call multiple times as the return will never change, but it might be significant for other apis. Presumably such returned strings should be freed using Memfree() or VirtualFree() though there is no mention of this in the docs.
#!/usr/bin/perl -w use strict; use Win32::API; Win32::API->Import("kernel32", "LPTSTR GetCommandLine()"); printf "\r%d : %s\t", $_, GetCommandLine() for 1 .. 1e6;
Do you get the same problems if you run the perl code directly rather than via the .bat wrapper?
Also, why are you using pack in your code? And does it help if you remove it?
(Incidently, why do you wrap it anyway?)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Win32::API Memory Exception with GetCommandLine() (which returns a static string)
by Wyrdweaver (Beadle) on Jul 07, 2007 at 17:55 UTC | |
by BrowserUk (Patriarch) on Jul 07, 2007 at 18:38 UTC |