Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^3: odd line in windows

by syphilis (Chancellor)
on Sep 07, 2011 at 15:14 UTC ( #924607=note: print w/replies, xml ) Need Help??


in reply to Re^2: odd line in windows
in thread odd line in windows

but sequenced question is, how free this memory I allocate in C?

One way is to create a copy, free the memory, then return the copy.

The following rewrite of get_proc_name() works for me, and frees the memory:
SV * get_proc_name( int processID ) { char * szProcessName; SV * outsv; Newxz(szProcessName, MAX_PATH, char); HANDLE hProcess = OpenProcess ( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID ); if (NULL != hProcess ) { HMODULE hMod; DWORD cbNeeded; if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), &cbNee +ded) ) { GetModuleBaseName( hProcess, hMod, szProcessName, MAX_PATH + - 1); } else { szProcessName[0] = '#'; szProcessName[1] = 0; } } else { szProcessName[0] = '*'; szProcessName[1] = 0; } CloseHandle( hProcess ); outsv = newSVpv(szProcessName, 0); Safefree(szProcessName); return outsv; }
Cheers,
Rob

Replies are listed 'Best First'.
Re^4: odd line in windows
by BrowserUk (Pope) on Sep 07, 2011 at 15:37 UTC

    You only need do one or the other. Not both.

    That is, if you are going to copy the string into an SV in order to return it, there is no reason not to allocate the local copy on the stack:

    char* get_proc_name( int processID ) { TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>"); SV * outsv; ... outsv = newSVpv(szProcessName, 0); return outsv; }

    You save an alloc and free that way.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      You save an alloc and free that way

      Yes - I think that's a better approach.
      (However, specifying a return type of char * and then returning an SV * is something that doesn't work well for me ;-)
      I find the following works as expected (without any need to dynamically allocate/free memory) , and doesn't throw any warnings:
      SV * get_proc_name( int processID ) { char szProcessName[MAX_PATH] = {0}; HANDLE hProcess = OpenProcess ( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID ); if (NULL != hProcess ) { HMODULE hMod; DWORD cbNeeded; if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), &cbNee +ded) ) { GetModuleBaseName( hProcess, hMod, szProcessName, MAX_PATH + - 1); } else { szProcessName[0] = '#'; szProcessName[1] = 0; } } else { szProcessName[0] = '*'; szProcessName[1] = 0; } CloseHandle( hProcess ); return newSVpv(szProcessName, 0); }
      Cheers,
      Rob

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://924607]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2017-10-18 06:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My fridge is mostly full of:

















    Results (242 votes). Check out past polls.

    Notices?