我遇到了一个非常奇怪的问题.
代码如下:
::boost::shared_ptr<CQImageFileInfo> pInfo=CQUserViewDataManager::GetInstance()->GetImageFileInfo(nIndex);
Image* pImage=pInfo->m_pThumbnail;
if(pImage==NULL)
pImage=m_pStretchedDefaultThumbImage;
else
{
//
int sourceWidth = pInfo->GetWidth();
int sourceHeight = pInfo->GetHeight();
int destX = 0,
destY = 0;
float nPercent = 0;
float nPercentW = ((float)GetThumbImageWidth()/(float)sourceWidth);;
float nPercentH = ((float)GetThumbImageHeight()/(float)sourceHeight);
if(nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = (int)((GetThumbImageWidth() - (sourceWidth * nPercent))/2);
}
else
{
nPercent = nPercentW;
destY = (int)((GetThumbImageHeight() - (sourceHeight * nPercent))/2);
}
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
rcShowImage=CRect(rc.left+destX, rc.top+destY,rc.left+destX+destWidth,rc.top+destY+destHeight); …Run Code Online (Sandbox Code Playgroud) c ++/boost使用都知道我们可以轻松地重置一个指向新实例的智能指针(旧的实例同时销毁).我想知道我们如何为COM智能指针做到这一点?
_COM_SMARTPTR_TYPEDEF(IMyClass,__ uuidof(IMyClass));
//normal class A
class A{
IMyClass m_spIMyClassObj; //this COM smart pointer is a member variable of a normal class
};
Run Code Online (Sandbox Code Playgroud)
我初始化COM智能指针:
m_spIMyClassObj.CreateInstance(__uuidof(MyLib::IMyClass));
Run Code Online (Sandbox Code Playgroud)
这很好但是在A的生命周期中,我需要将COM智能指针m_spIMyClassObj重置为一个新的IMyClass实例,我该怎么做(还要确保清除旧的id).
谢谢
我说的是一般应用程序,而不是低级别的,因此它们与硬件接口.
查看遗留代码有很多:
delete myObject;
Run Code Online (Sandbox Code Playgroud)
为什么Smart Pointer的概念不是很早就引入的,它只是利用了RAII,这是一个基于堆栈的对象在离开范围时破坏的概念.从第一天起就一直存在.
可能是性能,一个额外的方向(但这是一个小的价格/开销支付).
开发人员可以在不到30分钟的时间内创建基本但功能强大的自定义智能指针.那么为什么这么久?
或者它在那里,但人们选择不使用它...
我正在尝试创建一堆共享指针并将它们放入各种容器中.
使用原始指针我会考虑做以下事情:
Container a, b, c;
MyClass *ptr;
while(!finishedCreating){
ptr = new MyClass(SOME_CHANGING_THING);
a.add(ptr);
b.add(ptr);
c.add(ptr);
}
Run Code Online (Sandbox Code Playgroud)
不过,当然,如果我现在想破坏a,b和c我需要删除它们所包含的指针.如果我做了以下事情:
~Container{
delete[] myListOfPointers;
}
Run Code Online (Sandbox Code Playgroud)
在破坏时我会得到一个错误,因为删除a会删除相同的内存b并且c应该删除它.
特别是,输入智能指针std::shared_ptr.我想做一些类似于之前我可以创建单个实体并使用它指向大量内存位置的东西,但我不知道该怎么做?
我想创建一个指向a的指针,std::shared_ptr以便我可以重新分配该指针,例如
std::shared_ptr<MyClass> *ptr = new std::shared_ptr<MyClass>(new MyClass(THING));
Run Code Online (Sandbox Code Playgroud) 我有以下课程
class a {
std::shared_ptr<b> b_ref;
public:
a(std::shared_ptr<b> b_ref) : b_ref(b_ref) {}
};
class b {
std::shared_ptr<a> a_ref;
public:
b(std::shared_ptr<a> a_ref) : a_ref(a_ref) {}
};
Run Code Online (Sandbox Code Playgroud)
现在我想创建两个相互引用的对象有没有办法做到这一点?我认为使用c风格的指针我可以做到这一点.它也适用于共享指针吗?我尝试了reset()和swap()方法没有任何成功.
std::shared_ptr<a> a_ref;
std::shared_ptr<b> b_ref;
a_ref = std::make_shared<a>(b_ref);
b_ref = std::make_shared<b>(a_ref);
Run Code Online (Sandbox Code Playgroud)
不工作.
使用智能指针,您是否仍需要释放/重置它们以确保释放内存?或者让它们超出范围是否可以?
对于类成员智能指针,行为 - 与释放内存,悬空指针有关 - 有什么不同吗?析构函数是否应始终释放这些变量?
class Foo
{
public:
Foo()
{
myUPtr = std::unique_ptr<int>(new int);
mySPtr = std::shared_ptr<int>(new int);
}
~Foo()
{
// Should these smart pointers be released? Or will falling out of scope/deletion release the memory?
myUPtr.release();
mySPtr.reset();
}
private:
std::unique_ptr<int> myUPtr;
std::shared_ptr<int> mySPtr;
};
int main()
{
// When each of the below variables fall out of scope is there any difference in memory release?
Foo f;
std::unique_ptr<Foo> uFoo(new Foo);
std::shared_ptr<Foo> sFoo(new Foo);
std::unique_ptr<int> uPtr(new int); …Run Code Online (Sandbox Code Playgroud) 由智能指针管理的分配内存是否保证在发生异常时释放,例如下面?
#include <memory>
void test( std::shared_ptr<int> sptr )
{
throw "exception";
}
int main()
{
std::shared_ptr<int> ptr( new int(1) );
test( ptr );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我尝试执行代码,将断点放在shared_ptr析构函数中,但我没有看到它被调用.我认为应该自己清理内存.我是对的,还是不会被清理干净?
我在这里阅读智能指针实现,一切都很好,如示例所示.但是,如果我尝试向上转换对象指针并使用智能指针,那么将会有一个关于被释放的对象的运行时错误投诉没有分配,有人可以告诉我发生了什么以及如何解决它?谢谢
#include <iostream>
template <class T>
class SmartPointer{
T* ptr = nullptr;
public:
explicit SmartPointer(T* t):ptr(t){}
virtual ~SmartPointer(){
std::cout<<"call delete here"<<std::endl;
if (ptr!=NULL){
delete (ptr);
}
}
T & operator*(){
return *ptr;
}
T * operator->(){
return ptr;
}
};
class Animal{
public:
virtual void get_name(){};
virtual ~Animal(){std::cout<<"animal is destroied"<<std::endl;}
};
class Dog:public Animal{
std::string d_name;
public:
Dog(std::string n):d_name(n){}
virtual ~Dog(){std::cout<<"dog is destroied"<<std::endl;}
void get_name(){std::cout<<"the name is "<<d_name<<std::endl;}
};
int main(int argc, const char * argv[]) {
// …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个函数,接受unique_ptr到Base作为参数派生的类(表示所有权转移).我想出了以下代码:
#include <memory>
#include <type_traits>
class Base
{
public:
Base() {}
};
class Derived : public Base
{
public:
Derived() {}
};
void foo(std::unique_ptr<Derived> _data) {}
template <class T>
void boo(std::unique_ptr<T> _data) {
static_assert(std::is_convertible<T*, Base*>::value,
"Only classes derived from Base can be used as "
"parameter for boo");
}
int main() { foo(new Derived); }
Run Code Online (Sandbox Code Playgroud)
但是当我尝试编译它时,我得到并且错误地告诉我,could not convert ‘(operator new(1ul), (<statement>, ((Derived*)<anonymous>)))’ from ‘Derived*’ to ‘std::unique_ptr<Derived>’如果我在使用时使用foo和相同(加上一些模板细节)boo.如果我正确地理解了一切,我被告知的是没有构造函数std::unique_ptr<Derived>可以接受指针Derived.但是当我在unppue_ptr构造函数的cppreference 页面上查看时,我看到有这样的构造函数(编号3-4). …
我编写了一个自定义类来存储图像,并最终基于这些图像计算校准,但是在存储图像的方式上遇到了问题。我有两个重载函数可以执行此操作,一个函数使用读取文件中的图像,另一个函数cv::imread使用Snapshot用于保存数据的中间数据结构。使用函数的cv::imread效果很好,但是使用自定义数据结构的函数却不能。我现在尝试存储三个图像,问题是当我将图像推入矢量时,第二个图像的数据被复制到第一个图像中。
这是工作功能:
bool CalibClass::AddImage(const std::string& snapshotPath) {
cv::Mat img = cv::imread(snapshotPath);
// _snapshots is a private member declared as a std::vector<cv::Mat>
_snapshots.push_back(img);
return true;
}
Run Code Online (Sandbox Code Playgroud)
这是不起作用的功能:
bool CalibClass::AddImage(const ImageSet& snapshot) {
RGBImage *rgb_image_ptr = snapshot.GetRGBImage();
std::vector<unsigned char> img_data(rgb_image_ptr->GetData());
cv::Mat img(rgb_image_ptr->GetHeight(), rgb_image_ptr->GetWidth(), CV_8UC3, img_data.data());
_snapshots.push_back(img);
return true;
}
Run Code Online (Sandbox Code Playgroud)
在ImageSet类存储图像作为std::unique_ptr<RGBImage>。在RGBImage类存储的图像数据作为std::vector<unsigned char>。
这是将图像从中加载到类中的方式main:
cv::Mat img1 = cv::imread("img1.png");
cv::Mat img2 = cv::imread("img2.png");
cv::Mat img3 = cv::imread("img3.png");
int length …Run Code Online (Sandbox Code Playgroud)