|
|
Does this mean I have to write an ownerdraw control to get this functionality?
|
|
|
|
|
Hey,
does someone know how to check if a pointer is valid (was allocated by the application and can be used without problem) or no?
|
|
|
|
|
Have you looked into IsBadCodePtr() , IsBadReadPtr() , IsBadWritePtr() , or IsBadStringPtr() ?
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
You are asking for trouble if you rely on these APIs to tell if memory is safe to use. For example with heaps when you free a block it doesn't mean the memory is deallocated straight away; it can be marked as free and recycled. This is how heaps work. In short if IsBadReadPtr returns TRUE you know for sure that the memory isn't safe to read; but if it returns FALSE it does ***NOT*** mean that the memory is safe to use - it could be on the heap's free list for example. These API are only meant for debugging purposes and not for memory management in the manner your post alludes to.
Steve
|
|
|
|
|
Stephen Hewitt wrote: These API are only meant for debugging purposes and not for memory management in the manner your post alludes to.
Hence my question.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
In Windows you can try IsBadReadPtr , IsBadWritePtr and IsBadStringPtr .
In MFC you can try AfxIsValidAddress and AfxIsValidString .
In case of run-time library, you can try _CrtIsValidPointer , _CrtIsValidHeapPointer and _CrtIsMemoryBlock .
|
|
|
|
|
AfxIsValidAddress worked fine for me.
Thanks a lots
|
|
|
|
|
You are asking for trouble if you rely on these APIs to tell if memory is safe to use. For example with heaps when you free a block it doesn't mean the memory is deallocated straight away; it can be marked as free and recycled. This is how heaps work. For example: if IsBadReadPtr returns TRUE you know for sure that the memory isn't safe to read; but if it returns FALSE it does ***NOT*** mean that the memory is safe to read - it could be on the heap's free list for example.
Most people don't believe when I tell them this. The following code shows this effect in action:
int *pInt = new int;
delete pInt;
ASSERT( AfxIsValidAddress(pInt, sizeof(int)) );
If this practice was safe the ASSERT should fire: it doesn't. The memory is being cached in the heap but it is definitely ***NOT*** safe to use it. DO NOT USE THIS TECHNIQUE!
Steve
|
|
|
|
|
Stephen Hewitt wrote: If this practice was safe the ASSERT should fire: it doesn't.
Actually, it should only fire if the address pointed to by pInt was not contained entirely within the program’s memory space.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
DavidCrow wrote: Actually, it should only fire if the address pointed to by pInt was not contained entirely within the program’s memory space.
Yes, that's what the API does. This doesn't contradict my statement. The OP wanted to know how to tell if memory "was allocated by the application and can be used without problem". AfxIsValidAddress was put forward as a solution and the OP accepted it claiming that it worked. I pointed out that if this API could be used to perform this function then the ASSERT in the code below should fire as AfxIsValidAddress should return FALSE given that it is surely not safe to use a pointer after it has been deleted:
int *pInt = new int;
delete pInt;
ASSERT( AfxIsValidAddress(pInt, sizeof(int)) );
This is not the case. If you try this code AfxIsValidAddress returns TRUE and thus the ASSERT will not fire. Why? Because the address pointed to by pInt was entirely contained in the process’s address space. It's cached by the heap (the small block heap in this case) for recycling. So the problem is that just because an address is physically mapped into the process's address space doesn't mean it's safe to use.
In general these APIs can only be used to tell if memory can't be safely used but not to tell if it is safe to use.
Steve
|
|
|
|
|
Best programs always handle the pointers validity by checking NULL or not. for this you should cautious about allocation and de-allocation
SaRath.
"Do Next Thing..."
My Blog | Understanding State Pattern in C++
|
|
|
|
|
hatemtalbi wrote: does someone know how to check if a pointer is valid (was allocated by the application and can be used without problem) or no?
When you ask that question something is wrong in your code. You should never have to check if a pointer is 'valid'.
|
|
|
|
|
I agree 100%. You get a five for making so much sense.
Steve
|
|
|
|
|
Hi,
i need to know a way to read the motherboard sensors (temp, speed...) using C++. Just like MBM.
Grateful!
Honae
Edit/Delete Message
|
|
|
|
|
Have you checked out the various WMI classes?
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
I wanna do that withou WMI.
|
|
|
|
|
Why, especially when that's the most logical solution?
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Because i wanna do something with the less requirements. Wmi does not work in win 95/98/ME. For that is necessary to install the resource.
|
|
|
|
|
honae wrote: Because i wanna do something with the less requirements.
Which means what exactly? What requirements?
honae wrote: Wmi does not work in win 95/98/ME.
Sure it does.
honae wrote: For that is necessary to install the resource.
What resource?
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
WMI is not integrated in win95/98/ME like is in winxp for example. It is necessary to install WMI resource on this case. I don't want this. I wanna to do some application in the lower level using C++. Do you know the software called MBM (motherboard monitor)? I wanna do something like that, but more simple.
I could use the MBM, but it would be a requirement also. I want that my application makes everything, understands?
|
|
|
|
|
honae wrote: WMI is not integrated in...ME
Actually, it is. For Windows 95 OSR 2, Windows 98 or Windows NT v4.0, a WMI installation package can be downloaded from Microsoft.
honae wrote: I want that my application makes everything, understands?
I understand, but I do not know of a solution that meets that requirement.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Hi,
Is there a way to append text to a rich edit box without using SetSel/ReplaceSel? The problem is that the text is highlightable, and if the user is highlighting text, then receives a chat message, which runs in a separate thread, sometimes the mouse dragging will set the selection to within the chat history and replace the highlighted history instead of appending to the end of the editbox.
Thanks!
Kelly Ryan
|
|
|
|
|
In that case you just have to ensure that all accesses to your rich edit box all happen on your main (GUI) thread. When you get data on your seperate thread, post a user defined message to your gui thread and have the handler of that message do the appending (remember to save and restore the current user selection).
|
|
|
|
|
Oh yeah.. I was thinking I'd still run into shared memory problems if I set up an event queue but if I just post a windows message I won't have to worry about memory locking or anything. Great, thanks!
Kelly Ryan
|
|
|
|