Boost logo

Boost Users :

From: Robert Ramey (ramey_at_[hidden])
Date: 2007-02-03 20:58:15


Try avoiding the default handling that serialization uses.

template<class Archive>
void save_image_ptr(Archive & ar, IplImage const * const iptr){
    ar << binary_object(iptr, sizeof(IplImage));
}

template<class Archive>
void load_image_ptr(Archive & ar, IplImage * & iptr){
    iptr = cvCreateImage(...);
    ar >> binary_object(iptr, sizeof(IplImage)
}

Robert Ramey

Anders Sundman wrote:
> Hi!
>
> Is it possible to serialize a class A by pointer using a custom
> allocator? (Not the custom in-place allocator approach).
>
> I'm trying to serialize the (OpenCV) class IplImage. The class is used
> like this:
>
> IplImage* img = cvCreateImage(...);
> ...
> cvReleaseImage(&img);
>
> I've tried the following:
>
> template<class Archive>
> inline void load_construct_data(Archive & ar, IplImage * img, const
> unsigned int file_version){
> ...
> IplImage* tmpImg = cvCreateImage(...);
> memcpy(img, tmpImg, sizeof(IplImage));
> }
>
> But I get exceptions later, when releasing the loaded image. As far
> as I can tell, the image class data is identical before and after
> serialization, so I guess that the cvCreateImage/cvReleaseImage keep
> track of pointers somehow and things go bad when the ReleaseImage is
> called by a pointer not returned by the CreateImage function?
>
> Can the allocation of the IplImage used as input to
> load_construct_data be customized somehow?
>
> I also tried using an IplImage * & as input to load_construct_data:
>
> template<class Archive>
> inline void load_construct_data(Archive & ar, _IplImage * & img, const
> unsigned int file_version){
> _IplImage* tmpImg = cvCreateImage(...);
> delete img;
> img = tmpImg;
> }
>
> But that screws up the (de)serialization.
>
> Any suggestions or thoughts anyone?
>
>
> // Anders Sundman


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net