|
(edited)
Alvaro's explanation points to the reason. But note that even the following won't work:
CString a = "Ophelia";
CString b = a; // *1*
strcpy((LPTSTR)(LPCTSTR) b, "Hamlet"); // *2*
will change a and b, because:
CString uses a "copy on write" mechanism. At line *1*, b will reference the same string buffer as a. Only when you change one of the strings, an actual copy is made.
*2* now modifies the shared string buffer without CString having any chance of knowing it. Hence no copy, hence a shared buffer gets modified.
-----------------------
CString doesn't have a cast to LPTSTR for a reason. And blindly casting to LPTSTR is definitely not a good idea.
Peter
|
|
|
|
|
thank you all!, nice explanation and I've understood it!
|
|
|
|
|
Read this :
http://www.codeproject.com/string/cstringmgmt.asp
----
Xian
|
|
|
|
|
Does anyone know if I can talk to the parallel port the same way I'd do so with the serial port ( eg CreateFile(...,"COM1",...) -> CreateFile(...,"LPT1",...) ).
It'd be easy to test it out, but I don't have the hardware that I want to talk to - and I won't buy it unless I know I can talk to it over the parallel port under Win2k.
If anyone's interested, I'm trying to figure out whether or not I could build a Win2k driver for this Seiko 320x240 monochrome LCD screen (for an oboard computer in my car).
--
Russell Morris
Georgia Institute of Technology
"Hello, I'm doctor stupid. I'm going to take out your liver bones!" - Ralph Wiggum
|
|
|
|
|
Check this out:
http://www.naughter.com/parallelport.html
I've used his serial port wrapper class with great success.
Mike Mullikin
"Real programmers don't document their code. It was hard to write - it should be hard to read!"
|
|
|
|
|
Friggin' perfect!
I'm somewhat disappointed, though, as now I'll have to spend $200 on this LCD screen so I can start playing with it!
Thanks.
--
Russell Morris
Georgia Institute of Technology
"Hello, I'm doctor stupid. I'm going to take out your liver bones!" - Ralph Wiggum
|
|
|
|
|
look at this
http://www.techsoft.no/bendik/
or this
http://www.codeguru.com/treeview/CMultiTree.shtml
|
|
|
|
|
Hi,
I would like to have an OnKeyDown event like, for the virtual keys ( -> <- ALT CTRL), is there anyway to do that, or I have to go to the PretranslateMessage... ?
Thanks, Bye!
Braulio
|
|
|
|
|
Hi,
I need to find a tool or learn some techniques that can help me trace GDI leakage. I've found some tools on MSDN which work for Windows Me and 2000, but i also need to check on NT4. Besides, the tools i found on MSDN weren't very good anyway. They reported massively spiralling leakages for palletes and regions and bitmaps when i'm not creating any! Surely to leak, i would have to create and forget to destroy. So those tools may justfifably have been telling me about leaks - but definately they were incorrectly identifying the resource. And they didn't help you find the code.
Any ideas please ?
Jase
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
View your digital photos and images with ease using the ultimate desktop image manager for Microsoft Windows
Download your free copy of SlideShow Desktop today from http://www.slideshowdesktop.com
|
|
|
|
|
Hi Jase !
There is a tool called GDIObj.exe ( Try to find this on the web), if you don´t have it tell me, and I send it to you.
HTH
Braulio
|
|
|
|
|
Hi Braulio,
Thanks for you reply. I searched using google.com and found a gdiobj.exe on ftp.microsoft. It turned out to be a self extracting zip file from 1995 containing source code which i couldn't build (it doesn't have a workspace, so i would have to work out the libraries and dependencies myself in order to link it). It also came with 2 exe's which were dependant upon mfc30.dll so i coldn' run them.
I would really appreciate it if you could send me your copy.
jpj@totalise.co.uk
Thanks
Jase
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
View your digital photos and images with ease using the ultimate desktop image manager for Microsoft Windows
Download your free copy of SlideShow Desktop today from http://www.slideshowdesktop.com
|
|
|
|
|
Bounds checker is probably the best tool for tracking GDI leaks, but it ain't cheap. For memory leaks I would recommend PurifyNT, but, again, it's expensive.
In my experience of Windows programming (10 years now) the most common leaks are:
* Deleting pens, brushes or fonts that are still selected in the DC.
* Calling GetDC without a corresponding ReleaseDC.
Regards.
|
|
|
|
|
Thanks for your reply.
I have tried Purify but when i have Purify running, my dialog will not display. As for bounds checker, i would love to take look, as I have heard much about it. However, they don't provide an evaluation copy which in my book is criminal. I'm not paying all that money without testing it first ...
I shall double check my code for the instances you mention.
Jase
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
View your digital photos and images with ease using the ultimate desktop image manager for Microsoft Windows
Download your free copy of SlideShow Desktop today from http://www.slideshowdesktop.com
|
|
|
|
|
I agree - not having an evaluation copy to play with is pants!
Here are some classic leak examples:
void CMyView::OnDraw(CDC* pDC)
{
CFont font;
// Create a font
font.CreatePointFont(120, _T("Arial"), pDC);
// Select the font into the DC
pDC->SelectObject(&font);
// Draw
pDC->TextOut(0, 0, _T("Leak!"));
// The original font isn't selected back into the DC
// and the font we created is about to be deleted.
// This will soon deprive you of resources (Win9x especially)
}
void CMyView::DrawSomething()
{
// Get a DC
CDC* pDC = GetDC();
// Draw
pDC->TextOut(0, 0, _T("Leak!"));
// You must call ReleaseDC, else this will leak!
}
|
|
|
|
|
Hi,
I can see why these examples would cause leakage, and in looking through my source files - I'm not guilty of either of them (mores he pity, cos then at least i would have found some leaks ...
Jase
Jase
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
View your digital photos and images with ease using the ultimate desktop image manager for Microsoft Windows
Download your free copy of SlideShow Desktop today from http://www.slideshowdesktop.com
|
|
|
|
|
I am more than happy to look at your code if you like - I quite enjoy looking for leaks! Plus I have a pucker copy of BC I can run it through ...
|
|
|
|
|
Thanks for your kind offer. However, my source is for a commercial product so i can't show you the code ...
Jase
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
View your digital photos and images with ease using the ultimate desktop image manager for Microsoft Windows
Download your free copy of SlideShow Desktop today from http://www.slideshowdesktop.com
|
|
|
|
|
Out of interest - how did you get your hands on a copy. I've looked on their website and it doesn't even give a price. It sems you have to go through a salesman / vendor in order to get a copy. And did you then enter a registration code to it ? (that is, it was 'unlockable') ?
Jase
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
View your digital photos and images with ease using the ultimate desktop image manager for Microsoft Windows
Download your free copy of SlideShow Desktop today from http://www.slideshowdesktop.com
|
|
|
|
|
Also, calling DeleteObject instead of DeleteDC.
|
|
|
|
|
We used to use boundschecker, but it din't seem to work after I upgraded to VC 6.0. (i.e. it was reporting memory leaks that weren't there)
|
|
|
|
|
MSDN mag ran an article in the last 12-18 months and not in the last 6, which was a program that showed you the bitmaps/pens/etc that had not been deleted. It rocked, if you cannot find it LMK and I'll dig it up.
Also, W2000 makes you work pretty hard to leak GDI memory, because it cleans up after you. This I know from bitter experience.
Christian
As I learn the innermost secrets of the around me, they reward me in many ways to keep quiet.
Men with pierced ears are better prepared for marriage. They've experienced pain and bought Jewellery.
|
|
|
|
|
I think the tool you mentioned is the tool i mentioned earlier. It is only 95/98 , and you're right - it shows the pensd , palletes and fonts that you are leaking. And it does indeed display lots of them! Only i don't use any, so its not my app thats leaking them
From the outset, it looks pretty good. But in truth, it doesn't reflect the state of my application one iota.
It's called GDIUsage - is this the one ? You have to build it from source - available from MSJ online.
Jase
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
View your digital photos and images with ease using the ultimate desktop image manager for Microsoft Windows
Download your free copy of SlideShow Desktop today from http://www.slideshowdesktop.com
|
|
|
|
|
That sounds like it. I dunno about NT 4, but I can tell you that code that runs fine under W2000 will crash W98 for GDI leaks in seconds. It's possible NT4 protects itself in a similar way, but the reason the code only ran under 95/98 (and I guess ME ) is that the handles to the objects were all global. It shows you what exists on the system, not what has leaked, that is why it fills up. You look to see if the list is growing a lot by taking snapshots and seeing what is different between two points in time, and if there is a lot there you should have deleted.
Christian
As I learn the innermost secrets of the around me, they reward me in many ways to keep quiet.
Men with pierced ears are better prepared for marriage. They've experienced pain and bought Jewellery.
|
|
|
|
|
Hi,
Thanks for your reply.
I have managed to trace the problem that i had (thanks to an evaluation copy of Bounds Checker) - it was all thanks to a couple of HKEY's which i had forgot to do a RegClose() on. Just a silly mistake really (but aren't they all?).
Still, it begs the question - why did the gdi tool we are discussing show me lots of fonts , pallettes and regions when i took the comparison snapshots? I had no other program running at the time, so these resources weren't being created by any other application. Is it possible that it could mistake HKEY's (and whatever may be internally allocated when opening a registry key) for GDI resources , and then point me at some bogus resources ?
Are registry keys treated similarly to file's / sockets ? i.e. there are a physical limit to the number of open files you can have - is the same true for reg keys ? Would this also make sense of all the 'A resource you requested was unavailable' msg boxes that popped up when my 'resources' were running low. Am i likely to get this error if it was in fact due to reg keys ?
damn, there's so much to learn.
Jase
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
View your digital photos and images with ease using the ultimate desktop image manager for Microsoft Windows
Download your free copy of SlideShow Desktop today from http://www.slideshowdesktop.com
|
|
|
|
|
How did you manage to get an evaluation of Boundschecker?
James Spibey
I love the word naked, it's brilliant isn't it, 'naked'. When I was a kid I used to write the word naked on a bit of paper hundreds of times and rub my face in it - Jeff, Coupling, BBC2
|
|
|
|