forum.vdsworld.com Forum Index forum.vdsworld.com
Visit VDSWORLD.com
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


FREELIB Question

 
Post new topic   Reply to topic    forum.vdsworld.com Forum Index -> General Help
View previous topic :: View next topic  
Author Message
bornsoft
Contributor
Contributor


Joined: 19 Feb 2009
Posts: 113
Location: Germany

PostPosted: Sun Sep 19, 2010 5:01 pm    Post subject: FREELIB Question Reply with quote

Hi,

some time ago i guess to remember that i've read a posting (possibly by Jules), that an external DLL which is loaded multiple times by the same script anyway resides only once in memory but one FREELIB command would close the file.

Unfortunately i couldn't find this posting anymore.

Is this right in this way?
If it is, wouldn't it be better not to FREELIB a DLL from a reusable unit file in case that the DLL was called before and still is in use by the main script?
And isn't a dll released anyway if the calling task exits?

Or am i completely wrong?

Thanks. bornSoft
Back to top
View user's profile Send private message Visit poster's website
PGWARE
Web Host


Joined: 29 Dec 2001
Posts: 1562

PostPosted: Mon Sep 20, 2010 2:28 am    Post subject: Reply with quote

I believe you are refering to non-vds dll's. If you meant vds dll's they are loaded with the external command.

For non-vds dll's you really should only load them once and close them when you are done using the function. Unless of course you need to make multiple calls to that function over and over; the overhead of loading/unloading the dll would add to cpu/memory usage.

I'm not sure if using a unit would unload the same dll used in your script. When loading a dll a handle is created which is unique; I wouldn't think the unit would close the same handle that was created when you loaded the library in your main exe - the unit would only kill the handle it was assigned when loading the library. All speculation on my part but that is the way it *should work*.

Yes you really should release the dll when you are done, if you are closing them on termination of your app you should. It could cause an exception because the handle to the dll is still opened. It may not throw an error on your computer but the results vary on someone elses computer and it could very well throw an exception error when terminating the app. Also the memory may leak and never be released properly back to the system.
Back to top
View user's profile Send private message
bornsoft
Contributor
Contributor


Joined: 19 Feb 2009
Posts: 113
Location: Germany

PostPosted: Mon Sep 20, 2010 4:58 am    Post subject: Reply with quote

Thanks for the reply PGWare,

sorry, i wasn't clear, of course i ment DLLs like USER32 which ar loaded with LOADLIB.

I'd like you were right, but I realy doubt that, because if cou compile a script with a DSU included, the DSU code is considered as part of the main script, so calling a DLL would be from the same task.

If for example somebody includes my DSU, he loads USER32.DLL in the beginning of his program to use it frequently and then he calls a function from my DSU, which is actually nothing but a subroutine, that also loads USER32, would the DLL really be loaded twice from the same task?

I don't think so. When the subroutine now finishes and unloads the DLL that is still needed by the program, it can run into a serious problem.

Anyway, i hope that you are right.

bornsoft
Back to top
View user's profile Send private message Visit poster's website
PGWARE
Web Host


Joined: 29 Dec 2001
Posts: 1562

PostPosted: Mon Sep 20, 2010 12:54 pm    Post subject: Reply with quote

I don't have vds installed but give it a try and see.

when you load a dll you store a handle to that unique instance, loadlib likely holds this information and then when you free the library it just releases that handle. The unit if it too calls loadlib has it's own unique handle to the dll.

Loading a library mulitple times will open multiple unique handles, thus the reason to free them before exiting the app.
Back to top
View user's profile Send private message
vdsalchemist
Admin Team


Joined: 23 Oct 2001
Posts: 1448
Location: Florida, USA

PostPosted: Wed Sep 22, 2010 3:09 pm    Post subject: Reply with quote

All DLL's have a reference count being held by the OS for the process that has called the LoadLibrary API function. If the process loads the DLL multiple times the reference count increases. Until the reference count is 0 the DLL is still mapped into the memory of the process so technically for each LoadLib there should be a FreeLib. However by default when a process shuts down all DLL's launched by that process is sent a request to end by the OS process loader and there is a sequence to that end process. On very rear occasions a DLL may not unload itself from the process which causes a hang but that is usually due to a thread in the DLL becoming deadlocked. Technically speaking there is no real reason to execute the FreeLib due to the above. However programming etiquette would be to do the freelib when proper and not leave the overhead when you don't need the functionality of the DLL. For more information on this subject take a look at this MSDN article http://msdn.microsoft.com/en-us/library/ms685090(VS.85).aspx
_________________
Home of

Give VDS a new purpose!
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
bornsoft
Contributor
Contributor


Joined: 19 Feb 2009
Posts: 113
Location: Germany

PostPosted: Fri Sep 24, 2010 1:59 am    Post subject: Reply with quote

@vdsalchemist

thank you for the reply, but here is a code example to illustrate the problem I mentioned above.

The mainpart loads user32.dll and uses it continuously.
If you click the button, a function is called, that loads the same dll and unloads it later.

From that moment the script fails.

Now imagine you give this function away within a dsu wich you don't want to give the source code with it.
Someone else now uses this dsu and gets trouble.

Well, programming etiquette is a good thing - but shouldn't a reusable unit one gives to other people avoid to have trouble with it?

So shouldn't we (all who develops reusable units) generally not unload dlls?

EDIT: A much better way would be to evaluate if the needed dll is already loaded. Is this possible?

Please test this code:

Code:

#DEFINE FUNCTION,DLLTest

  option decimalsep,.

  DIALOG CREATE,DLLTest,-1,0,240,160
  DIALOG ADD,TEXT,MousePos,66,92,,,
  DIALOG ADD,BUTTON,Test,108,84,70,24,Test
  DIALOG SHOW

  loadlib User32.dll
  %A = @binary(DWORD,0)@binary(DWORD,0)

:Evloop
  wait event,0.01
  goto @event()

:timer
  %x = @lib(user32.dll,GetCursorPos,BOOL,@addr("%A"))
  %x = @val(@substr(%A,1,4))
  %y = @val(@substr(%A,5,8))
  dialog set,MousePos,%x x %y
  goto evloop

:Testbutton
  %T = @DLLTest(%x,%y)
  goto EvLoop

:Close
  freelib User32.dll
  exit

#    Function DLLTest does nothing useful but loads User32.dll and does a freelib
#    to demonstrate the problem.

:DLLTest
  loadlib User32.dll
  %R = @sum(%1,%2)
  freelib User32.dll
  exit %R

Back to top
View user's profile Send private message Visit poster's website
Aslan
Valued Contributor
Valued Contributor


Joined: 31 May 2001
Posts: 589
Location: Memphis, TN USA

PostPosted: Fri Sep 24, 2010 6:44 pm    Post subject: Reply with quote

Can you think of a better example?

You can use @mousepos() for this.

I cannot think of a realistic senario in vds where you would use an @lib() api call as frequently as in your example.
Back to top
View user's profile Send private message Send e-mail
bornsoft
Contributor
Contributor


Joined: 19 Feb 2009
Posts: 113
Location: Germany

PostPosted: Fri Sep 24, 2010 8:16 pm    Post subject: Reply with quote

You're jokin' man Wink

I'm pretty sure that @mousepos() uses the same api.

It would make sense not to load/unload a dll every time, even if it's not used that much frequently.

Edit: How about watching if an usb-plugin-device is present or not?
Back to top
View user's profile Send private message Visit poster's website
Aslan
Valued Contributor
Valued Contributor


Joined: 31 May 2001
Posts: 589
Location: Memphis, TN USA

PostPosted: Fri Sep 24, 2010 8:35 pm    Post subject: Reply with quote

Quote:
I'm pretty sure that @mousepos() uses the same api.

Hmmm.. Well let's test it
Code:
  #DEFINE FUNCTION,DLLTest

  option decimalsep,.

  DIALOG CREATE,DLLTest,-1,0,240,160
  DIALOG ADD,TEXT,MousePos,66,92,,,
  DIALOG ADD,BUTTON,Test,108,84,70,24,Test
  DIALOG SHOW

:Evloop
  wait event,0.01
  goto @event()

:timer
  %x = @mousepos(X)
  %y = @mousepos(Y)
  dialog set,MousePos,%x x %y
  goto evloop

:Testbutton
  %T = @DLLTest(%x,%y)
  goto EvLoop

:Close
  exit

#    Function DLLTest does nothing useful but loads User32.dll and does a freelib
#    to demonstrate the problem.

:DLLTest
  loadlib User32.dll
  %R = @sum(%1,%2)
  freelib User32.dll
  exit %R


Quote:
How about watching if an usb-plugin-device is present or not?

Are you really gonna test for USB insertion every 0.01 sec Question Wink
Back to top
View user's profile Send private message Send e-mail
vdsalchemist
Admin Team


Joined: 23 Oct 2001
Posts: 1448
Location: Florida, USA

PostPosted: Sat Sep 25, 2010 9:13 pm    Post subject: Reply with quote

bornsoft wrote:
You're jokin' man Wink

I'm pretty sure that @mousepos() uses the same api.

It would make sense not to load/unload a dll every time, even if it's not used that much frequently.

Edit: How about watching if an usb-plugin-device is present or not?


Honestly in the case of a DSU that you don't plan to release the code for I would say to not free a DLL that it is using so as not to cause issues. IMHO if you can't hack it code around it!!! Laughing

_________________
Home of

Give VDS a new purpose!
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
Aslan
Valued Contributor
Valued Contributor


Joined: 31 May 2001
Posts: 589
Location: Memphis, TN USA

PostPosted: Sun Sep 26, 2010 1:25 am    Post subject: Reply with quote

I think he is also concerned that the programmer's code might free the dll his dsu is using. But I see your point that if he loaded the dll every time a function of the dsu is used would correct that. However, if its used a lot it could start to use a lot memory.

Bornsoft, you could also load/free the dll in your DSU (or not). Just provide documentation with the DSU making the programmer aware of what you decide.
Back to top
View user's profile Send private message Send e-mail
vdsalchemist
Admin Team


Joined: 23 Oct 2001
Posts: 1448
Location: Florida, USA

PostPosted: Sun Sep 26, 2010 12:52 pm    Post subject: Reply with quote

Aslan wrote:
I think he is also concerned that the programmer's code might free the dll his dsu is using. But I see your point that if he loaded the dll every time a function of the dsu is used would correct that. However, if its used a lot it could start to use a lot memory.

Bornsoft, you could also load/free the dll in your DSU (or not). Just provide documentation with the DSU making the programmer aware of what you decide.


Actually Windows only loads the DLL into memory once unless it is freed. So even if you did a LoadLib over and over again it would still only have one instance of the DLL for that process.

_________________
Home of

Give VDS a new purpose!
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    forum.vdsworld.com Forum Index -> General Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum

Twitter@vdsworld       RSS

Powered by phpBB © 2001, 2005 phpBB Group