TwCopyCDStringToClientFunc (function)

void TwCopyCDStringToClientFunc(TwCopyCDStringToClient copyCDStringToClient)


This function is related to variables of type TW_TYPE_CDSTRING (C-Dynamic String). TwCopyCDStringToClientFunc must be used to provide a function that will be called by the AntTweakBar library to copy a C-dynamic string to the client application (ie. your program).

This function is required because memory allocated by a dynamic library (like AntTweakBar) cannot be resized or freed by its client application (your program) and vice versa. Thus the provided function is called by AntTweakBar to avoid bad memory handling between the two modules. If it is not provided, all variables of type TW_TYPE_CDSTRING will remain read-only.

In the other way, if your application needs to copy a C-dynamic string to AntTweakBar (for instance if you use callbacks to handle the variable via TwAddVarCB), call TwCopyCDStringToLibrary to copy it. See TwCopyCDStringToLibrary for an example.



The function that will be called by AntTweakBar to copy a C-dynamic string to the client application.

You should define this function like this:

void TW_CALL CopyCDStringToClient(char **destinationClientStringPtr, const char *sourceString)
  // Copy the content of souceString to a memory block pointed by *destinationClientStringPtr
  // The destination memory block should be allocated or resized to contain the string pointed by sourceString
  // ...

See below for an example of implementation of this function.


If you are using malloc, calloc, realloc, strdup and free to manage memory of your C-dynamic strings, you may implement CopyCDStringToClient like this:

void TW_CALL CopyCDStringToClient(char **destPtr, const char *src)
  size_t srcLen = (src!=NULL) ? strlen(src) : 0;
  size_t destLen = (*destPtr!=NULL) ? strlen(*destPtr) : 0;
  // alloc or realloc dest memory block if needed
  if( *destPtr==NULL )
    *destPtr = (char *)malloc(srcLen+1);
  else if( srcLen>destLen )
    *destPtr = (char *)realloc(*destPtr, srcLen+1);
  // copy src
  if( srcLen>0 )
    strncpy(*destPtr, src, srcLen);
  (*destPtr)[srcLen] = '\0'; // null-terminated string

Example of adding a C-dynamic string to a tweak bar:

char *s = strdup("a string allocated dynamically");
TwBar *bar = TwNewBar("TweakBar");
TwCopyCDStringToClientFunc(CopyCDStringToClient); // CopyCDStringToClient implementation is given above
// ...
TwAddVarRW(bar, "s", TW_TYPE_CDSTRING, &s, "");
// ...

See also