Click here to Skip to main content
15,888,521 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: program leaks memory when i resize Pin
Code-o-mat8-Mar-11 0:32
Code-o-mat8-Mar-11 0:32 
AnswerRe: program leaks memory when i resize Pin
CPallini8-Mar-11 0:51
mveCPallini8-Mar-11 0:51 
AnswerRe: program leaks memory when i resize Pin
Hans Dietrich8-Mar-11 1:13
mentorHans Dietrich8-Mar-11 1:13 
AnswerRe: program leaks memory when i resize Pin
leorex8-Mar-11 21:00
leorex8-Mar-11 21:00 
GeneralRe: program leaks memory when i resize Pin
Richard MacCutchan8-Mar-11 21:35
mveRichard MacCutchan8-Mar-11 21:35 
AnswerRe: program leaks memory when i resize Pin
Code-o-mat8-Mar-11 23:05
Code-o-mat8-Mar-11 23:05 
GeneralRe: program leaks memory when i resize Pin
leorex9-Mar-11 0:14
leorex9-Mar-11 0:14 
GeneralRe: program leaks memory when i resize Pin
Code-o-mat9-Mar-11 0:44
Code-o-mat9-Mar-11 0:44 
That solution looks better, but now you are deleting the brush while it is still selected into the DC, that is not healthy.
If i were you i'd just store the original brush somewhere in the beginnings (before the for loops...) and then use it along.
HBRUSH holdBrush = GetCurrentObject(hdc, OBJ_BRUSH);
...
hBrush_1 = CreateSolidBrush(color[i_row][j_column]);
SelectObject(hdc, hBrush_1);
Rectangle(hdc, 90*j_column+100, 50+30*i_row, 90*j_column+180, 30*i_row+75); //rember is x, y, x, y
SelectObject(hdc, holdBrush);
DeleteObject(hBrush_1);
...



Well, about the other thing, i will try to explain. When you use SelectObject, it will return the handle to the previously selected tool of given type. So if you do this (example):
//We make 3 brushes to play with
HBRUSH brush1    = CreateBrush...;
HBRUSH brush2    = CreateBrush...;
HBRUSH brush3    = CreateBrush...;

//Draw with the first brush
HBRUSH holdBrush = SelectObject(dc, brush1); //holdBrush now has the handle of the brush that was originally selected into the DC
...draw...

//Draw with the second brush
holdBrush = SelectObject(dc, brush2); //At this point, brush1 was previously selected into the DC, so now holdBrush equals brush1
...draw....

//Draw with the third brush
holdBrush = SelectObject(dc, brush3); //At this point, brush2 was previously selected into the DC, so now holdBrush equals brush2
...draw....

//We're done, cleanup (badly)
SelectObject(dc, holdBrush); //holdBrush has brush2 in it, so now we are simply selecting brush2 back into the DC
DeleteObject(brush1);
DeleteObject(brush2); //And here you delete the brush selected into the DC, bad move
DeleteObject(brush3);
...

In your code you did something similar in the loop, in every iteration you put the brush created in the previous iteration into holdBrush, the original, very first brush handle you should use in the end for the DC to perform the cleanup gets lost.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> //TODO: Implement signature here<

GeneralRe: program leaks memory when i resize Pin
leorex9-Mar-11 10:21
leorex9-Mar-11 10:21 
GeneralRe: program leaks memory when i resize Pin
Code-o-mat9-Mar-11 10:27
Code-o-mat9-Mar-11 10:27 
AnswerRe: program leaks memory when i resize Pin
SledgeHammer019-Mar-11 13:58
SledgeHammer019-Mar-11 13:58 
Questionput image on client area of main frame windows Pin
Max++7-Mar-11 19:37
Max++7-Mar-11 19:37 
AnswerRe: put image on client area of main frame windows Pin
Hans Dietrich7-Mar-11 19:45
mentorHans Dietrich7-Mar-11 19:45 
GeneralRe: put image on client area of main frame windows Pin
Max++7-Mar-11 20:09
Max++7-Mar-11 20:09 
QuestionSmall IVR Development Pin
AmbiguousName7-Mar-11 8:35
AmbiguousName7-Mar-11 8:35 
QuestionRe: Small IVR Development Pin
Maximilien7-Mar-11 9:12
Maximilien7-Mar-11 9:12 
AnswerRe: Small IVR Development Pin
Chris Meech7-Mar-11 9:46
Chris Meech7-Mar-11 9:46 
GeneralRe: Small IVR Development Pin
Eytukan10-Mar-11 4:47
Eytukan10-Mar-11 4:47 
AnswerRe: Small IVR Development Pin
VBVoice9-Mar-11 4:41
VBVoice9-Mar-11 4:41 
GeneralRe: Small IVR Development Pin
AmbiguousName12-Mar-11 4:14
AmbiguousName12-Mar-11 4:14 
GeneralRe: Small IVR Development Pin
VBVoice14-Mar-11 3:42
VBVoice14-Mar-11 3:42 
Questionmemory mapped Pin
sarfaraznawaz7-Mar-11 1:35
sarfaraznawaz7-Mar-11 1:35 
AnswerRe: memory mapped Pin
Niklas L7-Mar-11 1:41
Niklas L7-Mar-11 1:41 
GeneralRe: memory mapped Pin
sarfaraznawaz8-Mar-11 20:37
sarfaraznawaz8-Mar-11 20:37 
GeneralRe: memory mapped Pin
Niklas L9-Mar-11 22:40
Niklas L9-Mar-11 22:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.