Cannot return copy

Discussion in 'C++' started by none, May 11, 2010.

  1. none

    none Guest

    In the below code the call:

    DisplayImage(job.GetResultImage());

    just shows a black image even though it looks fine when its created.



    int main() {

    typedef MyImg MyImgType;
    typedef Job<MyImgType> JobType;

    MyImageApp<MyImgType> app;
    JobType job;
    app.Run(job);

    // This only shows a black image!
    DisplayImage(job.GetResultImage());
    return 0;

    }




    // ============== MyImageApp ======================

    template<typename F>
    class MyImageApp {
    public:


    void Run(JobType & job) {

    // The 'processor' instance will die when this function returns.
    Processor<F> processor;
    processor.Process();

    // Not possble to copy the image from the processor into the job!?
    job.SetResultImage(processor.GetWarpedImage());
    }

    };



    // ============== Processor =======================

    template <typename FI>
    class Processor {
    public:
    typedef FI FixedImageType;
    typedef itk::ResampleImageFilter< FixedImageType, FixedImageType > ResampleFilterType;

    WarpProcessor ();
    virtual ~WarpProcessor ();
    void Process();

    // Returning a copy not a reference!
    typename FixedImageType::pointer GetImage(){return this->imageW;}

    private:
    typename FixedImageType::pointer imageW;

    };

    template <typename FI>
    void
    Processor<FI>::process() {
    // Computing stuff

    // ...

    // Copying the result into the imageW instance
    CopyImageToImage<typename FixedImageType::pointer, FixedImageType>(
    resampler->GetOutput(),
    imageW);

    // image looks fine when viewed here!
    DisplayImage(imageW)

    }




    // ================== Job =======================

    template<typename F>
    class Job {
    public:
    typedef F FixedImageType;

    // Return copy!
    typename FixedImageType::pointer GetResultImage() {return this->imageR;}
    void SetResultImage(typename FixedImageType::pointer image) {this->imageR = image;}

    private:
    typename FixedImageType::pointer imageR;

    };




    Any ideas on why the image looks fine when calling:

    DisplayImage(imageW)

    in Processor::process() and pitch black when calling:

    DisplayImage(job.GetResultImage());

    in main?
     
    none, May 11, 2010
    #1
    1. Advertising

  2. none

    Jonathan Lee Guest

    >    // Return copy!
    >    typename FixedImageType::pointer GetResultImage() {return this->imageR;}


    From this I would guess that you're not actually returning
    a copy, you're returning a pointer. If the destructor is called
    for the image that is being pointed to, the data is lost.
    (Or, at least practice it should be.)

    So, giving that you're passing things around by pointer instead
    of by value, I expect that the thing pointed to doesn't exist
    anymore. That's just a guess, though.

    --Jonathan
     
    Jonathan Lee, May 11, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ram
    Replies:
    2
    Views:
    7,226
    Paul Glavich [MVP ASP.NET]
    Jun 30, 2005
  2. Alex
    Replies:
    2
    Views:
    1,239
  3. Replies:
    26
    Views:
    2,123
    Roland Pibinger
    Sep 1, 2006
  4. Waldy
    Replies:
    4
    Views:
    498
    Waldy
    Aug 3, 2007
  5. none
    Replies:
    0
    Views:
    263
Loading...

Share This Page