Frequently Asked Questions 编辑
This section will help you if you're fixing a broken build, or have what you think is a quick obvious question, and you don't have time to read the Reference Manual. This FAQ usually just refers back directly to the appropriate answer, there. If you're looking here just to learn about nsCOMPtr
s, you'll get a better introduction in the Getting Started Guide.
The FAQ is divided into sections to help you find what you're looking for faster. In most cases, the answer will just refer back into the reference manual, above. No need to explain things twice :-)
.
Buildtime Errors
The build just broke. It's not in your code, or it's not on your platform, but there's an nsCOMPtr
on the line where the error is and you're suspicious. You're looking in the right place.
comparing an nsCOMPtr
to a raw XPCOM interface pointer
declaring an nsCOMPtr
to a forward-declared class
not linking to XPCOM
not including nsCOMPtr.h
different settings of NSCAP_FEATURE_DEBUG_PTR_TYPES
Runtime Errors
NS_ASSERTION
"QueryInterface needed"
May be caused by a class that derives from a given interface, when you forgetting to also specify the interface name in the NS_IMPL_ISUPPORTS
/ NS_IMPL_THREADSAFE_ISUPPORTS
macro.
For example, the assertion occurs when you convert the pointer to the object to a raw pointer to that interface, then assign the raw pointer to another nsCOMPtr
for that interface.
NS_PRECONDITION
"You can't dereference a NULL nsCOMPtr with operator->()"
NS_PRECONDITION
"You can't dereference a NULL nsCOMPtr with operator*()"
Other issues
printf("%x\n", mynsCOMPtr);
can cause the program to crash onSIGILL
(illegal instruction), and not on the line with theprintf
, which can make it tricky to figure out. For debugging purposes you can writeprintf("%x\n", mynsCOMPtr.get());
How do I...
initialize an nsCOMPtr
?
Release
an nsCOMPtr
before it goes out of scope?
Assign 0
into it. Whenever an nsCOMPtr
takes on a new value, it always Release
s its old value, if any. Assigning in the value 0
is just like assigning in a raw pointer that happens to be NULL
. The old referent will be Release
d. [See Initialization and Assignment for more details]
You should note, though, that there is a small performance penalty for this. The nsCOMPtr
will still exercize logic in its destructor to attempt to Release
the value it has at that time. The optimal solution is to arrange the lifetime of your nsCOMPtr
to correspond to exactly how long you want to hold the reference. E.g., using blocks as in this sample
|
Editors Note: Move this discussion to the efficiency section, and link to it from here.
make an nsCOMPtr
leak (for a debug test)?
call a getter that uses a raw XPCOM interface pointer as an `in/out' parameter?
call a getter that fills in an nsIFoo*&
parameter?
call a getter that doesn't AddRef
its result?
Any XPCOM function that returns an interface pointer, i.e., a `getter', must have already AddRef
ed that pointer. If it didn't, you should probably report it as a bug. No matter which code pattern you use to solve this problem, you should comment it, e.g., // Warning: this getter doesn't AddRef() its result
. If the getter returns the new pointer as its function result, no worries,
General
Does nsCOMPtr
bloat the code?
Are nsCOMPtr
s fast? Can I use them in tight loops?
Bibliography
Web Resources
- nsCOMPtr.h, and nsCOMPtr.cpp are the source to
nsCOMPtr
. You can examine the source tonsCOMPtr
online using (the wonderful) LXR. Exploring this code is not an adventure for the faint of heart. - Some COM Ownership Guidelines.
- Interface Pointers Considered Harmful by Don Box originally appeared in the September 1995 issue of "The C++ Report".
- COM Smart Pointers Even More Harmful by Don Box is a follow-up article that originally appeared in the February 1996 issue of "The C++ Report".
Books
- Essential COM by Don Box.
- Effective COM by Don Box, et al.
- The C++ Programming Language (3rd Edition) by Bjarne Stroustrup.
- Effective C++ (2nd Edition): 50 Specific Ways to Improve Your Programs and Designs by Scott Meyers.
- More Effective C++ : 35 New Ways to Improve Your Programs and Designs by Scott Meyers.
- Effective C++ CD: 85 Specific Ways to Improve Your Programs and Designs by Scott Meyers.
People
- Don Box is a smart guy who has been writing about COM programming for a long time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论