Solved

COM and ASP: How to manage memory

Posted on 2002-03-08
7
328 Views
Last Modified: 2009-12-16
Can someone tell how memory operations work between C++ and VB/ASP. I am building a COM object that will be called from VB or ASP. I don't know if I have to insert code within the COM object for cleaning up allocated memory. The problem is shown below for a COM function, TestFunction. From C++ you can release the memory using SysFreeString(). How do I do the same from within ASP or VB?

C++ COM Code
-----------------------------------------
STDMETHODIMP MyComObject::TestFunctn(BSTR* TestMsg)
{
     AFX_MANAGE_STATE(AfxGetStaticModuleState())

     CString MyMessage("My name is James");    
     *TestMsg=MyMessage.AllocSysString();//Memory is allocated here
     return S_OK;
}  






C++ client code
-----------------------------------------
.
.
.
.
HRESULT hr=CoCreateInstance(.......pMyInstance..........);
.
.
BSTR TestMsg;  
HRESULT pMyInstance->TestFunctn(&TestMsg);//Call to TestFunctn with TestMsg as the argument.  
CString TheMessage(TestMsg);
::MessageBox(TheMessage);
::SysFreeString(TestMsg);//Memory is cleaned here
.
.



Equivalent ASP client Code
-----------------------------------------
Set pMyInstance = Server.CreateObject("MyComApplication.MyComObject")

'Function call to TestFunctn with TestMsg as the argument.
pMyInstance.TestFunctn TestMsg  
Response.Write "Message: " & TestMsg & "<br>"

'Where does one clean the memory?
0
Comment
Question by:aderounm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 4

Expert Comment

by:jtwine100697
ID: 6851299
IIRC, when dealing with automation languages (like ASP), the [IN] and [OUT] qualifiers in the IDL for your object dictate who owns what memory and when.

Take a quick look at an IDL reference on [IN] and [OUT], and I believe you will find your answer.

-=- James.
0
 
LVL 1

Expert Comment

by:MattC
ID: 6852718
As A personal rule I always set my instances of Server.Create Object to nothing

i.e., Set pMyInstance = Nothing

just to make sure ;-)
0
 
LVL 7

Accepted Solution

by:
peterchen092700 earned 200 total points
ID: 6854284
The protocol is fairly straightforward, and the scripting filter does much for you.

3 scenarios:


1) returning a string: server allocates and forgets, client frees

STDMETHODIMP MyComObject::Foo(/*[out]*/ BSTR* TestMsg)
{
  // you allocate the memory here, but don't store the pointer away
}  

scripting module will free the string when no longer needed


2) passing a string to the server: client allocates, server uses and forgets, client frees after call

STDMETHODIMP MyComObject::Bar(/*[in]*/ BSTR TestMsg)
{
   // leave TestMsg as is. If you need to remember it past the function call, make a copy
}  

scripting component allocates the string, passes to your function, frees the string

3) passing by reference - i.e. an in/out string: caller allocates; server uses may reallocate and forgets, client frees.

(this scenario is rare for scripting components)

STDMETHODIMP MyComObject::FooBar(/*[in,out]*/ BSTR *TestMsg)
{
  // you use and can change the contents of the string
  // if you need to re-allocate the string, free it before:
  ::SysFreeString(*TestMsg);
  *TestMsg = ::SysAllocString(L"Here's your string back");
}  

scripting component allocates the string, passes to your function, uses returned value, frees the string


Peter
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:makerp
ID: 6854789
any memory YOU allocate within your c++ code you must free/delete etc. if you are using BSTR to return values to asp in the OUT param of you method then just make sure you detach the BSTR
0
 
LVL 7

Expert Comment

by:peterchen092700
ID: 6855143
makerp: the wording of your comment might be confusing. Of course when you have to return a BST you must allocate but not free it. and you can't "detach a BSTR", you can only detach a BSTR from some wrapper class (like _bstr_t or CComBSTR).

Best Regards
Peter
0
 
LVL 11

Expert Comment

by:griessh
ID: 6955314
Dear aderounm

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept

     "peterchen"

comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points. The link to the Community Support area is: http://www.experts-exchange.com/commspt/

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 6

Expert Comment

by:Mindphaser
ID: 6979055
Force accepted

** Mindphaser - Community Support Moderator **
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question