Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. I have memory address problem.

I have memory address problem.

Scheduled Pinned Locked Moved General and Desktop
13 Posts 4 Posters 4.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    awesomesean
    wrote on last edited by
    #1

    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,

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      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.
      greetz

      Greetz, Jeroen

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mohsen
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • A Offline
          A Offline
          awesomesean
          wrote on last edited by
          #4

          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
          .
          .
          .
          Etc

          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,

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mohsen
            wrote on last edited by
            #5

            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()?

            1 Reply Last reply
            0
            • A Offline
              A Offline
              awesomesean
              wrote on last edited by
              #6

              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,

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mohsen
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  lgeyer
                  wrote on last edited by
                  #8

                  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).

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    awesomesean
                    wrote on last edited by
                    #9

                    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,

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      lgeyer
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mohsen
                        wrote on last edited by
                        #11

                        [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?

                        1 Reply Last reply
                        0
                        • L Offline
                          L Offline
                          lgeyer
                          wrote on last edited by
                          #12

                          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).

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            awesomesean
                            wrote on last edited by
                            #13

                            Finally, I got a problem.

                            Thank you for all guys!!

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved