|
|
Have a look at the tutorials on this site.
Typically you just declare the STL containers on the stack.
vector<int> vec;
vector<MyClass> vec;
You can add simple types and objects to the vector and you don't need to worry about memory management <i>unless </i>those individual simple types or objects were themselves dynamically allocated.
Kevin
|
|
|
|
|
template <typename T>
struct Deleter { void operator(T* pObj) { delete pObj; } };
template <typename Collection>
void DeleteAllObjects(Collection& coll)
{
std::for_each(coll.begin(), coll.end(), Deleter<Collection::value_type>());
coll.clear();
};
--
Based on a True Story
|
|
|
|
|
Yes, I have a similar utility functor myself, but I haven't done any C++ for 18 months. I'm in the .NET universe now. I guess I may do some C++/CLI some time.
Kevin
|
|
|
|
|
Problem with firing events
I created an ATL/COm dll. and I'm testing it with a VB client. The client behaves fine in the IDE (VB6.0)
But once we create the .exe and run it, It crashes the application when an Event is fired from the DLL
I narrowed down to the point where it actually crashes. It's failing when we say,
this->Fire_testEvent()
inside the implementation,
it fails here.
<br />
HRESULT Fire_testEvent()<br />
{<br />
CComVariant varResult;<br />
T* pT = static_cast<T*>(this);<br />
int nConnectionIndex;<br />
int nConnections = m_vec.GetSize();<br />
<br />
for (nConnectionIndex = 0; nConnectionIndex < nConnections; nConnectionIndex++)<br />
{<br />
pT->Lock();<br />
CComPtr<IUnknown> sp = m_vec.GetAt(nConnectionIndex);<br />
pT->Unlock();<br />
IDispatch* pDispatch = reinterpret_cast<IDispatch*>(sp.p);<br />
if (pDispatch != NULL)<br />
{<br />
VariantClear(&varResult);<br />
DISPPARAMS disp = { NULL, NULL, 0, 0 };<br />
</pre>pDispatch->Invoke(0x6, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &disp, &varResult, NULL, NULL);
</pre> }<br />
}<br />
return varResult.scode;<br />
<br />
}<br />
Please help me .. what could be wrong.. It's killing me.
<marquee scrollamount="1" scrolldelay="1" direction="up" height="10" step="1">--[ ]--
[My Current Status]
I dont know why the hell the script for voting 5 is disabled only for me??
|
|
|
|
|
Are you by any chance firing the event from a secondary thread?
If you want to fire events from other threads than the one that created the server, you have to marshal the sink interface pointer.
Have a look at Michael Lindig's solution here[^], I've used it quite a lot.
If you're not using multithreading, post again and describe your app further.
--
Rog
It's supposed to be hard, otherwise anybody could do it!
Regarding CodeProject: "resistance is pointless; you will be assimilated"
|
|
|
|
|
Yup exactly, I fire it from a worker thread. So that's the culprit? hmm.. thanks for your valuable help. Thanks you so much. I'll go through the link. and comeback here again. But Roger I have a question. How does it work, when I test the application in the IDE ? and what makes it fail after I create the .Exe file?
<marquee scrollamount="1" scrolldelay="1" direction="up" height="10" step="1">--[ ]--
[My Current Status]
I dont know why the hell the script for voting 5 is disabled only for me??
|
|
|
|
|
I'm glad I could help you, even if I guessed about your problem.
Regarding VB and why it works running from the IDE:
I don't know the technical reason for it, but VB is quite forgiving when running from the IDE. I guess (again... ) that VB makes some kind of qualified guess and if you're "lucky" it works.
Aahh, I forgot a few really important things: you have to set up the apartment as well from your secondary thread with a call to ::CoInitialize(). If you haven't done so yet, this could be the reason that it works when running from the IDE. If it's an in-process server it would be the same thing as using a function pointer even if the call is made from another thread. If you properly set up the secondary apartment you could get a HRESULT = RPC_E_WRONG_THREAD, especially in you're running the server out-of-process.
It's supposed to be hard, otherwise anybody could do it!
Regarding CodeProject: "resistance is pointless; you will be assimilated"
|
|
|
|
|
Had to solve other problems in my project. I should spend a while to fully understand it and implement. anyway I'm leaving office with a complete peace of mind only because of Mr. Roger Stoltz. . thanks!! .. see you on monday. Happy weekend
<marquee scrollamount="1" scrolldelay="1" direction="up" height="10" step="1">--[ ]--
[My Current Status]
I dont know why the hell the script for voting 5 is disabled only for me??
|
|
|
|
|
Have a nice weekend yourself.
You saved my weekend with your kind replies!
Careful with the beer, mate! Your belly measures are at risk!
It's supposed to be hard, otherwise anybody could do it!
Regarding CodeProject: "resistance is pointless; you will be assimilated"
|
|
|
|
|
Hello buddy Just fixed my thread-event problem. It's working fine now . Thank you so much Roger. I was really shocked to find the pDispatch->Invoke(0x1, line of code crashing the application. I was clueless what on earth makes it fail with the .exe. Hmmm.. now everything's fine chilling out a bit.. .. thanks again
[Roger].
<marquee scrollamount="1" scrolldelay="1" direction="up" height="10" step="1">--[ ]--
[My Current Status]
I dont know why the hell the script for voting 5 is disabled only for me??
|
|
|
|
|
Nice!!
Thanks for all the 5-votes, but go easy on them otherwise they won't be worth much. Inflation you know...
--
Rog
-- modified at 9:45 Tuesday 8th August, 2006
It's supposed to be hard, otherwise anybody could do it!
Regarding CodeProject: "resistance is pointless; you will be assimilated"
|
|
|
|
|
oh! yeah . but you know I got a little excited about fixing the issue. So all excitement turned into 5s
<marquee scrollamount="1" scrolldelay="1" direction="up" height="10" step="1">--[ ]--
[My Current Status]
I dont know why the hell the script for voting 5 is disabled only for me??
|
|
|
|
|
Roger, got a problem. The DLL works fine with windows application. But I'm trying to use it with an ASP page. In that page, I'm able to access all the properties & methods but not the events. Events are not getting fired. Things are different with the Browser? You have any clue?
<marquee scrollamount="1" scrolldelay="1" direction="up" height="10" step="1">--[ ]--
[My Current Status]
I dont know why the hell the script for voting 5 is disabled only for me??
|
|
|
|
|
I have inherited a COM class out of CComObjectRoot and CComObject using the ATL thingy. Everything compiles fine, but when i try to create an instance of the class, VC2003 breaks due to NULL pointer. It seems that for some reason CComObject depends on the _pAtlModule global variable which for some reason is NULL.
Should the ATL library be initialized in some way before use?
Here is the code where the app crashes from <atlcom.h>
---
template <class Base>
class CComObject : public Base
{
public:
typedef Base _BaseClass;
CComObject(void* = NULL) throw()
{
_pAtlModule->Lock();
}
|
|
|
|
|
I am just now learning COM with ATL, but will try to help.
_pAtlModule is a global variable declared in atlbase.h as:
__declspec(selectany) CAtlModule* _pAtlModule = NULL;
It is set in the constructor of "class ATL_NO_VTABLE CAtlModule : public _ATL_MODULE":
_pAtlModule = this;
So, if you have the normal ATL generated code in your main project file
class CMyATL_ProjectModule : public CAtlDllModuleT< CMyATL_ProjectModule ><br />
{<br />
public :<br />
DECLARE_LIBID(LIBID_MyATL_ProjectLib)<br />
DECLARE_REGISTRY_APPID_RESOURCEID(IDR_MYATL_PROJECT, "{157A51E9-ECF7-4D1B-8D66-694A7CEFB436}")<br />
};<br />
<br />
CMyATL_ProjectModule _AtlModule;
that global field will be set when you compile your program.
|
|
|
|
|
> So, if you have the normal ATL generated code in your main project file
Well, no i dont. You see i dont have any need for an entire ATL project. What i need to do is to implement a single COM interface. So i hoped that i could just extend the CComObject class and have it done the easy way, instead of writing all the COM stuff myself.
This is what i came up with:
---------------------------------------------
class CSampleProsessorRoot : public CComObjectRoot, public ISampleGrabberCB
{
public:
HRESULT STDMETHODCALLTYPE SampleCB(double SampleTime, IMediaSample *pSample);
HRESULT STDMETHODCALLTYPE BufferCB(double SampleTime, BYTE *pBuffer, long BufferLen);
DECLARE_NOT_AGGREGATABLE(CSampleProsessorRoot)
BEGIN_COM_MAP(CSampleProsessorRoot)
COM_INTERFACE_ENTRY(ISampleGrabberCB)
END_COM_MAP()
};
typedef CComObject<CSampleProsessorRoot> CSampleProsessor;
|
|
|
|
|
Hi Friends,
Can I erase elements from stl map while iterating throught it?
Vikram S
|
|
|
|
|
The short answer is no. Erasing elements invalidates any iterators. If you don't need to do anything with the elements in the map prior to deleting them, use the version of erase that takes a range. If you do need to do something with them (say, call delete on each element first), I would create a function that would take a reference to the object (which would be a pointer in this case), and call delete in that function. Then use for_each on the map to delete each element and use the ranged erase method to clear the map.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
From the VC7.1 docs on map :Inserting elements invalidates no iterators, and removing elements invalidates only those iterators that had specifically pointed at the removed elements.
--Mike--
Visual C++ MVP
LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
|
|
|
|
|
Hi!
Can anyone pls help me with this problem. I need to test my activex control in a very simple atl application. So I would really appreciate it if anyone here could be so kind to post code that I can use to test the control in ATL.
I basically just needs to be able to call one of the controls methods and write the returning value out to console for example!!
Thank you in advance!
|
|
|
|
|
Can't you use the MS ActiveX Test Container application (tstcon32.exe) that ships with Visual Studio? Check in the Microsoft Visual Studio Common7\Tools folder.
|
|
|
|
|
Thx for your time!
I know the control is working in the test container AND in MFC applications, but it has been reported that it doesnt work properly in ATL applications, which I find strange. Therefore I wanted to create a simple atl project and see if could reproduce the problem or not!
Thanks
|
|
|
|
|
Hi,
I've encountered decent issue with validity of CWndClassInfo structure of ATL's CWindowImpl. Suppose following scenario:
1. There is a control class derived from ATL's CWindowImpl and rich edit (available via RichEdit20.dll)
2. This control is instantiated - RichEdit20.dll is loaded, CWndClassInfo gets initialized (this time only, as a static content of our class), control gets created
3. Control is destroyed and the dll gets unloaded.
4. Control is instantiated again - dll gets loaded again, to different base address this time. No reinitialization of CWndClassInfo occurs, as it is static and 'valid' from last time.
5. Application crashes as it tries to use rich edit's window procedure (through CWndClassInfo, containing outdated information)
I 'solved' this by clearing m_atom member of CWndClassInfo explicitely; in such case CWndClassInfo gets reinitialized in #4. My knowledge of ATL is fairly limited, but I don't expect this to be correct solution, of course.
Where is the problem here? Should I invalidate the CWndClassInfo in some 'standard way'? Or should the dll not get unloaded at all?
Thx. for any hints
|
|
|
|
|
Howdy! I have been learning C++ and windows programming with MFC since I got canned from my mainframe job last year. I had bought the COM and ATL 3.0 book years ago, and decided to read it a few weeks ago. However, I get to chapter 9 and see that the stuff covered has been redesigned with ATL 7.0. There is only one book out there written since 3.0 that covers 8.0. I have VS pro 2005, so I could continue to learn ATL 8.
But I am unsure if it is worth it for me to learn ATL/COM now, or if I should dive into C# and .NET, which I know nothing about, but have just bought some books on for .NET 2.0. Does the lack of books on the "older" technologies, like MFC, ATL, and COM mean that they are perhaps only supported by legacy systems, and potential employers would be more likely to want .NET experience?
What to do?
Thanks.
|
|
|
|