|
|
|
10-01-2018, 06:23 AM
|
#1
|
Human being with feelings
Join Date: Feb 2010
Posts: 5
|
Memory management and strings
Hi everyone, bit of a newbie API question.
With a number of the Reaper C++ API functions that take a char* argument, for example GetSetMediaTrackInfo_String(), it appears that the memory the char* parameter points to is expected to outlive the call.
At any rate, if I use the function to set track names and immediately free my name buffer after the call, I end up corrupted track names in Reaper.
So I was wondering how this memory should be managed?
1) Don't allocate it yourself & blindly write into the pointer provided when you 'Get' the value - Haven't tried this as I've no idea of the size of the buffer that backs the pointer provided by Reaper or whether that's in any way safe.
2) Allocate and free yourself, but how do you know when the memory is no longer in use?
3) Something obvious I've missed
On a related note, does anyone know the meaning of 'NeedsBig' in strNeedsBig?
|
|
|
10-01-2018, 12:43 PM
|
#2
|
Administrator
Join Date: Jan 2005
Location: NYC
Posts: 15,721
|
Quote:
Originally Posted by rich
Hi everyone, bit of a newbie API question.
With a number of the Reaper C++ API functions that take a char* argument, for example GetSetMediaTrackInfo_String(), it appears that the memory the char* parameter points to is expected to outlive the call.
At any rate, if I use the function to set track names and immediately free my name buffer after the call, I end up corrupted track names in Reaper.
So I was wondering how this memory should be managed?
1) Don't allocate it yourself & blindly write into the pointer provided when you 'Get' the value - Haven't tried this as I've no idea of the size of the buffer that backs the pointer provided by Reaper or whether that's in any way safe.
2) Allocate and free yourself, but how do you know when the memory is no longer in use?
3) Something obvious I've missed
|
The buffer passed in order to set does not need to outlive the call. Perhaps there's some other bug in your code, maybe post your code?
Quote:
On a related note, does anyone know the meaning of 'NeedsBig' in strNeedsBig?
|
It's a comment for the reascript parser, pretty much ignore it
|
|
|
10-02-2018, 03:02 AM
|
#3
|
Human being with feelings
Join Date: Feb 2010
Posts: 5
|
Quote:
Originally Posted by Justin
The buffer passed in order to set does not need to outlive the call. Perhaps there's some other bug in your code, maybe post your code?
|
Ah brilliant, is that the same everywhere? I think the first place I noticed a problem was with the action description when registering via reaper_plugin_info_t::Register("gaccel", void* infostruct)
Thinking about it, it makes sense for registered infostructs in general to need to stick around as in some cases they include callbacks or can be unregistered? Or quite possibly I'm just doing something stupid in multiple places
Anyway, now I know the problem is at my end (it's usually the way) I'll go back and check my code. Will post a minimal example if I can't figure out where I've gone wrong (would be a bit sprawling at the moment)
Thanks for your help!
|
|
|
10-07-2018, 07:36 AM
|
#4
|
Administrator
Join Date: Jan 2005
Location: NYC
Posts: 15,721
|
Quote:
Originally Posted by rich
Ah brilliant, is that the same everywhere? I think the first place I noticed a problem was with the action description when registering via reaper_plugin_info_t::Register("gaccel", void* infostruct)
Thinking about it, it makes sense for registered infostructs in general to need to stick around as in some cases they include callbacks or can be unregistered? Or quite possibly I'm just doing something stupid in multiple places
|
Hard to say "everywhere", but anywhere that you're not registering something, probably.
Those infostructs do need to persist, as do any strings they point to. Normally those strings are constants which exist statically anyway...
|
|
|
Thread Tools |
|
Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -7. The time now is 03:17 PM.
|