Solved

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

Posted on 2004-05-03
4
300 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

Technology Partners: 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!

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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

705 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