Page 1 of 2

Please add a return value for CloseLibrary

Posted: Sat Oct 09, 2010 8:34 am
by Mistrel
If I close a library.. how do I know if it really closed? If I try to close it again just to be sure:

Code: Select all

LibID=OpenLibrary(#PB_Any,"kernel32.dll")
CloseLibrary(LibID)
CloseLibrary(LibID)
Crash!

No way to check to see if it failed? Crash if called twice? That's pretty harsh!

I know there is IsLibrary. But why not just use that internally and give us a return value?

Re: Please add a return value for CloseLibrary

Posted: Sat Oct 09, 2010 12:27 pm
by freak
When you call CloseLibrary(), the library is closed. Period. If there was a returnvalue, it would always return success.

See also: http://blogs.msdn.com/b/oldnewthing/arc ... 11066.aspx

Re: Please add a return value for CloseLibrary

Posted: Sat Oct 09, 2010 12:40 pm
by Mistrel
So I guess this falls into the category of:
So what should your program do when it gets an error from CloseHandle? Log the error and die. Die suddenly, horribly, without any opportunity to save files or cleanup. This will force you to fix it.

Re: Please add a return value for CloseLibrary

Posted: Sat Oct 09, 2010 3:21 pm
by freak
Calling CloseLibrary() with an invalid object may crash, as it is with all other PB objects.
Calling CloseLibrary() with a valid object will always succeed.

So... what do you need a returnvalue for?

Re: Please add a return value for CloseLibrary

Posted: Sat Oct 09, 2010 4:16 pm
by netmaestro
If you want to test if your CloseLibrary was successful you can do:

Code: Select all

CloseLibrary(mylib)
If IsLibrary(mylib)
  ;It failed, do whatever action you had in mind
Endif
And if you want to avoid crashes you can always precede CloseLibrary with If IsLibrary[..] and you'll never try to close an already-closed library.

Re: Please add a return value for CloseLibrary

Posted: Sun Oct 10, 2010 6:45 pm
by Thorium
netmaestro wrote:If you want to test if your CloseLibrary was successful you can do:

Code: Select all

CloseLibrary(mylib)
If IsLibrary(mylib)
  ;It failed, do whatever action you had in mind
Endif
And if you want to avoid crashes you can always precede CloseLibrary with If IsLibrary[..] and you'll never try to close an already-closed library.
As freak said, it never fails.
You may think it fails because a dll isnt unloaded but CloseLibrary unloads it only if no other code needs it anymore. At least on windows. The Win API counts how many times a dll load is called and if you call freelibrary it will decrement the counted number and will unload it if the number is 0.

Re: Please add a return value for CloseLibrary

Posted: Sun Oct 10, 2010 11:27 pm
by PMV

Code: Select all

mylib = OpenLibrary(#PB_Any, "mylib.dll")
If IsLibrary(mylib)
  ;do whatever action you had in mind
   CloseLibrary(mylib)
Endif

Re: Please add a return value for CloseLibrary

Posted: Mon Oct 11, 2010 12:21 am
by freak
PMV wrote:

Code: Select all

mylib = OpenLibrary(#PB_Any, "mylib.dll")
If IsLibrary(mylib)
  ;do whatever action you had in mind
   CloseLibrary(mylib)
Endif
This code is nonsense. You are wasting CPU power checking something that you already know.

The returnvalue of OpenLibrary() tells you wether the call succeeded or not. If it fails you get 0, otherwise it succeeded. That value is all you need to check...

Code: Select all

mylib = OpenLibrary(#PB_Any, "mylib.dll")
If mylib
  ;do whatever action you had in mind
   CloseLibrary(mylib)
Endif
Keep this in mind:
All IsXXX() commands are expensive operations, because the only way to validate #PB_Any objects is to look at all of them and see if one matches. That won't matter if you have just two libraries open but if we are talking about a few hundred gadgets or images, things look different. So better don't get into that habit.

In fact: A correctly written program should have no need to call any IsXXX() function ever since you can always tell on object creation whether it succeeded or failed, and you know when you free the object so there is no reason for a confusion of weather an object is valid or not. These functions are a debugging tool, nothing else.

Re: Please add a return value for CloseLibrary

Posted: Mon Oct 11, 2010 1:07 am
by c4s
freak wrote:Keep this in mind:
All IsXXX() commands are expensive operations, because the only way to validate #PB_Any objects is to look at all of them and see if one matches. That won't matter if you have just two libraries open but if we are talking about a few hundred gadgets or images, things look different. So better don't get into that habit.

In fact: A correctly written program should have no need to call any IsXXX() function ever since you can always tell on object creation whether it succeeded or failed, and you know when you free the object so there is no reason for a confusion of weather an object is valid or not. These functions are a debugging tool, nothing else.
Wohooo. You could have said this earlier. ;)
No, seriously now I'm going to look through my code to see if I'm using such a command without "real" need or if I could just delete all of them.
I have to admit that I thought it's the other way round. For me IsXXX() was something like "Returnvalue <> 0"... something helpful to double check it. I think this should be stated in the help file!

Re: Please add a return value for CloseLibrary

Posted: Mon Oct 11, 2010 2:39 am
by freak
To be honest, i thought this was somewhere in the manual.

Re: Please add a return value for CloseLibrary

Posted: Mon Oct 11, 2010 11:29 am
by eriansa
c4s wrote:
freak wrote:Keep this in mind:
All IsXXX() commands are expensive operations, because the only way to validate #PB_Any objects is to look at all of them and see if one matches. That won't matter if you have just two libraries open but if we are talking about a few hundred gadgets or images, things look different. So better don't get into that habit.

In fact: A correctly written program should have no need to call any IsXXX() function ever since you can always tell on object creation whether it succeeded or failed, and you know when you free the object so there is no reason for a confusion of weather an object is valid or not. These functions are a debugging tool, nothing else.
Wohooo. You could have said this earlier. ;)
No, seriously now I'm going to look through my code to see if I'm using such a command without "real" need or if I could just delete all of them.
I have to admit that I thought it's the other way round. For me IsXXX() was something like "Returnvalue <> 0"... something helpful to double check it. I think this should be stated in the help file!
Does this apply to PB4.31 AND IsImage()? (50000 images created with #PB_Any)
I just did some performance tests and it looks very fast! (IsImage() -> <1ms)

Forget it : I tested it more profoundly and indeed IsImage() can take some time!!!
This leads to the following : can we have something like CountImages()?

Re: Please add a return value for CloseLibrary

Posted: Mon Oct 11, 2010 12:17 pm
by Thorium
eriansa wrote: This leads to the following : can we have something like CountImages()?[/b]
Why would you need something like that?
You can easily count the images by yourself on loading or creation.

I never ever used the IsXXX commands and never saw a need for them.

Re: Please add a return value for CloseLibrary

Posted: Mon Oct 11, 2010 12:19 pm
by cas
Yes, you can have it right now:

Code: Select all

Procedure MyCountImages(Change=0)
  Static counter
  counter+Change
  ProcedureReturn counter
EndProcedure

Procedure MyCreateImage(ID,Width,Height,Depth=24)
  MyCountImages(1) ;increase counter by 1
  ProcedureReturn CreateImage(ID,Width,Height,Depth)
EndProcedure

Procedure MyFreeImage(ID)
  FreeImage(ID)
  MyCountImages(-1) ;decrease counter by 1
EndProcedure


image=MyCreateImage(#PB_Any,100,100)
Debug MyCountImages()

Re: Please add a return value for CloseLibrary

Posted: Mon Oct 11, 2010 1:04 pm
by Mistrel
This thread has.. taken an odd and unexpected turn. :|

It's not uncommon for my requests and bugs turn into a "Mistrel, what were you thinking!" moment. But sometimes it's interesting to see them acquire a life of their own.

Re: Please add a return value for CloseLibrary

Posted: Mon Oct 11, 2010 4:05 pm
by #NULL
Mistrel wrote:But sometimes it's interesting to see them acquire a life of their own.
well, what were you thinking! it's a Forum :lol: