[education/rkward/experimental/msvc_alloc] rkward/rbackend/rkwarddevice: Use R's calloc wrapper to avoid alloc/free mismatch when compiling with MSVC

Thomas Friedrichsmeier null at kde.org
Sat Apr 9 09:46:33 BST 2022


Git commit 2d7c6521c38bc11ae475b157c9d1b85edf1dc018 by Thomas Friedrichsmeier.
Committed on 09/04/2022 at 08:45.
Pushed by tfry into branch 'experimental/msvc_alloc'.

Use R's calloc wrapper to avoid alloc/free mismatch when compiling with MSVC

M  +2    -14   rkward/rbackend/rkwarddevice/rkgraphicsdevice_setup.cpp
M  +0    -12   rkward/rbackend/rkwarddevice/rkgraphicsdevice_stubs.cpp

https://invent.kde.org/education/rkward/commit/2d7c6521c38bc11ae475b157c9d1b85edf1dc018

diff --git a/rkward/rbackend/rkwarddevice/rkgraphicsdevice_setup.cpp b/rkward/rbackend/rkwarddevice/rkgraphicsdevice_setup.cpp
index e2541527..1c7b6227 100644
--- a/rkward/rbackend/rkwarddevice/rkgraphicsdevice_setup.cpp
+++ b/rkward/rbackend/rkwarddevice/rkgraphicsdevice_setup.cpp
@@ -45,10 +45,6 @@ struct RKGraphicsDeviceDesc {
 	QString default_family;
 	QString default_symbol_family;
 	pDevDesc rdevdesc;
-#ifdef _MSC_VER
-	// See RKD_Close()
-	pGEDevDesc rgdevdesc;
-#endif
 };
 
 #include "rkgraphicsdevice_stubs.cpp"
@@ -74,14 +70,10 @@ void RKStartGraphicsDevice (double width, double height, double pointsize, const
 	pDevDesc dev;
 	BEGIN_SUSPEND_INTERRUPTS {
 		/* Allocate and initialize the device driver data */
-#ifdef _MSC_VER
-		dev = (pDevDesc) malloc (sizeof (DevDesc));
-#else
-		dev = (pDevDesc) calloc (1, sizeof (DevDesc)); // don't really understand this, but R needs it this way (while MSVC hates it)
-#endif
+		dev = (pDevDesc) R_Calloc(1, DevDesc);
 		// NOTE: The call to RKGraphicsDeviceBackendTransmitter::instance(), here is important beyond error checking. It might *create* the instance and connection, if this is the first use.
 		if (!(dev && RKGraphicsDeviceBackendTransmitter::instance () && desc->init (dev, pointsize, family, bg))) {
-			free (dev);
+			R_Free (dev);
 			delete (desc);
 			desc = 0;
 		} else {
@@ -93,10 +85,6 @@ void RKStartGraphicsDevice (double width, double height, double pointsize, const
 			pGEDevDesc gdd = GEcreateDevDesc(dev);
 			gdd->displayList = R_NilValue;
 			GEaddDevice2(gdd, "RKGraphicsDevice");
-#ifdef _MSC_VER
-			// See RKD_Close()
-			desc->rgdevdesc = gdd;
-#endif
 		}
 	} END_SUSPEND_INTERRUPTS;
 
diff --git a/rkward/rbackend/rkwarddevice/rkgraphicsdevice_stubs.cpp b/rkward/rbackend/rkwarddevice/rkgraphicsdevice_stubs.cpp
index 1009c440..7ce64b61 100644
--- a/rkward/rbackend/rkwarddevice/rkgraphicsdevice_stubs.cpp
+++ b/rkward/rbackend/rkwarddevice/rkgraphicsdevice_stubs.cpp
@@ -404,18 +404,6 @@ static void RKD_Close (pDevDesc dev) {
 	{
 		RKGraphicsDataStreamWriteGuard guard;
 		WRITE_HEADER (RKDClose, dev);
-#ifdef _MSC_VER
-	// Ok, this is a terribly crude HACK, obviously, and it's just waiting to come back to bite us. However:
-	// We had to allocate the DevDesc in our own (MSVC-compiled) code (that's the way it is done), but if we allow R to delete
-	// it (also, as usual; in its MinGW-compiled code), we get a crash. Whatever the _exact_ reason. So what we do here is
-	// reset R's pointer, and do the free, ourselves. Fortunately, we can do this, and fortunately, the pointer is no longer
-	// needed at this point. At least not in R 3.2.3...
-	// If (or when) this breaks, we could try to just call some other device's init-routine, then hijack that device. Or out-source
-	// the RKGraphicsDevice backend init code into an R package...
-	// Or can we use R's Calloc/Malloc, instead? -> Manual caution not to use free() (only Free()), on that, though.
-		static_cast<RKGraphicsDeviceDesc*> (dev->deviceSpecific)->rgdevdesc->dev = NULL;
-	free (dev);
-#endif
 		delete static_cast<RKGraphicsDeviceDesc*> (dev->deviceSpecific);
 	}
 	{


More information about the rkward-tracker mailing list