Old 10-01-2018, 06:23 AM   #1
rich
Human being with feelings
 
Join Date: Feb 2010
Posts: 5
Default 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?
rich is offline   Reply With Quote
Old 10-01-2018, 12:43 PM   #2
Justin
Administrator
 
Justin's Avatar
 
Join Date: Jan 2005
Location: NYC
Posts: 15,721
Default

Quote:
Originally Posted by rich View Post
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
Justin is offline   Reply With Quote
Old 10-02-2018, 03:02 AM   #3
rich
Human being with feelings
 
Join Date: Feb 2010
Posts: 5
Default

Quote:
Originally Posted by Justin View Post
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!
rich is offline   Reply With Quote
Old 10-07-2018, 07:36 AM   #4
Justin
Administrator
 
Justin's Avatar
 
Join Date: Jan 2005
Location: NYC
Posts: 15,721
Default

Quote:
Originally Posted by rich View Post
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...
Justin is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 03:17 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.