Can't FREE a CREATEd object?

Dear All,

Are there any instances where you cannot free a 'created' object? The only reason I ask is I have created an object and when I try to free it I get an access violation. Obviously I'm concerned that the object is created but never freed, using up resources.

I have never seen this behaviour before when using create/free.

But the scenario for this is slightly different to normal, for me anyway. I am using a class created by the WSDL importer.

Does this react any differently to a normal class create and free scheme?

The example I am working on is quite big, so I don't really want to post it all here. I hope I've given enough outline for someone bo be able to help? if you need more information, I can supply it.

Basically:

Created in the WSDL import unit:

------

type
  TransactionRequest   = class;
  TransactionRequest = class(TRemotable)
  private
    ...
  public
    ...
  published
    ...
  end;

-------

In my code, I have (simplistcally):

var
  objTransactionRequest : TransactionRequest;

begin
  objTransactionRequest:=TransactionRequest.Create;
  objTransactionRequest.Free;
end;

-----

It's the 'free' line that fails wth an access violation. This is something to do with being a TRemotable decendent?

What can I do to ensure the object is released correctly?

Thanks,

Trevor.
trevorbAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

developmentguruPresidentCommented:
 There are instances especially in COM programming where an object can be freed while you are still trying to use it, but I do not think that is what you are running into here.

  Place a break point in the destructor of the TransactionRequest object.  Most likely it is being free'd inbetween the time you create it and the time you try to free it.  If it stops on the breakpoint then you can look up the call stack to see what is causing it.
0
2266180Commented:
normally, you shouldn't get that error. are you sure you are not freeing the object twice?
it's pretty easy to check:
- either create a descendant and use that descendent to make the test, or modify the class destructor and add as the first line a logging line which will log some information to a file. for example
Log('instance beeing destroyed is '+inttostr(integer(self)));

then, check the log file and see if you have that log info more than once. if you do, it means somehwere else, somebody is destroying it as well.

some possible palces to look into:
- are you adding the object to an tobjectlist? is it created with owneobjects=true? if so, when you remove an object from the list, it will automatically be destroyed. same when you destroy the list.
- are you havgin any other similar classes to onjectlist which also destroy the ovjects?
- are you passing the object to some thread which operates on it? does that thread destroy teh object?
etc :)
0
trevorbAuthor Commented:
Hi to you both and thanks so far.

Had a look at the destructor that is failing and it is here:

destructor ConfirmationRequest.Destroy;
begin
  FreeAndNil(FResultID);
  FreeAndNil(FTransactionID); //  <<<<<<<< fails here
  FreeAndNil(FMerchantDetails);
  inherited Destroy;
end;

Which is kind of strange? Unless I've missed something glaring obvious as usual.

I thought it might be helpful to attach the unit generated by the WSDL importer, there's nothing in there that is top secret as far as I know.

In the code I create instances of

  objMerchantDetails:=MerchantDetails.Create;
  XSDec:=TXSDecimal.Create;
  objTransactionRequest:=TransactionRequest.Create;
  ServerResponse:=TransactionResponse.Create;
  objConfirmRequest:=ConfirmationRequest.Create;

and

  objConfirmRequest.TransactionID:=ServerResponse.TransactionID;

The first release I try is:

objConfirmRequest.Destroy;

Which fails as above.

I'm a little confused. I guess somewhere I need to create an instance of TransactionID. But, having said that, the code uses TransactionID works okay in the code?

Thanks for your help,

Trevor.

gateway.txt
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

developmentguruPresidentCommented:
Something is detroying the transaction ID.  Place a breakpoint on it's destructor to find out where it happens...
0
2266180Commented:
I think I know.

objConfirmRequest.TransactionID:=ServerResponse.TransactionID;

only assigns the addres to that object, does not create a new one. so when the object ServerResponse is beeign destroyed that reference is no longer valid. you shoudl either remove the FreeAndNil(FTransactionID); , unless the unti is generated,

or create a new instance of the ServerResponse.TransactionID object and assign that to the objConfirmRequest.TransactionID
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
trevorbAuthor Commented:
Thanks to you both, it all helped so I split the points.

I put the various breakpoints in and a logging method and I could see that some items were destroyed already or not even created but an attempt was made to destroy them.

And I can see now why that TransactionID assignment would cause problems.

I have now created completely autonomus objects for use throughout and it works fine now.

I also cheated a little. The code generated by the WSDL importer used TXSDecimal types. In the written specification for the XML interface, the parameters generated as TXSDecimal were simple strings. I took the risk and changed these throughout the generated code and removed the FreeAndNils for these old objects as they were causing errors too, and it all still works. I'm assuming that was valid to do as I would assume the call to the server would have failed if the type was incorrect to that expected. Using widestrings instead of the XSDecimal has kind of simplified the tidying up when the objects are destroyed.

Many thanks for your help, appreciated.

Trevor.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.