class A
{
...
B b;
}
我传入的unique_ptr什么时候被删除?
void A::SetB( unique_ptr<B> b )
{
this->b = *b;
} // At end of scope, is `b` now reset
// and the `B b` in class `A` just stores the value in it?
如果上面的答案是否定的,那么它就不会被删除......这会将其删除:
void A::SetB( unique_ptr<B> b )
{
this->b = *move(b);
} // At end of scope, is `b` now reset
// and the `B b` in class `A` just stores the value in it?
或者我必须自己重置它,如果我希望在那时删除它:
void A::SetB( unique_ptr<B> b )
{
this->b = *b;
b.reset();
}
答案 0 :(得分:1)
在前两种情况下,unique_ptr
在函数SetB
返回时被重置。在任何一种情况下你都不会泄漏任何内存,但你所做的事情很奇怪。
在第一种情况下
this->b = *b;
unique_ptr::operator*
返回对托管对象的引用。因此,您需要将B
参数中包含的unique_ptr<B>
实例分配给您的班级数据成员b
。
第二种情况
this->b = *move(b);
的行为与第一个完全相同。这次你所做的就是unique_ptr::operator*
上的unique_ptr<B>&&
调用unique_ptr
只是将其参数转换为右值参考。
在这两种情况下,B
参数保留最初调用的SetB
实例unique_ptr
的所有权,并且当函数返回时它将销毁它。
您的用例似乎不是您应该使用void A::SetB( B b )
{
this->b = move(b);
}
的用例,您只需要一个setter。可能是以下
import cv2
import numpy as np
import zbar
import math
import time
#width of the object
qrwcoord = 0
qrPL = 0
qrNorm = 0
box = 0
focalLenght = 608 #Calibration data
intrinsic = np.array([[608.14578, 0, 292.31368], [0, 608.82584, 261.47420], [0, 0, 1]]) #3x3 Calibration intrinsic matrix
distortion = np.array([ [0.02232355, -0.11265588, 0.00247792, -0.00201052, 0.22786218]]) #1x5 Distortion vector
def distance_to_camera(knownWidth, focalLength, perWidth):
return (knownWidth * focalLength) / perWidth
def decode_QR(image):
global box
gray = cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)
(height, width)=gray.shape
raw = str(gray.data)
scanner = zbar.ImageScanner()
scanner.parse_config('enable')
image1 = zbar.Image(width, height, 'Y800', raw)
scanner.scan(image1)
for symbol in image1:
TLC, BLC, BRC, TRC = [item for item in symbol.location]
box = np.array([[TLC],[TRC],[BRC],[BLC]])
qrData=symbol.data
qrStrip = []
qrStrip = qrData.split(';')
qrNorm = qrStrip[0] #Qr Code norm vector
qrPL = qrStrip[1]
qrwcoord = qrStrip[2] #QR Code World Coordinates
qrPL=float(qrPL) #Qr Code size in meters
print 'decoded', symbol.type, 'symbol', '"%s"' % symbol.data
qrpixelwidth = math.sqrt((TLC[0]-TRC[0])**2+(TLC[1]-TRC[1])**2)
qrpixelheight = math.sqrt((BLC[0]-BRC[0])**2+(BLC[1]-BRC[1])**2)
pixel_size = float(qrPL/qrpixelwidth) #Pixel Size in meter
plane_x_size = float(pixel_size*640)
plane_y_size = float(pixel_size*480)
dist = distance_to_camera(qrPL, focalLenght, qrpixelwidth)
print qrNorm
print qrPL
print qrwcoord
print box
print qrpixelwidth
print qrpixelheight
print "Distance from QR to Camera is:", dist
print qrStrip
return box
cv2.namedWindow("Streaming...")
capture = cv2.VideoCapture(0)
capture.open('http://192.168.5.2:8080/stream/video.mjpeg')
while True:
(grabbed, img) = capture.read()
if not grabbed:
break
box = decode_QR(img)
cv2.drawContours(img, [box], -1, (0, 255, 0), 2)
cv2.imshow("Streaming...", img)
#time.sleep(0.05)
key = cv2.waitKey(1)& 0xFF
if key == ord("q"):
break
cv2.destroyWindow("Streaming...")