I have written a MFC dll to interact with an API and it is run by the target program. the first 100 bars are drawn properly but then the program ceases to operate. I am guessing I have a memory leak but I probably just dont understand what I am doing as I am quite new to programming MFC GDI. Any help is greatly appreciated. Here is my code that causes the trouble...

The variables coming in are commented out to facilitate testing and so it would be displaying the same bars over and over because of this. it worrks for 100 bars, then crashes.


void MainDlg::SS_UpdateChart()
{
// the chart plot is completely recalculated each time a new bar is shown
// 300 is plottable height
// 400 is plottable width, 39 bars
int displayChartHeight = 300;

// variables used to display the chart
double displayPriceRangeHigh = 0;
double displayPriceRangeLow = 10000;
double pixelsPerDollar = 0;





CDC * myDC; // the picture box context

CPen * oldPen;

CBrush * oldBrush;


// set up the picture box
myDC = GetDlgItem(IDC_STATICPIC1)->GetDC(); // get the item context

oldPen = (CPen *) myDC->SelectStockObject(WHITE_PEN); // initialize
myDC->SelectObject(oldPen);

oldBrush = (CBrush *) myDC->SelectStockObject(WHITE_BRUSH); // initialize
myDC->SelectObject(oldBrush);




// the data for each bar is loaded into the chart array
// for each movement of the bars back the range is recalculated
// and the data is reformulated and converted to fill the chart bars

// the current bar in is bar 40 to stage it
chartBar[40].open = 15;//currentBarOpen;
chartBar[40].high = 18;//currentBarHigh;
chartBar[40].low = 12;//currentBarLow;
chartBar[40].close = 13;//currentBarClose;
chartBar[40].volume = 0;//currentBarVolume;
chartBar[40].pressure = 0;//currentBarAveragePressure;



// the displayed range of prices
// the range of chart is determined by the highest and lowest of all the bars
// being displayed at the time from second bar to the current bar being built
// only include the range of the bars that exist when first starting the display

// get the range on bars 2 to 40

for( int a = 2; a <= 40; a = a + 1)
{
if ((chartBar[a].high != 0) && (chartBar[a].high != NULL))
{
if (displayPriceRangeHigh < chartBar[a].high)
{
displayPriceRangeHigh = chartBar[a].high;
}
}

if ((chartBar[a].low != 0) && (chartBar[a].low != NULL))
{
if (displayPriceRangeLow > chartBar[a].low)
{
displayPriceRangeLow = chartBar[a].low;
}
}
}




// to avoid division by zero or null
if ((displayPriceRangeHigh < 1) || (displayPriceRangeHigh == NULL))
{
displayPriceRangeHigh = 1;
displayPriceRangeLow = 1;
}
if ((displayPriceRangeLow < 1) || (displayPriceRangeLow == NULL))
{
displayPriceRangeHigh = 1;
displayPriceRangeLow = 1;
}



// get pixels per dollar
pixelsPerDollar = displayChartHeight / (displayPriceRangeHigh - displayPriceRangeLow);



// move bar data back one column and move current bar from 40 into bar 39
// this makes bars 1 to 39 current for displaying

for( int a = 2; a <= 40; a = a + 1)
{
chartBar[(a - 1)].open = chartBar[a].open;
chartBar[(a - 1)].high = chartBar[a].high;
chartBar[(a - 1)].low = chartBar[a].low;
chartBar[(a - 1)].close = chartBar[a].close;
chartBar[(a - 1)].volume = chartBar[a].volume;
chartBar[(a - 1)].pressure = chartBar[a].pressure;
}



// clear the display for the new bars
CBrush* blackBrush = new CBrush;

blackBrush->CreateSolidBrush(blackColor);

//oldBrush = myDC->SelectObject(blackBrush);
myDC->SelectObject(blackBrush);

myDC->FillRect(new CRect (0, 0, 460, 330), blackBrush);

DeleteObject(blackBrush);

myDC->SelectObject(oldBrush);


// find colors to display for each bar space 1 column at a time
// divide the 100% into the increments of the display

int barPosition;
int barOpen;
int barHigh;
int barLow;
int barClose;


for( int a = 1; a <= 39; a = a + 1)
{

barPosition = a;

barOpen = ((chartBar[a].open - displayPriceRangeLow) * pixelsPerDollar);
barHigh = ((chartBar[a].high - displayPriceRangeLow) * pixelsPerDollar);
barLow = ((chartBar[a].low - displayPriceRangeLow) * pixelsPerDollar);
barClose = ((chartBar[a].close - displayPriceRangeLow) * pixelsPerDollar);

if (chartBar[a].open > 1)
{


COLORREF myColor;
int xSpacing = 10;
int xWidth = 5;
int xMiddle = 2;
int chartWidth = 400; // 60 on right side for moving close price, 39 bars displayed at once
int chartHeight = 300; // 15 on top and 15 on bottom
int chartBottom = 315; // 315, 15 is the top
int bodyTop; // top of the body
int bodyBottom; // bottom of the body


// items are the amounts up from the bottom
// get correct color for bar and get body top and bottom
if (barOpen < barClose) // up bar
{
myColor = greenColor;

bodyBottom = barOpen;
bodyTop = barClose;
}

if (barOpen > barClose) // down bar
{
myColor = redColor;

bodyBottom = barClose;
bodyTop = barOpen;
}

if (barOpen == barClose) // same price bar
{
myColor = yellowColor;

bodyBottom = barClose;
bodyTop = barClose;
}










// chart veiwing area is 200 high by 400 long
// show bars as 5 wide with 5 space inbetween them


CBrush* mySolidBrush = new CBrush();
mySolidBrush->CreateSolidBrush(myColor);
//oldBrush = myDC->SelectObject(mySolidBrush);
myDC->SelectObject(mySolidBrush);

// body
myDC->FillRect(new CRect ((barPosition * xSpacing),(chartBottom - barOpen),((barPosition * xSpacing) + xWidth),(chartBottom - barClose)), mySolidBrush);

DeleteObject(mySolidBrush);

myDC->SelectObject(oldBrush);


CPen* myThinPen = new CPen();//(PS_SOLID(1, myColor));
myThinPen->CreatePen(PS_SOLID,1, myColor);
//oldPen = myDC->SelectObject(&myThinPen);
myDC->SelectObject(myThinPen);

// upper shadow
myDC->MoveTo(((barPosition * xSpacing) + xMiddle), (chartBottom - barHigh));
myDC->LineTo(((barPosition * xSpacing) + xMiddle), (chartBottom - bodyTop));

// lower shadow
myDC->MoveTo(((barPosition * xSpacing) + xMiddle), (chartBottom - barLow));
myDC->LineTo(((barPosition * xSpacing) + xMiddle), (chartBottom - bodyBottom));

DeleteObject(myThinPen);

myDC->SelectObject(oldPen);



}

}

DeleteObject(myDC);

DeleteObject(oldBrush);

DeleteObject(oldPen);

//ReleaseDC(myDC);



dayBarCount = dayBarCount + 1; // bars built so far this session

} // end void MainDlg::SS_UpdateChart()