I am developing an iOS with OpenCV.
OpenCV is an Open Source Image Library and works with C++.
I want to store some data from the Library in an NSData object.
Is it possible to convert a c++ struct to NSData without losing the object?
- (void)addMat:(cv::Mat)mat andImageName:(NSString *)name {
self.myMat = [NSData dataWithBytes:&mat length:sizeof(mat)];
self.imageName = name;
}
Now I'm using the code above.
But this NSData only stores the pointers not the actual data.
When I try to get the object back I
pointer being freed was not allocated
* set a breakpoint in malloc_error_break to debug
My data should be stored in a SqLite Database.
Do you have an idea to convert everything ?
I tried to convert every single property. This worked for some properties quite well and for others it didn't work.
I hope you could help me.
Thank you.
Greetings,
Alexander Heinrich
You may write bytes from objects inside data, but it doesn't store object. For example object can have pointers at other objects and they can become invalid after some time, but yours NSData copy will not know about that.
You should turn on Objective-C++ and work direct with C++ structures/classes.
Related
I am trying to save and load the state of b2World in order to resume a simulation at a later time, but with the states of the Collision Manager, etc being exactly maintained. What is the best way to do this (without getting into library internals, and having to use boost serialize while monitoring public/private members of every class)? Is there a way to repurpose the log file from b2World.dump function to construct the object again?
I think parsing Dump as-is is a dead end.
First, the output of Dump seems to be executable C++ code:
b2ChainShape chainShape;
b2Vec2 vertices[] = {b2Vec2(-5,0), b2Vec2(5,0), b2Vec2(5,5), b2Vec2(4,1), b2Vec2(-4,1), b2Vec2(-5,5)};
chainShape.CreateLoop(vertices, 6);
b2FixtureDef groundFixtureDef;
groundFixtureDef.density = 0;
groundFixtureDef.shape = &chainShape;
Secondly, there is the problem of dumping floating point values with enough precision to recreate the original object.
Finally, some objects don't seem to support Dumping at all.
Some alternatives:
Hack box2d and add your own state-preserving dumping mechanism
Keep all box2d objects in a specific memory area and use memory snapshotting and/or checkpointing techniques to restore that memory again on load. One such library I know of is Ken, but I'm sure there are other implementations.
I am attempting to use flatbuffers as a way to communicate between master/slave servers. The issue I am facing is, that after I have read data from char* into flatbuffer, I can't find a way to write it back into char*. First configuration:
flatc --cpp --gen-mutable --gen-object-api -o ${OUT} ${IN}
And here is code which bothers me:
char* buffer = <MY_FLATBUFFER_DATA>;
auto managedObject = GetMutableManagedObject(buffer);
makeChanges(managedObject);
char* newBuffer = managedObject.deserialize(); // This is my imaginative method
I want to be able to deserialize object which has been modified and send it back to the slave. Is it possible? Or I have to use "slower" (as it describes tutorial) Pack/UnPack methods?
(I do not mind another approach kind of answer)
Yes, you need Pack/UnPack. --gen-mutable only allows very limited modification (some scalars), if you want to be able to modify everything you need the object api.
I thought this would be simple but I seem to have myself confused.
I have been learning C++ for some time now and thought I was getting the hang of it. I have moved over to the 'Visual' aspects of it, specifically MFC.
Basically I'm trying to pass a variable from a DLL into a simple static text box on a MFC application.
( This is a plugin for the game rFactor )
This is the code I have in the DLL file ( .hpp )
struct TelemInfoBase
{
float mEngineRPM; // engine RPM
}
And this is the code to put it into the Static text
SetDlgItemText(IDC_DRIVER_NAME, TelemInfoBase::mEngineRPM);
But I'm getting a 'illegal reference to non-static member' error
I assume I have to define a variable for the data to be stored in and then access that variable, but I can't quite get my head around how to do that.
Would anyone care to point me in the correct direction?
I'm trying to do the following trick:
I have IDataObject* to be set into the clipboard, so I'm using OleSetClipboard() to set it into the clipboard.
I have another CLIPFORMAT I want to add to the clipboard, but I can't do it with OleSetClipboard() because the IDataObject* I receive does not implement SetData() method. So, to overcome this limitation I OpenClipboard() with GetClipboardOwner(), this way, I can SetClipboardData() to the clipboard without EmptyClipboard() first.
Now, it all works well, but what happens is that OleGetClipboard() does not return the data I placed in the clipboard using SetClipboardData(), but I can using GetClipboardData().
I can imagine why this happens (It just returns the IDataObject*), so I tried to OleFlushClipboard() to delete the IDataObject*, and OleGetClipboard() again to let the OS rebuild a new IDataObject*, and it still didn't contain the CLIPFORMAT added by SetClipboardData().
Does anyone have any idea how to overcome this issue? or a different trick? or even explain why it works this way? Thanks
I just tried this (on Windows 7) and it appears to work but only cross-process:
In a different process to the clipboard owner, OleGetClipboard returns a data object that contains all of the formats (i.e. the original formats from the data object and the extra ones added to the clipboard).
In the same process, OleGetClipboard always returns a data object that does not contain the extra clipboard formats.
In both cases, calling OleFlushClipboard makes no difference.
Anyway, this doesn't seem like a terribly robust solution. What you can do instead is create your own data object that responds to the formats it knows about and delegates other formats to the original data object. The EnumFormatEtc method would combine formats from both objects, and so on. This article has the skeleton of a simple data object you could extend.
im using gdi+ to output my images.
i tried to use the keyword new but it didn't work.
shot(L"image name") = new Image;
that didn't work any other ideas how to make it work
Something like this seems a lot more likely:
Image *image_object = new Image(L"Image Name");
Except that there's a pretty decent chance you don't want to allocate the Image object dynamically at all -- unless you really do, you generally want to just define an object with automatic storage duration:
Image image_object(L"Image Name");
The GDI+ Image class is not default constructible. You have to supply some parameters to the constructor in order to create one - see here.
You may well be better off using Gdiplus::Bitmap, which derives from Image. That seems more likely if you're trying to output it.