[mapguide-commits] r10266 - in branches/4.0/MgDev: Common/MapGuideCommon/Services Common/PlatformBase/Services Server/src/UnitTesting

svn_mapguide at osgeo.org svn_mapguide at osgeo.org
Wed Nov 4 17:03:46 PST 2026


Author: jng
Date: 2026-11-04 17:03:45 -0800 (Wed, 04 Nov 2026)
New Revision: 10266

Modified:
   branches/4.0/MgDev/Common/MapGuideCommon/Services/ServerAdmin.cpp
   branches/4.0/MgDev/Common/MapGuideCommon/Services/Site.cpp
   branches/4.0/MgDev/Common/PlatformBase/Services/Warnings.cpp
   branches/4.0/MgDev/Server/src/UnitTesting/TestMisc.cpp
Log:
Fix warning getters that handed out uncounted objects, and a HasWarning() that was always true

MgWarnings::GetMessages() and MgServerAdmin::GetWarningsObject() returned their
Ptr<> member. A Ptr<> converts to a raw T* without AddRef, so the caller got a
borrowed pointer where every consumer treats the result as an owned reference:

  * The ten internal callers - SetWarning() in MgSite and in each proxy service -
    hold the result in a Ptr<>, which attaches without AddRef and releases on
    scope exit. Each was therefore consuming the owner's reference and leaving
    the owner with a dangling member, since MgStringCollection::Dispose() and
    MgWarnings::Dispose() are both "delete this".

  * The Java and .NET bindings wrap a returned pointer as owned (the javaout and
    csout typemaps pass ownMemory = true), so disposing the proxy, or collecting
    it, released a reference the wrapper never received. PHP records the result
    as borrowed and was the only tier that was correct.

Both now return SAFE_ADDREF(...), which is what MgService::GetWarningsObject(),
MgConnectionProperties::GetUserInfo and MgFeatureSet::GetClassDefinition already
did.

MgServerAdmin::HasWarning() and MgSite::HasWarning() answered "is the member
non-NULL", but both allocate that member in their constructor, so a fresh object
reported a warning it did not have. Both now ask the collection, as
MgService::HasWarnings() does.

  * Common/PlatformBase/Services/Warnings.cpp: GetMessages() returns a counted
    reference.

  * Common/MapGuideCommon/Services/ServerAdmin.cpp: GetWarningsObject() returns a
    counted reference, and HasWarning() reports recorded warnings rather than the
    member having been allocated.

  * Common/MapGuideCommon/Services/Site.cpp: the same HasWarning() correction.

  * Server/src/UnitTesting/TestMisc.cpp: WarningGetterOwnership asserts the
    returned refcount is 2 and that the owner is still usable after the caller
    releases its reference; HasWarningReportsRecordedWarnings covers both
    classes.

Reverting the three fixes fails all four assertions (2 == 1 twice, and both
!HasWarning() checks). Verified on ubuntu22 x64 with the Misc component in the
Debug/ASan tree and in a Release tree: "All tests passed (229 assertions in 19
test cases)" in both, with no LeakSanitizer record.


Modified: branches/4.0/MgDev/Common/MapGuideCommon/Services/ServerAdmin.cpp
===================================================================
--- branches/4.0/MgDev/Common/MapGuideCommon/Services/ServerAdmin.cpp	2026-11-05 01:02:06 UTC (rev 10265)
+++ branches/4.0/MgDev/Common/MapGuideCommon/Services/ServerAdmin.cpp	2026-11-05 01:03:45 UTC (rev 10266)
@@ -118,7 +118,7 @@
 /// </returns>
 MgWarnings* MgServerAdmin::GetWarningsObject()
 {
-    return m_warnings;
+    return SAFE_ADDREF(m_warnings.p);
 }
 
 ///////////////////////////////////////////////////////////////////////////////////
@@ -131,7 +131,9 @@
 /// </returns>
 bool MgServerAdmin::HasWarning()
 {
-    return m_warnings != NULL ? true : false;
+    // m_warnings is always allocated (see the constructor), so it says nothing about whether any
+    // warning has been recorded - ask the collection instead, as MgService::HasWarnings() does.
+    return m_warnings->GetCount() > 0 ? true : false;
 }
 
 ///////////////////////////////////////////////////////////////////////////////////

Modified: branches/4.0/MgDev/Common/MapGuideCommon/Services/Site.cpp
===================================================================
--- branches/4.0/MgDev/Common/MapGuideCommon/Services/Site.cpp	2026-11-05 01:02:06 UTC (rev 10265)
+++ branches/4.0/MgDev/Common/MapGuideCommon/Services/Site.cpp	2026-11-05 01:03:45 UTC (rev 10266)
@@ -62,7 +62,9 @@
 /// </returns>
 bool MgSite::HasWarning()
 {
-    return this->m_warning != NULL ? true : false;
+    // m_warning is always allocated (see the constructor), so it says nothing about whether any
+    // warning has been recorded - ask the collection instead, as MgService::HasWarnings() does.
+    return m_warning->GetCount() > 0 ? true : false;
 }
 
 //////////////////////////////////////////////////////////////////

Modified: branches/4.0/MgDev/Common/PlatformBase/Services/Warnings.cpp
===================================================================
--- branches/4.0/MgDev/Common/PlatformBase/Services/Warnings.cpp	2026-11-05 01:02:06 UTC (rev 10265)
+++ branches/4.0/MgDev/Common/PlatformBase/Services/Warnings.cpp	2026-11-05 01:03:45 UTC (rev 10266)
@@ -69,7 +69,7 @@
 ///
 MgStringCollection* MgWarnings::GetMessages()
 {
-    return m_arguments;
+    return SAFE_ADDREF(m_arguments.p);
 }
 
 //////////////////////////////////////////////////////////////////

Modified: branches/4.0/MgDev/Server/src/UnitTesting/TestMisc.cpp
===================================================================
--- branches/4.0/MgDev/Server/src/UnitTesting/TestMisc.cpp	2026-11-05 01:02:06 UTC (rev 10265)
+++ branches/4.0/MgDev/Server/src/UnitTesting/TestMisc.cpp	2026-11-05 01:03:45 UTC (rev 10266)
@@ -1429,4 +1429,58 @@
     }
 
     REQUIRE(failed == true);
-}
\ No newline at end of file
+}
+
+TEST_CASE("WarningGetterOwnership", "[Misc]")
+{
+    // A getter that hands out an object it owns must return a counted reference. Every caller in
+    // the tree holds the result in a Ptr<>, which attaches without AddRef and releases on scope
+    // exit, and the Java/.NET bindings wrap the result as an owned pointer and release it when the
+    // proxy is disposed. A borrowed pointer is therefore destroyed while its owner still holds it.
+    SECTION("MgWarnings::GetMessages")
+    {
+        Ptr<MgWarnings> warnings = new MgWarnings();
+        warnings->AddMessage(L"A warning");
+
+        MgStringCollection* messages = warnings->GetMessages();
+        REQUIRE(2 == messages->GetRefCount());
+        CHECK(1 == messages->Release());
+        CHECK(1 == warnings->GetCount());
+    }
+
+    SECTION("MgServerAdmin::GetWarningsObject")
+    {
+        Ptr<MgServerAdmin> serverAdmin = new MgServerAdmin();
+
+        MgWarnings* warnings = serverAdmin->GetWarningsObject();
+        REQUIRE(2 == warnings->GetRefCount());
+        CHECK(1 == warnings->Release());
+
+        // The owner must still hold a usable object after the caller released its reference
+        Ptr<MgWarnings> stillHeld = serverAdmin->GetWarningsObject();
+        REQUIRE(NULL != stillHeld.p);
+        CHECK(2 == stillHeld->GetRefCount());
+    }
+}
+
+TEST_CASE("HasWarningReportsRecordedWarnings", "[Misc]")
+{
+    // m_warnings/m_warning is allocated by the constructor, so "the member is not NULL" is not the
+    // question HasWarning() is being asked - it has to report a recorded warning. Both classes used
+    // to answer it with the member test, which made a fresh object claim it had warnings.
+    SECTION("MgServerAdmin::HasWarning")
+    {
+        Ptr<MgServerAdmin> serverAdmin = new MgServerAdmin();
+        Ptr<MgWarnings> warnings = serverAdmin->GetWarningsObject();
+        REQUIRE(0 == warnings->GetCount());
+        CHECK(!serverAdmin->HasWarning());
+    }
+
+    SECTION("MgSite::HasWarning")
+    {
+        Ptr<MgSite> site = new MgSite();
+        Ptr<MgWarnings> warnings = site->GetWarning();
+        REQUIRE(0 == warnings->GetCount());
+        CHECK(!site->HasWarning());
+    }
+}



More information about the mapguide-commits mailing list