[ome-devel] Saving File attachements to an Image in C++
Josh Moore
josh.moore at gmx.de
Tue Jun 8 15:28:36 BST 2010
On 6/8/10 3:13 PM, Jerome Avondo wrote:
>
> Hi Josh,
>
> Ahhh... Yes it was a casting issue, using the dynamicCast solves my issue. In fact I wasn't aware of that casting function, which now makes a lot of sense...
>
> FYI I added the if (!originalFile) as shown below, and the object returned seems not to be null...
Hmmm....then it might be the reference counting. The assignment to
IObjectPtr increments the reference count and keeps the object alive.
Whatever (IObjectPtr&) did on MSVS2008 may have allowed the object to be
freed up leading to your access violation.
> originalFile = (omero::model::OriginalFilePtr&) up->saveAndReturnObject(originalFile);
> if (!originalFile) cout<< "file is null"<<endl;
>
> I originally, *cough* *hack* *cough*, added (omero::model::OriginalFilePtr&) just to get the thing to compile, as just doing what I thought it should be intuitively,
>
> originalFile = (omero::model::OriginalFilePtr) up->saveAndReturnObject(originalFile);
>
>
> would give me the compile error below...
>
> error C2440: '=' : cannot convert from 'omero::model::IObject *const ' to 'omero::model::OriginalFile *'
>
> I was struggling with this whole casting stuff, but now understand....
>
> The crash was an "Access violation reading location 0xfffffffc" when calling the line
>
> rawfilestore->setFileId(originalFile->getId()->getValue());
>
> The compiler I'm using btw is MSVS2008 on WinXP 32bit.
>
> Thanks a lot for the help!
> Let me know if you need more info...
Sure, no problem. I don't think we need to follow up. But it is good to
know that (ObjPtr&) is *not* a valid way to cast. :)
Cheers,
~J.
> J.
>
> ----------------------------------------
>> Date: Tue, 8 Jun 2010 14:14:53 +0200
>> From: josh.moore at gmx.de
>> To: ome-devel at lists.openmicroscopy.org.uk
>> Subject: Re: [ome-devel] Saving File attachements to an Image in C++
>>
>> Hi Jerome,
>>
>> with $ gcc --version
>> gcc (GCC) 4.1.2 (Gentoo 4.1.2 p1.3)
>>
>> I can't get the (OriginalFilePtr&) line to even compile, so I'm not sure
>> what your compiler is doing with that statement. Could you add something
>> like:
>>
>> if (!originalFile) {
>> cout<< "file is null";
>> }
>>
>> to find out? Better yet, what do you get when using the Ice-generated
>> dynamicCasts?
>>
>> originalFile =
>> OriginalFilePtr::dynamicCast(up->saveAndReturnObject());
>>
>> saveAndReturnObject should never return an object for which:
>>
>> obj->getId()->getValue()
>>
>> throws an exception, which is why I assume the issue is the cast. Note
>> also: here you could save yourself the cast by saving as:
>>
>> omero::model::IObjectPtr obj = query->findByQuery();
>> obj->getId()->getValue();
>>
>> Finally, what exception handling do you have in place and what kind of
>> crash are you getting?
>>
>> Cheers,
>> ~Josh.
>>
>> On 6/8/10 12:11 PM, Jerome Avondo wrote:
>>>
>>> Hi all,
>>>
>>> I am trying to save a movie file to an image via the C++ API. I have code to do this in Java so I am trying to reproduce this functionality in C++ but am struggling....
>>>
>>> My code logic is as follows,
>>>
>>> omero::model::OriginalFilePtr originalFile = new omero::model::OriginalFileI();
>>>
>>> //omero::model::FormatPtr f = (omero::model::FormatPtr&)query->findByString("omero.model.Format", "value", "video/mpeg");
>>> omero::model::FormatPtr f = (omero::model::FormatPtr&) query->findByQuery("from Format as f where f.value='video/mpeg'", 0);
>>>
>>> originalFile->setName(rstring(movieFile));
>>> originalFile->setPath(rstring(fullPath));
>>> originalFile->setSize(rlong(size));
>>> originalFile->setSha1(rstring("pending"));
>>> originalFile->setFormat(f);
>>>
>>> originalFile = (omero::model::OriginalFilePtr&) up->saveAndReturnObject(originalFile);
>>>
>>> rawfilestore->setFileId(originalFile->getId()->getValue());
>>>
>>> However my originalFile object seems to be wrong/incorrect, as the call to originalFile->getId()->getValue() is causing a crash.
>>> Is the above calls the minimum I need to create an OriginalFile object?
>>> I'm running in circles at this end :)
>>>
>>> Thanks!
>>>
>>> J.
More information about the ome-devel
mailing list