BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

events cause exceptions in ActiveX controls.

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (ActiveX)
View previous topic :: View next topic  
Author Message
Cagatay Undeger
Guest





PostPosted: Sat Jan 24, 2004 12:04 pm    Post subject: events cause exceptions in ActiveX controls. Reply with quote



Hi,

I have a problem on writing ActiveX controls in C++ Builder 5.
I have developed a builder component using TPanel, and installed into
component panel.
Then using that component I have created an activeX control.
But the ocx does not work properly in both c++builder and visual basic.
When an event is triggered from ocx the event causes an exception.
That is always the case for every event including BSTR.

As I found out, the error is cause when the leaving the event callback.
Thus the event is successfully executed but while leaving the function an
exception occurs,
even there is no function assigned to that event. I have tried changing some
of the options of ocx.
But could not manage to fix it. The exception probably caused at the point
shown below.
How can I solve the problem?

Thanks
Cagatay Undeger

void __fastcall TClientComXImpl::ReceiveName( AnsiString aname )
{
BSTR Tempaname;
Tempaname = WideString(aname).Copy();
Fire_OnReceiveName (Tempaname);
delete Tempaname; <<<<<<<<<<<<<<<<<<<<<<<<<< probably this call causes the
exception?????
}


Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Sat Jan 24, 2004 9:58 pm    Post subject: Re: events cause exceptions in ActiveX controls. Reply with quote




"Cagatay Undeger" <cagatay (AT) interaktif (DOT) gen.tr> wrote


Quote:
When an event is triggered from ocx the event causes
an exception. That is always the case for every event
including BSTR.

You are probably mismanaging the BSTR memory.

Quote:
delete Tempaname;

That is *not* the correct way to free a BSTR. The BSTR was not allocated
using the 'new' operator, so you should not be using the 'delete' operator
to free it. You are mixing memory managers, freeing memory using one
manager that was allocated with a different manager. That is why it blows
up on you. You must use the SysFreeString() function instead:

SysFreeString(Tempaname);

Better yet, since you are making use of WideString anyway, just let it
manage the memory for you since it already calls SysFreeString() internally
when it goes out of scope:

void __fastcall TClientComXImpl::ReceiveName( AnsiString aname )
{
WideString Tempaname( aname );
Fire_OnReceiveName( Tempaname );
}

Or simply:

void __fastcall TClientComXImpl::ReceiveName( AnsiString aname )
{
Fire_OnReceiveName( WideString(aname) );
}


Gambit



Back to top
Cagatay Undeger
Guest





PostPosted: Sun Jan 25, 2004 1:01 pm    Post subject: Re: events cause exceptions in ActiveX controls. Reply with quote



Himm, ok I will try this way.
But this code was an auto created code by builder itself.
Is that mean builder has a bug on creating activex controls?

Thanks
Cagatay

"Remy Lebeau (TeamB)" <gambit47.no.spam (AT) no (DOT) spam.yahoo.com> wrote

Quote:

"Cagatay Undeger" <cagatay (AT) interaktif (DOT) gen.tr> wrote in message
news:40125f21 (AT) newsgroups (DOT) borland.com...

When an event is triggered from ocx the event causes
an exception. That is always the case for every event
including BSTR.

You are probably mismanaging the BSTR memory.

delete Tempaname;

That is *not* the correct way to free a BSTR. The BSTR was not allocated
using the 'new' operator, so you should not be using the 'delete' operator
to free it. You are mixing memory managers, freeing memory using one
manager that was allocated with a different manager. That is why it blows
up on you. You must use the SysFreeString() function instead:

SysFreeString(Tempaname);

Better yet, since you are making use of WideString anyway, just let it
manage the memory for you since it already calls SysFreeString()
internally
when it goes out of scope:

void __fastcall TClientComXImpl::ReceiveName( AnsiString aname )
{
WideString Tempaname( aname );
Fire_OnReceiveName( Tempaname );
}

Or simply:

void __fastcall TClientComXImpl::ReceiveName( AnsiString aname )
{
Fire_OnReceiveName( WideString(aname) );
}


Gambit





Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Sun Jan 25, 2004 9:27 pm    Post subject: Re: events cause exceptions in ActiveX controls. Reply with quote


"Cagatay Undeger" <cagatay (AT) interaktif (DOT) gen.tr> wrote


Quote:
But this code was an auto created code by builder itself.

BCB does not auto-generate body contents for COM methods, only their
declarations and skeleton that you need to fill in yourself. Besides, the
fact that your function is using AnsiString as a parameter is something the
COM system does not use at all, so you must have added that yourself by hand
anyway.


Gambit



Back to top
Cagatay Undeger
Guest





PostPosted: Sun Jan 25, 2004 10:30 pm    Post subject: Re: events cause exceptions in ActiveX controls. Reply with quote

But I haven't writen any single line to ocx.
I have writen a normal builder component drived from TPanel.
Then installed the component to the components pallette.
And created an activex control from that component.
I didn't write any code inside ocx. builder did it.


"Remy Lebeau (TeamB)" <gambit47.no.spam (AT) no (DOT) spam.yahoo.com> wrote

Quote:

"Cagatay Undeger" <cagatay (AT) interaktif (DOT) gen.tr> wrote in message
news:4013bdd6 (AT) newsgroups (DOT) borland.com...

But this code was an auto created code by builder itself.

BCB does not auto-generate body contents for COM methods, only their
declarations and skeleton that you need to fill in yourself. Besides, the
fact that your function is using AnsiString as a parameter is something
the
COM system does not use at all, so you must have added that yourself by
hand
anyway.


Gambit





Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Mon Jan 26, 2004 2:32 am    Post subject: Re: events cause exceptions in ActiveX controls. Reply with quote

"Cagatay Undeger" <cagatay (AT) interaktif (DOT) gen.tr> wrote


Quote:
I have writen a normal builder component drived from TPanel.
Then installed the component to the components pallette.
And created an activex control from that component.
I didn't write any code inside ocx. builder did it.

Further investigation shows that it is, indeed, a bug in BCB's code
generator for ActiveX controls from VCL components. It also appears that
there is another bug regarding a memory leak for events that have AnsiString
parameters passed by reference. It also turns out that both bugs have
existed all the way back to BCB3 and still exist in BCB6.

Your only choice in this matter is to fix the generated code by hand. I
already gave you the code that fixes the AV issue you were originally
reporting. As for the memory leak, it occurs in code that looks like the
following:

void __fastcall TSomeClassXImpl::SomeEvent( AnsiString &Value )
{
BSTR TempValue;
TempValue = WideString(Value).Copy();
Fire_OnSomeEvent(&TempValue);
Value = (BSTR) TempValue; // <-- memory leak, the BSTR is not
freed
}

It should look more like the following instead:

void __fastcall TSomeClassXImpl::SomeEvent( AnsiString &Value )
{
WideString TempValue(Value);
Fire_OnSomeEvent(&TempValue);
Value = TempValue;
}

Both bugs have now been reported to Borland.


Gambit



Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Mon Jan 26, 2004 2:53 am    Post subject: Re: events cause exceptions in ActiveX controls. Reply with quote


"Remy Lebeau (TeamB)" <gambit47.no.spam (AT) no (DOT) spam.yahoo.com> wrote


Quote:
Both bugs have now been reported to Borland.

They are reported as issues #7069 and #7070 in QualityCentral.


Gambit



Back to top
Cagatay Undeger
Guest





PostPosted: Mon Jan 26, 2004 9:34 pm    Post subject: Re: events cause exceptions in ActiveX controls. Reply with quote

Thank you very much for your corrections.

There are 2 other bugs I have found also (Indeed I have found many in recent
years but forget now).

----------------------------------------------------------------------------

1) The methods returning AnsiString is writen incorrect. Thus a compiler
error occurs.

For example the method:
AnsiString GetAccountNick();
is written as:
STDMETHODIMP TChatClientComXImpl::GetAccountNick(BSTR* Value)
{
try
{
*Value = m_VclCtl->GetAccountNick(); >>>>>>>>>> AnsiString cannot be
assigned to WideString
/// I have fixed it as >>>>>>>> *Value =
WideString(m_VclCtl->GetAccountNick()).Copy();
/// But I am not sure if a memory leak accurs with this correction.
}
catch(Exception &e)
{
return Error(e.Message.c_str(), IID_IChatClientComX);
}
return S_OK;
};

----------------------------------------------------------------------------

2) There is another bug but not directly related with ActiveX Components,
but with normal components.
And Indeed it is a very very interesting bug, but generating the bug is not
so easy. I will describe the bug but I also need to sent a sample to you.
But your email server rejects my email.

****** For example if there are two function definitions like below:

typedef __fastcall void (__closure *TOnReceiveAccountAddess) ( int
account_id, AnsiString account_adr );
typedef __fastcall void (__closure *TOnAccountNickChanged) ( int account_id,
AnsiString nick );

****** And these types are used to define two published events in a
component like below:

protected:
TOnReceiveAccountAddess fOnReceiveAccountAddess;
TOnAccountNickChanged fOnAccountNickChanged;

__published:
__property TOnReceiveAccountAddess OnReceiveAccountAddess =

{read=fOnReceiveAccountAddess,write=fOnReceiveAccountAddess,default=NULL};
__property TOnAccountNickChanged OnAccountNickChanged =

{read=fOnAccountNickChanged,write=fOnAccountNickChanged,default=NULL};

****** When you want to use the OnAccountNickChanged event, the code builder
creates is as follows:

void __fastcall TForm1::ChatClientCom1AccountNickChanged(int account_id,
AnsiString account_adr)
{
}

****** The above code is wrong because the parameter name is wrong:
the parameter:
AnsiString account_adr
must be:
AnsiString nick

I tried to solve it but could not manage. Reordering the variables,
definitions etc. nothing changes the result.
But the wrong named parameter works correct. :-)

----------------------------------------------------------------------------

Best Regards,
Cagatay


Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Mon Jan 26, 2004 11:24 pm    Post subject: Re: events cause exceptions in ActiveX controls. Reply with quote

"Cagatay Undeger" <cagatay (AT) interaktif (DOT) gen.tr> wrote


Quote:
/// I have fixed it as >>>>>>>> *Value =
WideString(m_VclCtl->GetAccountNick()).Copy();
/// But I am not sure if a memory leak accurs with this correction.

No, it does not leak, as long as the caller is freeing the BSTR afterwards,
which it should be doing anyway.

Quote:
2) There is another bug but not directly related with ActiveX
Components, but with normal components.

That is a known and long-standing issue. Basically, because the two event
types have the exact same signature, the IDE can't distinquish between them
as it is only concerned with the actual signatures, not the parameter names.
When creating your own event types, it is best to always make sure that they
have different signatures. The easiest way is to just change the order of
the parameters. Otherwise, you'll have to change actual data types, or
introduce dummy parameters.

Quote:
I will describe the bug but I also need to sent a sample to you.
But your email server rejects my email.

You probably did not remove the anti-spam portions from the email address
first. Or your attachment was too large. Either way, though, please do not
send code privately unless specifically asked for. Always post to the
borland.public.attachments group instead.

Quote:
I tried to solve it but could not manage. Reordering the
variables, definitions etc. nothing changes the result.

Yes, it does.


Gambit



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (ActiveX) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.