|
Hi Mike
I got it to work on my own with some trial/error. But I am worried about leaks
Nish
Regards,
Nish
Native CPian.
Born and brought up on CP.
With the CP blood in him.
|
|
|
|
|
Okay, after some fiddling I came up with this. It works, but is it okay?
CDC mdc;
mdc.CreateCompatibleDC(pDC);
CBitmap b;
b.LoadBitmap(IDB_BITMAP1);
BITMAP bmap;
b.GetBitmap(&bmap);
CSize s = CSize(bmap.bmWidth,bmap.bmHeight);
mdc.SelectObject(&b);
pDC->BitBlt(0,0,s.cx,s.cy,&mdc,0,0,SRCCOPY );
I am not deleting or releasing the CBitmap or the CDC as I assume their destructors will do it for me. is that a valid assumption?
Nish
Regards,
Nish
Native CPian.
Born and brought up on CP.
With the CP blood in him.
|
|
|
|
|
When b's destructor is called, the bitmap is still selected into mdc device context. ::DeleteObject call silently fails and you have resource leak. You have to ensure that b isn't selected into any context when its C++ d'tor is executed. The tricky way is to change order of declarations of mdc and b. If mdc is second, it'll be destroyed first and your bitmap will be released.
Or - you can store CBitmap* returned from mdc.SelectObject call and re-select it after BitBlt.
Tomasz Sowinski -- http://www.shooltz.com
- It's for protection - Protection from what? Zee Germans?
|
|
|
|
|
Hello Tomasz, I realized that. So I did this :-
CDC mdc;
mdc.CreateCompatibleDC(pDC);
CBitmap b;
b.LoadBitmap(IDB_BITMAP1);
BITMAP bmap;
b.GetBitmap(&bmap);
CSize s = CSize(bmap.bmWidth,bmap.bmHeight);
mdc.SelectObject(&b);
pDC->BitBlt(0,0,s.cx,s.cy,&mdc,0,0,SRCCOPY );
b.DeleteObject();
I have added b.DeleteObject() and I hope that'll avoid the situation you predict
Nish
Author of the romantic comedy
Summer Love and Some more Cricket [New Win]
Buy it, read it and admire me
|
|
|
|
|
Sorry - this will not work. b is selected into mdc when you're calling DeleteObject; the call fails. GDI doesn't allow deletions on objects (brushes, pens, bitmaps, etc) selected into any device context.
DeleteObject call doesn't make any difference - CBitmap d'tor calls it anyway.
Tomasz Sowinski -- http://www.shooltz.com
- It's for protection - Protection from what? Zee Germans?
|
|
|
|
|
Thanks Tomasz. I alreday sent the prototype. But the leak is a once only leak. You see, when the app starts up there is no msp file loaded and thus no image to show. So I addeda default bitmap resource and was showing it. so it'll be a one-time leak
anyway, bnad of me to do that
Nish
ps. thanks
Author of the romantic comedy
Summer Love and Some more Cricket [New Win]
Buy it, read it and admire me
|
|
|
|
|
Nish - Native CPian wrote:
so it'll be a one-time leak
I'm not sure. If the code you've posted is in OnDraw, you'll have a leak each time your view is repainted.
Tomasz Sowinski -- http://www.shooltz.com
- It's for protection - Protection from what? Zee Germans?
|
|
|
|
|
Tomasz Sowinski wrote:
I'm not sure. If the code you've posted is in OnDraw, you'll have a leak each time your view is repainted.
Actually, it is a one-time leak. Because I check my document variables and ONLY if they are empty do I attempt to do all this stuff. That entire block of code I pasted is in an if() {} block. And I have removed "New" from the menu, toolbar and the accelerator keys.
Nish
Author of the romantic comedy
Summer Love and Some more Cricket [New Win]
Buy it, read it and admire me
|
|
|
|
|
Assuming that it's inside OnDraw (is it?), the leak will occur anytime WM_PAINT is processed. This can happen when you resize the window, open About box of your program, switch between applications, start screensaver, etc.
Tomasz Sowinski -- http://www.shooltz.com
- It's for protection - Protection from what? Zee Germans?
|
|
|
|
|
Tomasz Sowinski wrote:
Assuming that it's inside OnDraw (is it?), the leak will occur anytime WM_PAINT is processed. This can happen when you resize the window, open About box of your program, switch between applications, start screensaver, etc.
Uh Oh! That's true. But I hope that the moment they run the program, they'll open an image file. If they don't and they leave it open....I hate to think of the rest...
Nish
Author of the romantic comedy
Summer Love and Some more Cricket [New Win]
Buy it, read it and admire me
|
|
|
|
|
Nish - Native CPian wrote:
I am not deleting or releasing the CBitmap or the CDC as I assume their destructors will do it for me. is that a valid assumption?
No, it isn't, because the bitmap is selected in the DC, the DC cannot be destroyed. Therefore I would change your code to following:
CDC mdc;
mdc.CreateCompatibleDC(pDC);
CBitmap b;
b.LoadBitmap(IDB_BITMAP1);
BITMAP bmap;
b.GetBitmap(&bmap);
CSize s = CSize(bmap.bmWidth,bmap.bmHeight);
CBitmap* pOldBitmap = (CBitmap*)mdc.SelectObject(&b);
pDC->BitBlt(0,0,s.cx,s.cy,&mdc,0,0,SRCCOPY );
mdc.SelectObject(pOldBitmap); SelectObject returns the previous selected object of that type (in this case a bitmap), so you can use this to unselect your bitmap.
Regards
Thomas
Sonork id: 100.10453 Thömmi
Disclaimer: Because of heavy processing requirements, we are currently using some of your unused brain capacity for backup processing. Please ignore any hallucinations, voices or unusual dreams you may experience. Please avoid concentration-intensive tasks until further notice. Thank you.
|
|
|
|
|
Hi Nish. Check out this [clickety] article by Joseph Newcomer.
---
CPUA 0x5041
Sonork 100.11743 Chicken Little
Within you lies the power for good - Use it!
|
|
|
|
|
isn't it okay if I do a b.DeleteObject()
That'll delete the bitmap even if it's selected in a DC, won't it?
Nish
Author of the romantic comedy
Summer Love and Some more Cricket [New Win]
Buy it, read it and admire me
|
|
|
|
|
|
Nope, because even if the bitmap handle is invalid, it is still selected in the DC. It would be an overkill, if Windows had to check, if any selected handle is still valid, when it deletes a DC.
Regards
Thomas
Sonork id: 100.10453 Thömmi
Disclaimer: Because of heavy processing requirements, we are currently using some of your unused brain capacity for backup processing. Please ignore any hallucinations, voices or unusual dreams you may experience. Please avoid concentration-intensive tasks until further notice. Thank you.
|
|
|
|
|
Thanks Thommi. I have actually sent the sample app. But it's only a sample protoype. I'll correct this issue in the final release
Nish
Author of the romantic comedy
Summer Love and Some more Cricket [New Win]
Buy it, read it and admire me
|
|
|
|
|
question: don't you need to delete the memory DC you created? I remember I read such a sentence in a VC++ book:
whenever you create something, you take the responsibility to delete it, otherwise, there would be resource leak.
so the code would be:
CDC memDc = dc.CreateCompatibleDC();
......
memDc.DeleteDC();
|
|
|
|
|
Why is CDocument::DoSave undocumented???
Nish
Regards,
Nish
Native CPian.
Born and brought up on CP.
With the CP blood in him.
|
|
|
|
|
It's semi-documented. They mention DoSave in MFC Technical Note 22. There's also KB article Q141921.
Are you trying to support multiple file extensions per doc type?
Tomasz Sowinski -- http://www.shooltz.com
- It's for protection - Protection from what? Zee Germans?
|
|
|
|
|
Tomasz Sowinski wrote:
Are you trying to support multiple file extensions per doc type?
Nope.
My SDI app is a msp-image to bmp converter (msp is a funny custom image format and no one ever heard of it)
So I want file-open to show *.msp and file-save to show *.bmp
Nish
p.s. issue is solved now. DoSave did the trick for me
Author of the romantic comedy
Summer Love and Some more Cricket [New Win]
Buy it, read it and admire me
|
|
|
|
|
hi,
I do paint some images on a window dynamically.
while painting if i maximise & minimise some other window over that.Then image won't be disaplyed properly.
anybody has any idea,why it is happening so?
Neha
|
|
|
|
|
During handling of WM_PAINT, or any message processing for that matter, you (obviously) can't recieve and handle another message. Therefore, if your message processing is lengthy it would look like your application is frozen/hung.
Perhaps it could work better if you did your painting to an off-screen bitmap and just blitted it to your window on WM_PAINT?
|
|
|
|
|
Hello
I have an SDI app. Basically it opens files of type *.MSP [a custom image format unheard of previously].
Now this is my IDR_MAINFRAME in the string table :-
Msp File Viewer/Converter\n\nMspTes\nMsp Files(*.msp)\n.msp\nMspTest.Document\nMspTes Document
Now my problem is that when I open files I want to get *.msp and only *.msp
But when I save my file I want to get *.bmp
My program can open only msp files and it can save only bmp files
It's an msp-to-bmp converter
How do I have different default documents for OnOpenDoicument and OnSaveDocument???
Regards
Nish
p.s. I also tried to override the OnSaveDocument save-dialog, but failed
Regards,
Nish
Native CPian.
Born and brought up on CP.
With the CP blood in him.
|
|
|
|
|
How do I have different default documents for OnOpenDoicument and OnSaveDocument???
That I think you can't really do, but how about just overriding CDocument::OnSaveDocument or CDocument::DoSave and changing the extension?
|
|
|
|
|
Mike Nordell wrote:
CDocument::OnSaveDocument
I tried overriding this. But by the time it reaches here, the save-dialog has already been shown and dismissed!!!
Mike Nordell wrote:
CDocument::DoSave
????
Nish
Regards,
Nish
Native CPian.
Born and brought up on CP.
With the CP blood in him.
|
|
|
|