Important: Please read the Qt Code of Conduct -

I have memory address problem.

  • Hi, I have a question about use memory. I am using the library which is made by C++.
    There is a function for capture image in library and I am calling this library function in Qt application to capture image.

    Function in library is return image buffer address when image capture is finished then I need to use this address for image processing.
    The problem is image buffer address from library function is changed when I use this address in Qt application.
    (So, I could not get image data using memory address)

    For instance, if I try to save image buffer as .bmp or .raw format using address from library (I used fwrite), it is impossible to store it because image buffer address is changed as wired address when I call fwrite.

    Of course, if library send Qt application image data instead of image buffer address,
    my problem will be solved but I could not change the library due to compatibility with other application.

    Could you guys give me some tips or clue?

    I am using Qt Creator (ver 4.8.1) and Qt Gui application template.

    Thank you,

  • Hi there,
    Using fwrite is a C++ function and not really a Qt way to do it. Better to use the QFile classes.
    Of course you are free to use it anyway.

    A way to protect your pointer is to make it const. You have to initialise it when creating it, otherwise it will be set to random location or probably zero. So:
    @const Image* = new Image(the address from the lib);@
    Then fwrite will not be able to change the pointer.
    I think it has more to do with a 32 bit/64 bit library and operating system. When the library returns a 64 bit pointer (most likely) and fwrite will change it to 32 bits, this will cause the pointer to be changed.
    This what I would try to check, if that doesn't work, a bit more example code so we can take a look at it would be nice.

  • without showing your code, less help we can do for you

  • 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

    &image data information after call memcpy :
    Buffer : 0x9a4d8
    Width : 4202293
    Hight : 631832

    Then 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

  • It would be of help to know how the IBSU_ImageData looks like to judge if it qualifies for a deep copy. So if possible you might post the header file here (or, if it is of a larger extent pastebin it somewhere).

  • 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

    /// 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;


    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.


  • 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!!

Log in to reply