|
Hi, everyone!
Please look at the following codes,
--------
template <class ForwardIterator>
inline void seq_delete (ForwardIterator first, ForwardIterator last) {
while (first != last) delete *first++;
}
vector<char*> v;
char* c1 = new char[20]; strcpy (c1, "Tim");
char* c2 = new char[20]; strcpy (c2, "Charles");
v.push_back (c1); v.push_back (c2);
seq_delete (v.begin(), v.end() );
// vector v is destroyed at the end of scope
--------
I think that since we use new[], so we should use delete[] other
than delete in the following line.
--------
while (first != last) delete *first++;
--------
Or else, memory leak will occur.
Am I correct?
Thanks in advance,
George
|
|
|
|
|
Yes, you are correct. Since the vector is using what I would call an "unmanaged pointer", the vector has no way of knowing it needs to delete the memory.
For the most part, this is considered bad style since any time elements are removed from the vector, you have to manage the pointers. Thus you have things like boost's ref pointer or stl's string implementation that handles all the management for you. Many times, you can use this "bad style" to your advantage but there is always an associated risk. But like all programming, there are associated risks with everything. It is just a question of managing those risks.
EDIT: I commonly use this "bad style" in situations where the vector is part of another class and is wholly owned by that class. Thus risks are greatly minimized. For the most part, I would never use this "bad style" with something that was directly exposed to other classes or the main program. (i.e. public class members or a global vector.)
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
Hi, Tim buddie!
What means
--------
EDIT: I commonly use this "bad style" in situations where the vector is part of another class and is wholly owned by that class. Thus risks are greatly minimized. For the most part, I would never use this "bad style" with something that was directly exposed to other classes or the main program. (i.e. public class members or a global vector.)
--------
in your reply, can you show me an example?
I found the examples are better to make oneself understood when
expressing thoughts. I am a newbie.
Thanks in advance,
George
|
|
|
|
|
Trying to think of a simple one... *thinking*
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
Hi, Tim buddie!
What is your meaning? I can not get your ideas?
Thanks in advance,
George
|
|
|
|
|
No, I was just trying to think of a simple example.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
Thanks, Tim buddie!
I am waiting for your nice example.
George
|
|
|
|
|
In MFC we can use: CString::GetBuffer() or GetBufferSetLength()
- muck with string - then CString::ReleaseBuffer() to work directly with the internal buffer.
In STL we can use s.reserve( .. ); s.data(); which I assume will work, but how do we set the new size. Calling s.reserve() when we are done makes some sense, but how do we set the new size. My reference for reserve() says it may or may not resize the internal buffer, but it doesn't sound like it sets the length.
Neville Franks, Author of ED for Windows. www.getsoft.com
Make money with our new Affilate program
|
|
|
|
|
s.reserve(n) makes sure that the string length can grow to at least n characters without reallocations. It doesn't have effect to the actual string length. You can use s.resize() to set the string length. There's no equivalent to GetBuffer() /ReleaseBuffer() , you can't directy manipulate the characters in the internal buffer.
|
|
|
|
|
markkuk wrote:
There's no equivalent to GetBuffer()/ReleaseBuffer(), you can't directy manipulate the characters in the internal buffer.
Thanks, that's what I thought. This is a pity as you have to waist time and code copying an STL string to a C string and back again at times. eg. When you call a function which only accepts a C string as a parameter, and the callee has an STL string.
Thanks for your help.
Neville Franks, Author of ED for Windows. www.getsoft.com
Make money with our new Affilate program
|
|
|
|
|
I noticed WTL programs often have implementation in the header files, I guess this is the convention ?
I noticed the option in VS.NET project creation to create .cpp files, but by defualt it is not checked.
I guess I should conform and implement in header files, correct ?
R.Bischoff | C++
.NET, Kommst du mit?
|
|
|
|
|
Two reasons for that. First, with template classes, all the code has to be in a header file (well, technically that's not true, but most people do it that way). And second, when you use ClassView to add message handlers, it puts the code in the header file.
I personally hate this because it makes the header file a pain to maintain, and results in longer builds because you'll be changing header files all the time as you're developing.
--Mike--
The Internet is a place where absolutely nothing happens.
-- Strong Bad
1ClickPicGrabber - Grab & organize pictures from your favorite web pages, with 1 click!
My really out-of-date homepage
Sonork-100.19012 Acid_Helm
|
|
|
|
|
I personally hate this because it makes the header file a pain to maintain, and results in longer builds because you'll be changing header files all the time as you're developing.
That is the big flaw in the design. It can be a huge problem for large scale projects.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
Hello,
I created a COM DLL. (with VC++.NET but this is not important).
And created some assembly the has a function, that create instance from the
COM class, and call some function from the COM object.
When I call the functions of the assembly as simple as possible, from some
console app, this is work.!
But if I do it via remoting, it isn't work!! and give this exception:
Unhandled Exception: System.InvalidCastException: QueryInterface for
interface ScreenCamaraASM.IScreenCapturing failed.
Server stack trace:
at ScreenCamaraASM.CScreenCapturingClass.GetNumber()
at RCServerNP.RCServer.GetScreenPicture() in d:\mydocs\visual studio
projects
\remotecommunication\rcserver\rcserver.cs:line 24
at
System.Runtime.Remoting.Messaging.StackBuilderSink.PrivateProcessMessage(M
ethodBase mb, Object[] args, Object server, Int32 methodPtr, Boolean
fExecuteInC
ontext, Object[]& outArgs)
at
System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMes
sage msg, Int32 methodPtr, Boolean fExecuteInContext)
Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage
req
Msg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&
msgDa
ta, Int32 type)
at RCServerNP.RCServer.GetScreenPicture() in d:\mydocs\visual studio
projects
\remotecommunication\rcserver\rcserver.cs:line 21
at Testing.Class1.Main(String[] args) in D:\MyDocs\Visual Studio
Projects\Rem
oteCommunication\Testing\Class1.cs:line 36.
Please, any solution?
Thank's, Itay.
|
|
|
|
|
Hi, everyone!
Here is an example about the usage of
bind2nd. But I do not understand what the
function is doing. I have used MSDN to find
help, but still puzzled because there is only
a little of material on the topic.
Who can tell me what is the function doing in
the following example?
--------
vector<int> v;
// fill v with 4 6 10 3 13 2
int bound = 5;
replace_if (v.begin(), v.end(), bind2nd (less<int>(), bound), bound);
// v: 5 6 10 5 13 5
--------
Thanks in advance,
George
|
|
|
|
|
In this particular case bind2nd is function that uses comparison function (less by default) to compare any argument to value of 'bound'.
So for each element in v.begin() ... v.end() range replace_if calls bind2nd and if true returned it replaces element value with value of 'bound'
Your example:
vector v;
// fill v with 4 6 10 3 13 2
int bound = 5;
replace_if (v.begin(), v.end(), bind2nd (less(), bound), bound);
//////////////////////////////////////////
another way(not the best):
const int bound = 5;
for(vector::size_type i = 0, size = v.size();i< size;++i)
{
if(v[i] <= bound)
v[i] = bound;
}
+
|
|
|
|
|
Thanks, AlexO buddie!
George
|
|
|
|
|
Hello guys,
Where can I find a good implementation of IClassFactory2 , whitout using that DECLARE_CLASSFACTORY2(CMyLicense) macro, to perform licensing capabilities to my software?
Thanks in advance,
Raphael Amorim Dantas Leite
VC++, VB, Java, .NET and eMbedded Programmer
|
|
|
|
|
I want to use the internet explorer inside my application. This works fine:
AtlAxWinInit();
HWND hwndChild=::CreateWindow( "AtlAxWin",
"Shell.Explorer.1",
WS_CHILD|WS_VISIBLE,
0,0,0,0,
hwnd,NULL,
::GetModuleHandle(NULL),
NULL);
IUnknown *pUnk=NULL;
AtlAxGetControl(hwndChild,&pUnk);
CComPtr<IWebBrowser2> spBrowser;
pUnk->QueryInterface(IID_IWebBrowser2,(void**)&spBrowser);
if(spBrowser)
spBrowser->put_Visible(VARIANT_TRUE);
But, I want this to be more abstract. How can I use the interface IDispatch instead.
My attempt to abstract it, returned a DISP_E_UNKNOWNNAME while calling GetIDsOfNames with "Visible" (it IS a member of IWebBrowser2). It has to be something like that...
using: [VISUAL STUDIO 6.0] [WIN98/2]
using: [VISUAL STUDIO 6.0] [WIN98/2]
|
|
|
|
|
It is easily possible. I never tried it on IWebBrowser2 , but it should work on any IDispatch derived interface.
For your GetIDsOfNames , you have to check, if all flags was set properly.
Anyway, there is a helper classes in ATL to doing dispatch calls, if you don't want to program it yourself as a mental excersise.
CComDispatchDriver dispDrv( ipDispInterface );<br />
<br />
dispDrv.PutPropertyByName( bstrName, &variantValue );<br />
dispDrv.Invoke0( bstrName, NULL );
etc...
|
|
|
|
|
I've tried that CComDispatchDriver. <<atlcom.h>> is needed for that.
Now, during compilation, I receive errors from atlcom.h. like these:
VC98\ATL\INCLUDE\atlcom.h(2945) : error C2065: '_Module' : undeclared identifier
VC98\ATL\INCLUDE\atlcom.h(2945) : error C2228: left of '.Lock' must have class/struct/union type
...
My includes are like these:
#pragma comment(lib,"atl.lib")
#include <<atldef.h>>
#include <<atlbase.h>>
#define _ATL_DLL_IMPL
#include <<atliface.h>>
#include <<exdisp.h>>
#include <<atlcom.h>>
What have I done now?
But, frankly, all of the params in my call to getidsfromnames are valid (used it already to control an excel application). IWebBrowser2 doesn't seem to have "Visible", "Quit", ... but I saw it using the typeview tool.
using: [VISUAL STUDIO 6.0] [WIN98/2]
|
|
|
|
|
the _Module refers to the current DLL (module). I no way how to test the include stuff on my compiler now, but generally somewhere in your code is extern CComModule _Module;
and the #include <atlcom.h> must stay behind that definition from the point of view of compiler (this is for VC6, I assume you have)
But the second part is really weird. Fortunatelly I had some project with WebBrowser manipulation, so I fired it up and tested the following code:
CComPtr<IWebBrowser2> m_spBrowser;<br />
CComDispatchDriver drva( m_spBrowser );<br />
_variant_t vtOut;<br />
drva.GetPropertyByName( L"visible", &vtOut );
on the end I had in vtOut vt=VT_BOOL value=VARIANT_TRUE
then at least on my IE6 the property is included = the way you are trying to go is right.
I would suggest to play with the headers a bit, or if it will take more than 10 minutes look to the srcs of the CComDispatchDriver and cut only the parts you need (but duplicating code is really the last chance solution )
|
|
|
|
|
I've included the atlcom.h just above my function and it works now. BIG TNX
Two remarks however...
1: you used L"visible" instead of L"Visible". Aren't those parameters case sensitive?
2: I've found a page on msdn about CComDispatchDriver, but it's not the dispatcher you showed me. It has GetProperty(), PutProperty() and Release(). That's all. So, which one are you talking about? Is it an extended dispatcher or what?
This is the link I've found http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_atl_ccomdispatchdriver.asp[^]
... and one again, thank you. (but the failure in getIdsFromNames(...) remains very strange )
using: [VISUAL STUDIO 6.0] [WIN98/2]
|
|
|
|
|
1. these should not be case sensitive, at least the IDispatch requires that (else the VB Script will be also case sensitive). In fact developer can change it while implementing the IDispatch in his object, but I don't believe that Microsoft will break it's own rulez.
2. probably this is some older version than my, I am using the ATL7, so might be that this class is updated.
|
|
|
|
|
My euphoria came to quick. It still doesn't work.
the 0x80020006 error is still there...
using: [VISUAL STUDIO 6.0] [WIN98/2]
|
|
|
|
|