3

I'm again in a situation where I've spend an obscene amount of time on trying to customize datasnap callback samples to my needs. I'm old school OOP programmer and have several very large Object hierakies in my "toolbox" PODO style :-) .. and having this great datasnap feature, I want to utilize the forces of the callback.

BUT - when I implement it ... it simply fails ... (FASTMM4 reports mem leaks).

Try and create a simple VCL datasnap server - TCP. And add a button and this source ...

procedure TForm1.Button1Click(Sender: TObject);
var
//  AObject : TObject;
  aJSONVal : TJSONValue;
begin
//  AObject := TObject.Create;
//  ServerContainer1.DSServer1.BroadcastObject('SomeChannel','SomeCallbackID', AObject);
//  AObject.Free;

  aJSONVal := TJSONObject.Create;
  ServerContainer1.DSServer1.BroadcastMessage('SomeChannel','SomeCallbackID',aJSONVal);
  // aJSONVal.Free; // Mat pointed out that this is done by the broadcast.
end;

It will work - as long as you keep using TJSONValue ... But try and switch the commented code - and you will see what I mean.

I could of course change all my existing code to JSON ... but that is simply not acceptable.

Does anyone have any idea on how to use the BroadcastOBJECT or NotifyOBJECT ?

Regards Bjarne

Bimmer_R
  • 588
  • 9
  • 18
  • @Bimmer_R- Suggestion: please explain to us **how it fails** - what happens? What exception is Delphi raising? What error message are you getting? I don't think you'll get many answers by simply telling us 'it simply fails' and asking us to go and build a datasnap server and find out what does or doesn't happen. – Vector May 08 '11 at 07:48
  • @Mikey ... Sorry .. of course I should provide all info needed. When I say fails - it means Acces violation .. or as I pointed out. Fastmm4 reports mem leaks. To create a datasnap server app. you actually need so few steps/cliks that its allmost as quick as starting a new vcl forms app :-) Here is a great example of how to setup a datasnap server - and use callback. http://edn.embarcadero.com/article/41374 The only difference as I see, is that Im broadcasting from the server ... but that really shouldn't be a problem .. or is it? – Bimmer_R May 08 '11 at 18:40

2 Answers2

2

The object which you give to a Notify or Broadcast call is then owned by that call. Therefore do not call "AObject.Free;" or "aJSONVal.Free;". Doing so will often result in an Access Violation or other memory management related problems.

Note also that Broadcasted messages get put in a queue on the server and are later sent, in a different thread. Meaning, when your call to Broadcast returns, it hasn't actually sent the message to all the clients yet.

I hope that helps,

Mat

Mat DeLong
  • 297
  • 4
  • 17
  • Hi Mat. Thanks for your interest in this. I've modified the CallbackChannels demo program - and put it on megaupload (didn't know how to share files via SO - if its possible). http://www.megaupload.com/?d=BL0SZC98 Remember - doing callback broadcast to a callbackid ... still won't work. But a broadcast to a channel is OK. And a notifyobject to a callbackid/clientid is also OK. I hope you find the demo usefull. – Bimmer_R May 14 '11 at 07:52
0

Possible answer: Your question was vague but based on what you've said, I'd start here:

Delphi XE help: (ms-help://embarcadero.rs_xe/vcl/DSServer.TDSServer.BroadcastObject.html):

function BroadcastObject(const ChannelName: String; const CallbackId: String; const Msg: TObject): boolean; overload;

The second overload sends an object to all client callbacks with a given registered callback identifier. For this purpose, an additional CallbackId parameter is required in the call."

You are using the second overload which takes 3 params - are your callback identifiers set up properly?

Vector
  • 10,879
  • 12
  • 61
  • 101
  • Hi Mikey. Your correct - there are two overloads for each of the broadcast methods. But as I read the "instructions" .. a broadcast does not need any "setup". You can broadcast anything - anytime you want. If noone listens, then it doesn't matter. And with 4 possible broadcasts - I can only get one to work. All others fail with access violation or mem leaks. – Bimmer_R May 08 '11 at 18:20
  • @Bimmer_R: I wouldn't call mem leaks failures per se. But AV's-are they generated on client side or server side? How is your deployment set up? Do you have clients on remote machines or everything local? Can you trace through the entire execution path until the AV? What version of Delphi are you using? Have you got 'break on Delphi language expections' turned on in your IDE? As I'm sure you know, an AV often masks the real problem. If an exception is generated on a remote client, it may surface as an AV from the server call that masks the actual client side exception. Please - details! HTH – Vector May 08 '11 at 20:03
  • @Bimmer_R - and I gather that when you say 'it simply fails' you mean that sometimes NOTHING happens at all, except that you see a leak reported, correct? "I can only get one to work. All others fail with access violation or mem leaks" - what's different about the one that works? That may lead you to your answer. – Vector May 08 '11 at 20:09
  • @Mikey. Hi again - I have something working now. And I would like to share my knowledge. Doing BroadCastObject to a channel (all callbacks/clients) is pretty easy to do. And aparently so is NotifyObject (specifik channel, callback, client). Where Im failing is the BroadCastObject to a specifik Callback. For some reason it wont work. I've modified the embacadero CallBackChannels demo program and mailed it to Pawel Glowacki, with whom I've just attended a 2 day workshop on this area. Let me know if anyone would like to get the modified version of the ChannelCallbackDemo. – Bimmer_R May 12 '11 at 13:21
  • @Bimmer_R - Thanks for the accept - I didn't really answer anything but I guess I was going in the right direction? Now, this modified version does what you need? If so I'd like to see it - not using this feature now but it is very powerful and would like to know how to use it correctly if necessary. Tnx – Vector May 12 '11 at 14:14
  • @Bimmer_R - where did you attend this workshop? Part of an embacadero program? Online or face to face workshop? Tnx. – Vector May 12 '11 at 14:17
  • @Mikey. I've send the modified version to Pawel. And I could easily send it to you too :-) And yes - it was face to face on a very relaxed two day workshop here in Denmark. – Bimmer_R May 12 '11 at 20:05
  • yes, well lucky you - Delphi is a lot stronger today in europe than USA (where I am) - Developer Days didn't even bother coming to NYC! MS is so dominant here - and USA has always been more C language oriented - Europe has deep Pascal roots. Please send what you put together - not sure how you do it on SO... – Vector May 12 '11 at 22:04
  • Hi Mikey. I just put the complete modified version on megaupload here : http://www.megaupload.com/?d=BL0SZC98 – Bimmer_R May 14 '11 at 07:55