SDL clipboard and CEGUI clipboard

For help with general CEGUI usage:
- Questions about the usage of CEGUI and its features, if not explained in the documentation.
- Problems with the CMAKE configuration or problems occuring during the build process/compilation.
- Errors or unexpected behaviour.

Moderators: CEGUI MVP, CEGUI Team

andrewfeng123
Just popping in
Just popping in
Posts: 14
Joined: Sat Sep 02, 2017 20:28

SDL clipboard and CEGUI clipboard

Postby andrewfeng123 » Tue Dec 05, 2017 23:54

I am implementing a class that derives from CEGUI::NativeClipboardProvider.

I have no problem with sendToClipboard so far, but my implemenation of retriveveFromClipboard will have memory leak.

SDL's SDL_GetClipboardText() returns a UTF-8 char*, but it must be freed using SDL_free() later. The thing is I don't know when to delete the char* returned by SDL.

Here's my code:

Code: Select all

   void SDLClipboardProvider::retrieveFromClipboard(String & mimeType, void *& buffer, size_t & size)
   {
      mimeType = "text/plain";
      char* text = SDL_GetClipboardText();
      buffer = text;
      size = strlen(text);
   }

andrewfeng123
Just popping in
Just popping in
Posts: 14
Joined: Sat Sep 02, 2017 20:28

Re: SDL clipboard and CEGUI clipboard

Postby andrewfeng123 » Wed Dec 06, 2017 01:38

Ohhhhh!

I figured it out.

I just store the char* for now and manually call SDL_free() right after UIContext::injectPasteRequest()

here is the code for my SDLClipboardProvider class:

SDLClipboardProvider.h

Code: Select all

#include <SDL.h>
#include <CEGUI\CEGUI.h>
#include <iostream>

namespace CEGUI
{
   class SDLClipboardProvider : public NativeClipboardProvider
   {
   private:
      SDLClipboardProvider() {};
      char* text;
   public:
      //get the singleton class
      static SDLClipboardProvider* GetInstance()
      {
         static SDLClipboardProvider* inst = new SDLClipboardProvider();
         return inst;      //return singleton
      }
      ~SDLClipboardProvider() {};
      virtual void sendToClipboard(const String& mimeType, void* buffer, size_t size) ;
      virtual void retrieveFromClipboard(String& mimeType, void*& buffer, size_t& size) ;

      void Deallocate();
   };
}


SDLClipboardProvider.cpp

Code: Select all

#include "SDLClipboardProvider.h"

namespace CEGUI
{
   void SDLClipboardProvider::sendToClipboard(const String & mimeType, void * buffer, size_t size)
   {
      SDL_SetClipboardText(mimeType.c_str());
   }

   void SDLClipboardProvider::retrieveFromClipboard(String & mimeType, void *& buffer, size_t & size)
   {
      mimeType = "text/plain";
      text = SDL_GetClipboardText();
      buffer = text;
      size = strlen(text);
   }

   void SDLClipboardProvider::Deallocate()
   {
      SDL_free(text);
   }
   

}


Return to “Help”

Who is online

Users browsing this forum: No registered users and 1 guest