How to release memory under following situation? - macos

Below is some core graphics code..
CGColorRef colorRefArray[MAGIC_NUM];
for (int i = 0; i < MAGIC_NUM ; i++)
{
...
colorRefArray[i] = CreateColor(colorValueForEachColor, numberofcomp);
}
colorRefArray already has memory and CreateColor(); will again create a memory and it leads to memory leak.
How do I avoid this situation?
One possible thought I have is
CGColorRef colorRefArray[MAGIC_NUM];
for (int i = 0; i < MAGIC_NUM ; i++)
{
...
CGColorRef colorref = CreateColor(colorValueForEachColor, numberofcomp);
colorRefArray[i] = colorref;
CFRelease(colorref);
}
Is this approach correct?

No it's not. You're immediately releasing the color you created. The correct approach would be this:
CGColorRef colorRefArray[MAGIC_NUM];
for (int i = 0; i < MAGIC_NUM ; i++)
{
...
colorRefArray[i] = CreateColor(colorValueForEachColor, numberofcomp);
}
//Use your colors
//Now release them
for (int i = 0; i < MAGIC_NUM ; i++)
{
CFRelease(colorRefArray[i]);
}

No, because then colorRefArray will be filled with invalid pointers.
Try using a CFMutableArray instead of a raw C array. Then you only have to worry about the reference to the array, as it will own the colours for you:
CFArrayRef CopyColorArray(void) {
CFMutableArrayRef colorRefArray = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
if (colorRefArray) {
for (int i = 0; i < MAGIC_NUM ; i++) {
...
CGColorRef colorref = CreateColor(colorValueForEachColor, numberofcomp);
if (colorref) {
CFArrayAppendValue(colorRefArray, colorref);
CFRelease(colorref);
}
}
}
return colorRefArray;
}

Related

Find unique number in array in fill list with this unique numbers using for loops

I'm trying to use for loop inside for loop but it doesn't working i also tried while loop but...
int[] numArray = new int[10] {1,2,2,3,3,4,4,5,5,9};
List<Int32> uNum = new List<Int32>();
/*Random rnd = new Random();
for (int i = 0; i < numArray.Length; i++)
{
int randomNumber = rnd.Next(0, 10);
numArray[i] = randomNumber;
}*/
for (int i = 0; i < numArray.Length; i++)
{
if (numArray[i] != numArray[i])
{
for (int j = 0; j < numArray.Length-1; j++)
{
if (numArray[i] != numArray[j])
{
uNum.Add(numArray[i]);
}
}
}
}
You have an error in this line:
if (numArray[i] != numArray[i])
This condition will always return False because a number is always equal to itself.
Do something like this:
for (int i=0; i<numArray.Length; i++)
{
int j;
for (j=0; i<numArray.Length; j++){
if (i != j){
if (numArray[i] != numArray[j])
{
uNum.Add(numArray[i]);
}
}
}
}
int[] numArray = new int[10];
List<Int32> uNum = new List<Int32>();
Random rnd = new Random();
for (int i = 0; i < numArray.Length; i++)
{
int randomNumber = rnd.Next(0, 10);
numArray[i] = randomNumber;
}
for (int i = 0; i < numArray.Length; i++)
{
int num = numArray[i];
int count = 0;
for (int j = 0; j < numArray.Length; j++)
{
if (numArray[j] == num)
{
count++;
}
}
if (count == 1)
{
uNum.Add(num);
}
}

MFC Rich Edit TOM -- InsertTable; cannot add more than one column?

May you please help, I can only insert a table with any number of rows, but not columns; I get an E_INVALIDARG when cols > 1 in ITextRange2::InsertTable(cRow,cCol,AutoFit)??
IRichEditOle* pREOle = NULL; // rCtrl is my derived CRichEditView in MFC
HRESULT hr = rCtrl.SendMessage(EM_GETOLEINTERFACE, 0,
(LPARAM)&pREOle);
CComQIPtr<ITextDocument2> pITextDoc;
hr = pREOle->QueryInterface(__uuidof(ITextDocument2), (void*)&pITextDoc);
CComPtr<ITextRange2> range;
pITextDoc->Range2(start, start, &range);
hr = range->InsertTable(3,3, 1); // get hr = E_INVALIDARG here
Below is code, that will insert a table for editing rows or inserting a new table. The function is declared in my derived view class CMHMView. I added fields such as ITextRange2* m_ptr2, HRESULT m_hr.
Initialization is done in my CRichEditView::OnInitialUpdate:
CRichEditCtrl& re = GetRichEditCtrl();
m_pREOle = re.GetIRichEditOle();
m_hr = m_pREOle->QueryInterface(IID_PPV_ARGS(&m_ptd2));
This is the function I use to add the table(s)
void CMHMView::InsertTableRow(long index, long& start, long& end, long ncells,
long cellwidths[], long skip, LPCTSTR* sText, BOOL bAppend){
if (SUCCEEDED(m_hr))
{
long lDelta{};
CComPtr<ITextFont> pFont;
m_hr = m_ptr2->GetFont(&pFont);
CComPtr<ITextRow> pRow;
CComPtr<ITextFont> pFontDuplicate;
m_hr = m_ptr2->GetRow(&pRow);
m_hr = pFont->GetDuplicate(&pFontDuplicate);
if (pRow && pFontDuplicate)
{
CComPtr<ITextPara> pPara, pParaDuplicate;
m_ptr2->GetPara(&pPara);
pPara->GetDuplicate(&pParaDuplicate);
pFontDuplicate->SetName(CComBSTR(L"Courier New"));
pFontDuplicate->SetSize(12.0f);
m_ptr2->MoveStart(tomTable, start, &lDelta);
m_ptr2->Collapse(tomEnd);
if (bAppend)
{
pRow->SetCellCount(ncells);
for (int i = 0; i < ncells; i++)
{
pRow->SetCellIndex(i);
pRow->SetCellWidth(cellwidths[i]);
}
pRow->Insert(1);
m_ptr2->SetIndex(tomTable, index, 0);
for (int i = 0; i < skip; i++)
{
if (i > 0)
m_ptr2->MoveStart(tomCell, 1, &lDelta);
m_ptr2->SetText(CComBSTR(L""));
}
for (int i = 0; i < ncells; i++)
{
if (i > 0)
m_ptr2->MoveStart(tomCell, 1, &lDelta);
m_ptr2->SetText(CComBSTR(sText[i]));
m_ptr2->SetFont(pFontDuplicate);
m_ptr2->SetPara(pParaDuplicate);
}
m_ptr2->EndOf(tomTable, tomMove, &lDelta);
m_ptr2->GetEnd(&end);
}
else
{
m_ptr2->SetText(CComBSTR(L"\r"));
m_ptr2->Collapse(tomEnd);
pRow->SetCellCount(ncells);
for (int i = 0; i < ncells; i++)
{
pRow->SetCellIndex(i);
pRow->SetCellWidth(cellwidths[i]);
/*pRow->SetCellMergeFlags(tomHStartCell)*/
}
pRow->Insert(1);
m_ptr2->SetIndex(tomTable, index, 0);
for (int i = 0; i < ncells; i++)
{
if (i > 0)
m_ptr2->MoveStart(tomCell, 1, &lDelta);
m_ptr2->SetText(CComBSTR(sText[i]));
m_ptr2->SetFont(pFontDuplicate);
m_ptr2->SetPara(pParaDuplicate);
}
m_ptr2->EndOf(tomTable, tomMove, &lDelta);
m_ptr2->GetEnd(&end);
}
}
}
m_pREOle->Release();}
I hope this will help others!

Create HBitmap in IExtractImage

I'm developping a thumbnail creator as a shell extension.
To do that, I choosed to implement interface IExtractImage.
My dll is loaded and correctly called, but, the thumbnail is always black, instead of being red.
What am I missing?
class MyShellPreview : public IExtractImage, IPersistFile
// set by IExtractImage::GetLocation
SIZE m_size;
IFACEMETHODIMP Extract(HBITMAP *phBmpImage)
{
size_t size = m_size.cx * m_size.cy * 3;
// alloc buffer
BYTE *buffer = (BYTE*)malloc(size);
// fill buffer
for (k = i = 0; i < m_size.cx; ++i)
{
for (j = 0; j < m_size.cy; ++j, ++k)
{
buffer[k] = 128;
buffer[k+1] = 0;
buffer[k+2] = 0;
}
}
*phBmpImage = CreateBitmap(m_size.cx, m_size.cy, 3, 8, buffer);
free(buffer);
return S_OK;
}
};
I know that for performance reason, I should use CreateCompatibleBitmap and SetDIBits, but I'm not sure from where I should get an HDC.
As pointed on comments, the solution was to use:
32 bits images
1 color plane
class MyShellPreview : public IExtractImage, IPersistFile
// set by IExtractImage::GetLocation
SIZE m_size;
IFACEMETHODIMP Extract(HBITMAP *phBmpImage)
{
size_t size = m_size.cx * m_size.cy * 4;
// alloc buffer
BYTE *buffer = (BYTE*)calloc(size, 1);
// fill buffer
for (k = i = 0; i < m_size.cx; ++i)
{
for (j = 0; j < m_size.cy; ++j, k+=4)
{
buffer[k] = 128;
}
}
*phBmpImage = CreateBitmap(m_size.cx, m_size.cy, 1, 24, buffer);
free(buffer);
return S_OK;
}
};

Simpler way to rewrite this loop

Is there a simpler way to rewrite this loop? Something with less code.
Any help is appreciated.
for(int i=0; i< 50; i++){
if(i>=0 && i<10){
method(arr[0]);
}
if(i>=10 && i<20){
method(arr[1]);
}
if(i>=20 && i<30){
method(arr[2]);
}
if(i>=30 && i<40){
method(arr[3]);
}
if(i>=40 && i<50){
method(arr[4]);
}
}
You could use this approach:
for(int i = 0; i < 50; i++){
int index = i == 0 ? 0 : (int)Math.floor(i / 10);
method(arr[index]);
}
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < 10; j++) {
method(arr[i]);
}
}
You could use a function to increase readability:
for (int i = 0; i < arr.length; i++) {
method10(arr[i]);
}
I would probably rewrite it using the modular function and a counter. This should help reduce the code significantly.
int index;
int count = 0;
for(int i=0; i< 50; i++){
index = i%10;
if(index==0){
count++;
}
method(arr[count-1]);
}
Use else:
for (i=0; i<50: i++) {
if (i<10) {
method(arr[0]);
} else if (i<20) {
method(arr[1]);
} else if (i<30) {
method(arr[2]);
} else if (i<40) {
method(arr[3]);
} else {
method(arr[4]);
}
}
For one liner you can use short if statement. Where the FALSE checks another condition
for (i=0; i<50: i++) {
(condition) ? TRUE : FALSE
}

Backpropagation learns for one dataset but fails at multiple datasets

Having an issue in my neural network where the error on the inputs gets enormously small (in the negative thousands). The network can learn one training set (ie 1+3=4) and will output four with inputs 1 and 3 but cant learn the generel pattern from larger datasets. My friend has taken a look at it and can't see the issue. Any help appreciated.
for (int j = 0; j <3000; j++)
{
for (int i = 0; i < tr_inp.Length; i++)
{
nn.inputs = tr_inp[i];
nn.desired = tr_out[i];
nn.FeedForward(tr_inp[i]);
nn.Backpropagate(tr_out[i]);
}
training loop,
public void FeedForward(double[] inputs)
{
this.inputs = inputs;
//set inputs outputs to the input weight,
for (int i = 0; i < nodes[0].Count; i++)
{
nodes[0][i].output = nodes[0][i].weights[0];
}
//set hidden layers outputs to dot product
for (int i = 0; i < nodes[1].Count; i++)
{
double sum = 0;
for (int j = 0; j < nodes[1][i].weights.Length; j++)
{
sum += nodes[1][i].weights[j] * nodes[0][j].output;
}
nodes[1][i].output = Normalization.Logistic(sum);
}
for (int i = 0; i < output; i++)
{
double sum = 0;
for (int j = 0; j < hidden; j++)
{
sum += nodes[2][i].weights[j] * nodes[1][j].output;
}
nodes[2][i].output = Normalization.Logistic(sum);
}
}
public void initilizeError()
{
for (int j = 0; j < hidden; j++)
{
nodes[1][j].error = 0;
}
for (int j = 0; j < input; j++)
{
nodes[0][j].error = 0;
}
}
public void Backpropagate(double[] desired)
{
#region error calculations
this.desired = desired;
for (int j = 0; j < output; j++)
{
nodes[2][j].error = (desired[j] - nodes[2][j].output);
}
for (int j = 0; j < hidden; j++)
{
// nodes[1][j].error = 0;
}
for (int i = 0; i < output; i++)
{
for (int j = 0; j < hidden; j++)
{
nodes[1][j].error += nodes[2][i].weights[j] * nodes[2][i].error;
}
}
for (int j = 0; j < input; j++)
{
// nodes[0][j].error = 0;
}
for (int i = 0; i < hidden; i++)
{
for (int j = 0; j < input; j++)
{
nodes[0][j].error += nodes[1][i].weights[j] * nodes[1][i].error;
}
}
#endregion
#region Backpropagation
for (int i = 0; i < input; i++)
{
var Dx = Normalization.Dx_Logistic(nodes[0][i].output);
for (int j = 0; j < input; j++)
{
nodes[0][i].weights[0] += nodes[0][i].error * inputs[j]*Dx;
}
}
for (int i = 0; i < hidden; i++)
{
var Dx = Normalization.Dx_Logistic(nodes[1][i].output);
for (int j = 0; j < input; j++)
{
nodes[1][i].weights[j] += nodes[1][i].error * nodes[0][j].output * Dx;
}
}
for (int i = 0; i < output; i++)
{
var Dx = Normalization.Dx_Logistic(nodes[2][i].output);
for (int j = 0; j < hidden; j++)
{
nodes[2][i].weights[j] += nodes[2][i].error * nodes[1][j].output * Dx;
}
}
#endregion
}
}

Resources