methods deviceName() and plotDeviceList(), memory leak?

methods deviceName() and plotDeviceList(), memory leak?

Anonymous
Not applicable
707 Views
4 Replies
Message 1 of 5

methods deviceName() and plotDeviceList(), memory leak?

Anonymous
Not applicable

In objectARX program, to list and select AutoCAD plot device, there are two ways I found:

 

// Returns the device name or PC3 file name
class AcPlPlotConfigInfo's deviceName() method,
ACPL_PORT const ACHAR * deviceName() const;

 

// Returns an array of all available plot devices found on the system.
class AcDbPlotSettingsValidator's plotDeviceList() method.
virtual Acad::ErrorStatus plotDeviceList(AcArray<const ACHAR *> & deviceList) = 0;

 

But the strings returned by them can't be freed with acutDelString(), does that mean memory leak?

0 Likes
Accepted solutions (2)
708 Views
4 Replies
Replies (4)
Message 2 of 5

thierry_prince
Advocate
Advocate
Accepted solution

Hi,

When methods returns const ACHAR* values, it means that the values are not new copies of existing strings but the strings themselves.

This is explicitly specified by the const keyword.

In that case you shouldn't try to free them if you don't want AutoCAD to crash...

Cheers

0 Likes
Message 3 of 5

Anonymous
Not applicable

Thanks so much to thierry.prince! Your reply reminds me of a puzzle always in my mind when reading objectARX documents.

 

The question is: When returning string, why some methods/functions return *const* string pointer, and the others return *non-const* string pointer?

 

I realize now that there are two kind of strings to return:

 

1. For returning non-volatile, system-wide string, AutoCAD/objectARX allocates in advance this kind of limited number of fixed strings in memory, they always stay in memory, when returning, just gives the pointer to caller, there is no need for the caller to free the "returned" string. If the caller use acutDelString() to force "free" the returned string, AutoCAD will crash.

 

2. For returning Trivial and volatile string, objectARX lets the method/function allocate the string with malloc() (or something like) in heap memory, and makes the caller repsonsible for freeing the retured string in heap memory with free() (or something like).

 

Sorry for my poor English 😞

0 Likes
Message 4 of 5

tbrammer
Advisor
Advisor
Accepted solution

The ARX docs recommend not to store returned  const TCHAR *  pointers "for later use". Instead you should construct a CString or AcString object with them immediately because AutoCAD might release the pointers at some point.

For example Acad::ErrorStatus AcDbSymbolTableRecord::getName(ACHAR*& pName) const; will return a layer name or a block name in pName. If the layer name changes the pName pointer will become invalid.


Thomas Brammer ● Software Developer ● imos AGLinkedIn
If an answer solves your problem please [ACCEPT SOLUTION]. Otherwise explain why not.

0 Likes
Message 5 of 5

Anonymous
Not applicable

Thanks a lot to tbrammer for your complementation and correction! Yes, my guess was wrong, when ObjectARX returning *const* string, the returned string (or in other words more accurately, the string pointed by the retuned pointer) is not pre-allocated in advance and can not be assumed to be there always in memory! So,


1. For the returned *non-const* string, remember to call acutDelString() to free it after using it.


2. For the returned *const* string, copy it to your own string variable as soon as getting it.