Solved

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

Posted on 2004-05-03
4
260 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
4 Comments
 
LVL 3

Expert Comment

by:akalmani
Comment Utility
0
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 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.

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now