Solved

How to export char** in a class method in a DLL?

Posted on 2004-05-03
4
294 Views
Last Modified: 2012-08-14
I have a class exported as such

class  __declspec(dllexport) SigObject
{
public:
  void GetUsername(char **username)
  {
    *username = new TCHAR[10];
    strcpy(*username,"Hello");
  }
}

I include this class file and compile in a main program as such:
int main()
{
SigObject *myObject;
myObject = new SigObject();
char *username = NULL;
myObject->GetUsername(&username);
printf("\nUsername: %s\n",username);

delete[] username;
delete myObject;
}

which runs without error.  However, when I create the class file as a DLL and include the .lib file into the main program and call it, I get an exception at "delete[] username".  Can anyone help me to understand why?
0
Comment
Question by:pcssecure
[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
4 Comments
 
LVL 3

Expert Comment

by:akalmani
ID: 10976361
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10976426
Generally, memory allocated in Dll should be released in the same Dll. Add function ReleaseUserName(char*):

public:
  void GetUsername(char **username)
  {
    *username = new TCHAR[10];
    strcpy(*username,"Hello");
  }
 
   void DeleteUserName(char* username)
   {
        delete[] username;
   }
}

Client code:

int main()
{
SigObject *myObject;
myObject = new SigObject();
char *username = NULL;
myObject->GetUsername(&username);
printf("\nUsername: %s\n",username);

myObject->DeleteUserName(username);
delete myObject;
}

Another way is using of clller-allocated buffer.


0
 
LVL 9

Expert Comment

by:Cayce
ID: 10977825
Everything posted above is ok, and the proper answer to your question.

However I would advise you on forgetting about C strings and using the better C++ strings:

class  __declspec(dllexport) SigObject
{
private:
  string username;
public:
  string GetUsername()
  {
    username = "hello, whatever";
    return username;
  }
}

int main()
{
  SigObject *myObject;
  myObject = new SigObject();
  string username;
  username = myObject->GetUsername();
  printf("\nUsername: %s\n",username.c_str());
  delete myObject;
}
0
 
LVL 2

Accepted Solution

by:
vhss earned 500 total points
ID: 10985253
The problem is trying to free memory that was in private heap, which works when compiled without the DLL as compared to freeing memory in default process heap when compiled with DLL.

Apparently, your class does the allocation of memory, not the caller and you are passing the memory out.  To do this, you should use CoTaskMemAlloc/CoTaskMemFree instead.

Refer to:
http://archives.neohapsis.com/archives/microsoft/various/dcom/2001-q1/0182.html

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

756 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