|
Hello,
I've made a function that uses a BSTR, and returns it type casted as a CString.
Something like that:
CString Myfunc()<br />
{<br />
BSTR a;<br />
somefunc(&a);<br />
return (CString)a;<br />
}
And then I use that in order to set the returned CString as the text of a CEdit:
CEdit B;<br />
B.SetwindowtextA(MyFunc());
Is there any way that this could be generating a memory leak?
As always thank you in advance.
|
|
|
|
|
That code will leak memory.
Steve
|
|
|
|
|
OK, how should I avoid it?
Thank you for your feedback.
|
|
|
|
|
It could - depending on how the BSTR is managed... BSTR s normally transfer ownership, so when somefunc(...) places a BSTR into the passed variable, it likely transferred ownership of that copy of the BSTR . You would have to free that BSTR to prevent a leak (note that this kind of leak may not be reported by the debugger). Using a CComBSTR will take care of freeing it for you, BTW. So basically, if you are handing the BSTR correctly, your code will leak, and if you are handling it incorrectly, you might not leak!
If all you are doing is putting that value into an edit control, you can take advance of the fact that a BSTR is a wide string behind the scenes and can directly call SetWindowTextW( HWND, LPCWSTR ) to copy the data in the BSTR directly to the edit control, saving the overhead of the temporary CString . May not be a big difference in your application, but efficiency a good habit to get into IMHO.
{
CComBSTR bstrSomeValue;
CEdit B;
somefunc( &bstrSomeValue );
::SetWindowTextW( B.m_hWnd, bstrSomeValue );
} Peace!
-=- James Please rate this message - let me know if I helped or not!<HR> If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! See DeleteFXPFiles
|
|
|
|
|
Hi
How to create two splitbars in VC++?
|
|
|
|
|
Using MFC CSplitterWnd?
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
|
|
|
|
|
please help me this code logic
where make I the mistake this code
It is running console wizard in C++ but I trasformation to form enviroment borland C++ 6.0
i hope to your find to my mistake
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#include "Unit1D.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
#define MAXPOP 25
struct gene {
int alleles[4];
int fitness;
float likelihood;
// Test for equality.
operator==(gene gn) {
for (int i=0;i<4;i++) {
if (gn.alleles[i] != alleles[i]) return false;
}
return true;
}
};
class CDiophantine {
public:
CDiophantine(int, int, int, int, int);
int Solve();
gene GetGene(int i) { return population[i];}
protected:
int ca,cb,cc,cd;
int result;
gene population[MAXPOP];
int Fitness(gene &);
void GenerateLikelihoods();
float MultInv();
int CreateFitnesses();
void CreateNewPopulation();
int GetIndex(float val);
gene Breed(int p1, int p2);
};
CDiophantine::CDiophantine(int a, int b, int c, int d, int res) : ca(a), cb(b), cc(c), cd(d), result(res) {}
int CDiophantine::Solve() {
int fitness = -1;
srand((unsigned)time(NULL));
for(int i=0;i<maxpop;i++) {
="" for="" (int="" j="0;j<4;j++)" population[i].alleles[j]="rand()" %="" (result="" +="" 1);
="" }
="" }
="" if="" (fitness="=" createfitnesses())="" return="" fitness;
="" int="" iterations="0;
" while="" !="0" ||="" <="" 50)="" generatelikelihoods();
="" createnewpopulation();
=""
="" iterations++;
="" -1;
}
int="" cdiophantine::fitness(gene="" &gn)="" float="" total="ca" *="" gn.alleles[0]="" cb="" gn.alleles[1]="" cc="" gn.alleles[2]="" cd="" gn.alleles[3];
="" gn.fitness="abs(total" -="" result);
}
int="" cdiophantine::createfitnesses()="" avgfit="0;
" fitness="0;
" for(int="" i="0;i<MAXPOP;i++)" 0)="" i;
="" 0;
}
="" cdiophantine::multinv()="" sumi="0;
" k="10;
" {
="" k;
="" sumi;
}
void="" cdiophantine::generatelikelihoods()="" multinv="MultInv();
" last="1;
" aktr="1;
" ((1="" (population[i].fitness)="" multinv)="" 100);
="" last+="last;
" }
}
int="" cdiophantine::getindex(float="" val)="" (last="" &&="" val="" else="" 4;
}
gene="" cdiophantine::breed(int="" p1,="" p2)="" crossover="rand()" 3+1;
="" first="rand()" 100;
="" gene="" child="population[p1];
" initial="0," final="3;
" (first="" {="" child.alleles[i]="population[p2].alleles[i];
" (rand()="" 101="" 5)="" child;
}
void="" cdiophantine::createnewpopulation()="" temppop[maxpop];
="" parent1="0," parent2="0," while(parent1="=" population[parent1]="=" population[parent2])="" 101));
="" (++iterations=""> 25) break;
}
temppop[i] = Breed(parent1, parent2); // Create a child.
}
for(int i=0;i<maxpop;i++) population[i]="temppop[i];
}
//---------------------------------------------------------------------------
__fastcall" tform1::tform1(tcomponent*="" owner):="" tform(owner)
{
}
="" ---------------------------------------------------------------------------
void="" __fastcall="" tform1::button1click(tobject="" *sender)
{
int="" solve();
int="" fitness;
edit1-="">Text=StrToInt(fitness);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
CDiophantine dp(1,2,3,4,30);
gene gn;
int ans;
ans = dp.Solve();
if (ans == -1) {
//cout << "No solution found." << endl;
} else {
gene gn = dp.GetGene(ans);
Memo1->Text= gn.alleles[0];
}
}
//---------------------------------------------------------------------------
|
|
|
|
|
oz-gur wrote: i hope to your find to my mistake
i hope nobody will, and that you'll learn to use your debugger and learn also how to ask a question on a forum propery
|
|
|
|
|
I keep reading articles which say virtual function has overhead on performance, but I'd like to know how much is it?
For example, the MFC framework, if all the functions inside are virtual, how much will MFC's performance decrease, for the current 1GHz/512M PC hardware ?
|
|
|
|
|
Yes there is overhead on performance. But it is very important to understand the virtue/performace relation when using virtual functions. I mean, one should avoid using virtual functions in low level operations such as accessing each character in a sequence. But for not so heavy and/or intensive operations you can [and sometimes should] ignore performance issues.
As for MFC, a good candidate area of using virtual functions is message handlers. But MFC refused virtual functions and used message maps. Really, so many classes with so many virtual functions would indeed decrease speed and increase memory usage.
--
=====
Arman
|
|
|
|
|
Dear all
How I can make a tooltip for a button in Visual C++ 6. The message should be dynamic. I want to change it on the fly(at run time).
Best regards
|
|
|
|
|
|
Thanks
It was what I need.
|
|
|
|
|
hi guys,
DO anyone know what is the reason that i can only play avi file which is original in Microsoft, but when i import a avi format from external i cannot play it..
How can i play my avi in animation control by displaying it full on the screen?
Pls advice, am using VC++ 2003 MFC.
Thanks ..
|
|
|
|
|
I think Animation Controls [^] will be helpful for you and also it has description of this control
|
|
|
|
|
Hi
thanks white sky..dun get what u mean.
The avi file i play can't be display full on the animation control box.
SOme avi file cannot even display on the animation control
Pls advice..
Thanks !!
|
|
|
|
|
I said before use of MCI functions but you said I can't you them Animation control can show avi file that are standard
|
|
|
|
|
I also said before you'll probably need to use some other method (Windows multimedia, for example)
to get the flexibility you need. An animation control isn't going to do it.
Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
|
|
|
|
|
Hi guys,
How do u write using MCI to open and open a avi file??
do i need to specify any box etc animation ctrl box??
Tks
|
|
|
|
|
You can play the AVI to a window - Playing a Device[^]
Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
|
|
|
|
|
Hi guys,
I have tried to add a bitmap on my picture control using the add resource method.
this is the code that i place on my button the display the bitmap.
void CPictureDlg::OnBnClickedOk()
{
// TODO: Add your control notification handler code here
CBitmap Bmp;
Bmp.LoadBitmap(IDB_BITMAP1);
m_pic.SetBitmap(Bmp);
}
However no picture was display on the picture control please advice..
Thanks in advance.
|
|
|
|
|
Who is actually drawing the picture?
How does it know the picture has changed?
Try invalidating the window (or button) that is supposed to display the picture.
INTP
"Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
|
|
|
|
|
Hi,
There is no drawing of picture involved,just one click on the button to display the bitmap.
However seem like the my source code is not working..
Pls advice.
|
|
|
|
|
Set Bitmap to true on the property window of Button and use of global CBitmap,now what happens?
|
|
|
|
|
Hi all:
Say:
char *string = NULL, *buffer = NULL;
int string_len = 0;
fgets(buffer);
string_len = strlen(buffer);
buffer[string_len] = '\0';
string = malloc(string_len+1);
strncpy(string, buffer, string_len);
return string;
Is my codes correct?
Thank you
|
|
|
|