Please add a return value for CloseLibrary

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Please add a return value for CloseLibrary

Post 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?
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Please add a return value for CloseLibrary

Post 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
quidquid Latine dictum sit altum videtur
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Please add a return value for CloseLibrary

Post 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.
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Please add a return value for CloseLibrary

Post 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?
quidquid Latine dictum sit altum videtur
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Please add a return value for CloseLibrary

Post 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.
BERESHEIT
Thorium
Addict
Addict
Posts: 1305
Joined: Sat Aug 15, 2009 6:59 pm

Re: Please add a return value for CloseLibrary

Post 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.
PMV
Enthusiast
Enthusiast
Posts: 727
Joined: Sat Feb 24, 2007 3:15 pm
Location: Germany

Re: Please add a return value for CloseLibrary

Post by PMV »

Code: Select all

mylib = OpenLibrary(#PB_Any, "mylib.dll")
If IsLibrary(mylib)
  ;do whatever action you had in mind
   CloseLibrary(mylib)
Endif
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Please add a return value for CloseLibrary

Post 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.
quidquid Latine dictum sit altum videtur
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: Please add a return value for CloseLibrary

Post 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!
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Please add a return value for CloseLibrary

Post by freak »

To be honest, i thought this was somewhere in the manual.
quidquid Latine dictum sit altum videtur
eriansa
Enthusiast
Enthusiast
Posts: 277
Joined: Wed Mar 17, 2004 12:31 am
Contact:

Re: Please add a return value for CloseLibrary

Post 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()?
Thorium
Addict
Addict
Posts: 1305
Joined: Sat Aug 15, 2009 6:59 pm

Re: Please add a return value for CloseLibrary

Post 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.
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Re: Please add a return value for CloseLibrary

Post 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()
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Please add a return value for CloseLibrary

Post 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.
#NULL
Addict
Addict
Posts: 1497
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Please add a return value for CloseLibrary

Post 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:
Post Reply