MINDMAKERS Forum
Welcome, Guest. Please login or register.
September 08, 2010, 02:59:55 PM

Login with username, password and session length
Search:     Advanced search
NB: Spam bots are becoming smarter every day - we had to turn off regular registration. To become member, please send email to Kris Thorisson ([kris'_lastname] att ru dott is).
337 Posts in 99 Topics by 99 Members
Latest Member: peterwit
* Home Help Search Login Register
+  MINDMAKERS Forum
|-+  Projects
| |-+  Psyclone (Moderator: cmlabs)
| | |-+  posting messages with something in the object slot
« previous next »
Pages: [1] Print
Author Topic: posting messages with something in the object slot  (Read 2476 times)
mauman
Newbie
*
Posts: 13


View Profile Email
« on: December 01, 2006, 08:23:41 AM »

Hello all.
I'm trying to post messages with "something" in the Object field. I can compile the code but I get errors at runtime when the messages are posted. Here is an example of code, suppose I want to post a Collection object within the message:

Collection coll;
//add something to the collection
...
Message* tosend = new Message(this->name, "MyWhiteboard", "MessageType");
tosend->setObject((Object *)(&coll));
this->postMessage("MyWhiteboard",((Message *)tosend),""); //HERE I get the runtime error

The code is ok but I cannot successfully run it...

Do you have any suggestion about this problem?

Thanks,
Maurizio
Logged
thorlist
Jr. Member
**
Posts: 51


thorlist
View Profile WWW Email
« Reply #1 on: December 01, 2006, 10:13:00 AM »

Yes, the solution is easy. When you put an object into a Message, the Message now owns the object and will delete it somewhere down the line or hand over that responsibility to the receiver of the message.

What you are doing here is adding the address of a local variable (coll) to your Message and this means that coll will be deleted twice, both locally and by the Message/receiver.

Two solutions:

a) do instead:

   tosend->setObject(coll.clone());

or

b) do instead:

   Collection* coll = new Collection();
   tosend->setObject(coll);

Usually there is very little difference between the two, unless coll is very large


Best,

Thor
Logged
mauman
Newbie
*
Posts: 13


View Profile Email
« Reply #2 on: December 05, 2006, 04:21:04 PM »

I will try one of the 2 solutions, anyway I don't get the difference between the second one and the one I've posted in the example.
By the way, suppose I need to send something different from a collection object, let's say a wave file. I though I could proceed as follows: define a new class derived from Object and instantiate an object from this class; load the wave file in the object; add the object to the object slot of a Message. Is it correct?
Thanks,
Maurizio
Logged
thorlist
Jr. Member
**
Posts: 51


thorlist
View Profile WWW Email
« Reply #3 on: December 05, 2006, 06:57:12 PM »

Hi Maurizio,

A much easier way is to use the existing DataSample object. This object can hold large amounts of binary data and can be attached to a Message as it is an Object. This works well for both Java and C++ and works without problems across the network.

Defining new objects is a bit more complex and can be done, but then there are issues regarding Java and C++ compatibilities.

Best,

Thor
Logged
Pages: [1] Print 
« previous next »
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.4 | SMF © 2006-2007, Simple Machines LLC Valid XHTML 1.0! Valid CSS!