Page 1 of 1

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

Posted: Sun Mar 30, 2008 10:23 pm
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:

Posted: Sun Mar 30, 2008 10:32 pm
by Kaeru Gaman
crashing is not ok....
returning -1 or such as an error-code would be nice, yap.

Posted: Sun Mar 30, 2008 11:08 pm
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).

Posted: Sun Mar 30, 2008 11:14 pm
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.

Posted: Mon Mar 31, 2008 2:07 am
by Mistrel
It is your responsibility as a programmer to know what your pointers are. :roll:

Posted: Mon Mar 31, 2008 12:15 pm
by Kaeru Gaman
ok, convinced...
didn't understand the problem fully.
yes, a programmer is responsable for his pointers.

Posted: Mon Mar 31, 2008 6:36 pm
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]

Posted: Mon Mar 31, 2008 7:39 pm
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!

Posted: Mon Mar 31, 2008 7:55 pm
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:

Posted: Mon Mar 31, 2008 8:49 pm
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.

Posted: Mon Mar 31, 2008 9:00 pm
by Kaeru Gaman
@freak
that IS a reason. thanks for explaining.