I have memory address problem.
-
Hi there,
Thank you for your help for me. I add source code and screen shoot for your information.
I define values that are a structure, have image information, like shown below.
@
IBSU_ImageData image, image2;
@Then, I called a function from C++ library to get image data from our device to value (&image). (This library returns image buffer address to Qt application.)
@
typedef int (*IBSU_BGetImage)(const int, IBSU_ImageData *, IBSU_ImageType *, IBSU_ImageData *,
int *, IBSU_FingerCountState *, IBSU_FingerQualityState *, int *);IBSU_BGetImage bGetImage = (IBSU_BGetImage)lib.resolve(“IBSU_BGetImage”);
nRc = bGetImage(m_nDeviceHandle, &image, &imageType, &pSplitImageArray[0], &splitImageArrayCount, &fingerCountState, &m_FingerQuality[0], &qualityArrayCount);
@Finally, I called “memcpy” function to copy data from image to image2;
@
memcpy(&image2, &image, sizeof(IBSU_ImageData));
@However, original data’s (&image) address and value are changed at running “memcpy” function like shown below.
&image data information before call memcpy :
Buffer : 0xbc10020
Width : 800
Hight : 750
.
.
.
Etc&image data information after call memcpy :
Buffer : 0x9a4d8
Width : 4202293
Hight : 631832
.
.
.
EtcThen changed address is copied to &image2.
So, When I use &image2, it has garbage data.That is my problem. Could you guys give me some clue?
Thank you,
-
all it seems, you've trying "memcpy" to perform a deep copy of an "object". be aware "memcpy" will only be useful when you want to copy through c++ fundamental types. in order to clone an object you should use c++ "copy constructor" pattern.
if you only pass the image data then why not let Qt handle your image and make it easy just by calling QPixmap::copy() or QImage::copy()? -
Thank you Mohsen!
However, I would like to copy not only image buffer but also other struct members such as
width and height so on.Because of, IBSU_ImageData struct has many members about image.
So, I need to copy whole struct.If you have any clue about it, please let me know.
Thank you,
-
i've already mentioned in my previous post
"be aware “memcpy” will only be useful when you want to copy through c++ fundamental types. in order to clone an object you should use c++ “copy constructor” pattern."just a little search on google
http://en.wikipedia.org/wiki/Copy_constructor -
Thanks Mohsen and Lukas
It is header for IBSU_ImageData. Here we go.
@
typedef enum tagIBScanUImageFormat
{
IBSU_IMG_FORMAT_GRAY, ///< Gray scale image
IBSU_IMG_FORMAT_RGB24, ///< 24 bit RGB color image
IBSU_IMG_FORMAT_RGB32, ///< True color RGB image
IBSU_IMG_FORMAT_UNKNOWN ///< Format not set or unknown
}
IBSU_ImageFormat;/// Container to hold image data together with meta information.
typedef struct tagIBSU_ImageData
{
void* Buffer; ///< Pointer to image buffer
DWORD Width; ///< Image horizontal size
DWORD Height; ///< Image vertical size
double ResolutionX; ///< Horizontal image resolution (in PPI)
double ResolutionY; ///< Vertical image resolution (in PPI)
double FrameTime;int Pitch;
BYTE BitsPerPixel;
IBSU_ImageFormat Format;
BOOL IsFinal;}
IBSU_ImageData;
@Also,
@
void* Buffer; ///< Pointer to image buffer
@above buffer data is made by inside of library which is c++.
I tried to copy a struct which does not have pointer value using memcpy.
It will be working well. So, I assume that pointer value in struct causes this problem.Thanks,
-
You cannot shallow copy this object, as both objects will then point to the same buffer, referenced by the void* Buffer member.
You will have to do a deep copy, which means you will have to copy both, the structure itself and the memory region referenced by Buffer, and then adjust the void *Buffer member in the copied structure to reference the copied memory region. This is what usually a copy constructor would do.
The compiler will automatically generate (besides a default constructor and a default destructor) a default copy constructor and a default assignment operator, but which will do just a memberwise copy (basically a memcpy).
So in your case are equal (and equally wrong in terms of a deep copy).
@
memcpy(&image2, &image, sizeof(IBSU_ImageData));
image2 = image;
@Have you tried creating your QImage using QImage::fromData() directly from the buffer?
However, this does not explain why your call to memcpy does modify the source structure, and why the destination structure contains invalid data. You may provide a small, compileable example which reproduces your problem.
-
[quote author="Lukas Geyer" date="1348830767"]
However, this does not explain why your call to memcpy does modify the source structure, and why the destination structure contains invalid data. You may provide a small, compileable example which reproduces your problem.[/quote]memcpy does a bitwise copy, could it affect?
-
It doesn't matter, as IBSU_ImageData is composed of integral types only.
The problem in this case is that source appears to be modified, which should never happen when using memcpy. This is only possible if source and destination are consecutively on the stack (which grows top down, from a higher memory address to a lower memory address, with destination beeing located before source in memory), and memcpy is passed a size exceeding sizeof(destination).
-
Finally, I got a problem.
Thank you for all guys!!