Could MemorySize(adr) not just return 0 instead of crash?

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Franky
Enthusiast
Enthusiast
Posts: 213
Joined: Sat Apr 26, 2003 2:58 pm

Could MemorySize(adr) not just return 0 instead of crash?

Post by Franky »

Hi, this one would be nead

In my code I have some structures with Adresses of MemoryBuffers.
The Problem is, that I have no chance of asking if checking for a valid buffer. So, it could be nice if MemorySize() could just return 0 instead of crashing in the whole if the Memorybuffer is invalid.


Thanks for reading (and adding? ^^) :wink:
Give Up everything but trying!
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

crashing is not ok....
returning -1 or such as an error-code would be nice, yap.
oh... and have a nice day.
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Unfortunately, it would mean to maintain an internal list of memory block to see if the pointer you provided is really a block allocated with AllocateMemory(). It would be both memory hungry and slow, so i don't think it worth it. You can still use the IsBadWritePtr_() WinAPI to check if you can write to this pointer (which is not exactly the same, but it could work for you).
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

If you have memory pointers in your program of which you don't know if they are valid, then you have a serious problem.
In this case, it *should* crash, and you should fix it asap.
This is a programming error, its not something the PB commands should cover for.
quidquid Latine dictum sit altum videtur
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

It is your responsibility as a programmer to know what your pointers are. :roll:
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

ok, convinced...
didn't understand the problem fully.
yes, a programmer is responsable for his pointers.
oh... and have a nice day.
Franky
Enthusiast
Enthusiast
Posts: 213
Joined: Sat Apr 26, 2003 2:58 pm

Post by Franky »

Fred wrote:Unfortunately, it would mean to maintain an internal list of memory block.
Oh, Sorry, I thought there was one, now I wonder how it´s made sure that they´re all killed with end. But no matter.

I take my wish back ;)

@Freak and the rest: Of Course I´m responsible for my pointers and Buffers. :D
It was not that I didn´t know if they exist but if they´ve already been allocated.
I just wanted to make sure if the size (x*y) is still the needed one, to reallocate it in the other case. I normaly do it in all needed places, but it´s just somehing like the last Wall before a crash. Show me a bugfree Program and I´ll tell you the author: Chuck Norris.


But however, i´ll use the Api-Function, I just wanted to declare why I asked for this.

[Offtopic]Damn, my English is gone :roll: [/OffTopic]
Give Up everything but trying!
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

Franky wrote:Show me a bugfree Program and I´ll tell you the author: Chuck Norris.
*ROFEL*
yo, I can subscribe this!

Google will not find Chuck Norris, Chuck Norris will find Google!
oh... and have a nice day.
Franky
Enthusiast
Enthusiast
Posts: 213
Joined: Sat Apr 26, 2003 2:58 pm

Post by Franky »

Franky wrote:
Show me a bugfree Program and I´ll tell you the author: Chuck Norris.

*ROFEL*
yo, I can subscribe this!

Google will not find Chuck Norris, Chuck Norris will find Google!

+1 :twisted:
Give Up everything but trying!
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

> But however, i´ll use the Api-Function, I just wanted to declare why I asked for this.

I wouldn't do that. All it gives is a false sense of security.

So IsBadWritePtr_() tells you the location is writable. What does that help you ?
You have no idea what the memory contains. Could be some critical structure of a PB library.
All you know is that it is for some reason writable, you don't know if it is what you think it is.
Hell, it could be your own thread's stack for all you know.

So maybe you prevented an immediate crash, but could have well caused one in the next instant,
or a couple of hours later... you never know.

The only save thing to do is put the pointer value to 0 when it doesn't point
to your own memory and test for that, because 0 is the only memory location
that is always invalid.
quidquid Latine dictum sit altum videtur
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

@freak
that IS a reason. thanks for explaining.
oh... and have a nice day.
Post Reply