Go Back   Cockos Incorporated Forums > REAPER Forums > ReaScript, JSFX, REAPER Plug-in Extensions, Developer Forum

Reply
 
Thread Tools Display Modes
Old 09-04-2014, 12:11 AM   #1
vtsaran
Human being with feelings
 
Join Date: Jan 2011
Posts: 64
Default Reaper crashes when using IP_ITEMNUMBER with GetSetMediaItemInfo API method

Hi,
I attempted to use IP_NUMBER with both GetSetMediaItemInfo and GetMediaItemInfo in order to retrieve the number of the current item, however, Reaper crashes every time I access this parameter. Interestingly enough, all other parameters seem to work just fine.
Here is a quick example of what I'm trying to do.

Code:
void SelectMoveToItem(int cid)
{
char text[50] = "";
MediaTrack* tr;
MediaItem* item;
int itemNumber;
double itemLength;

if (cid == cidSelectMoveNextItem)
Main_OnCommand(cidSelectMoveNextItem, 0);
else if (cid == cidSelectMovePrevItem)
Main_OnCommand(cidSelectMovePrevItem, 0);

item = GetSelectedMediaItem(0, 0);
itemNumber = *(int*)GetSetMediaItemInfo(item, "IP_ITEMNUMBER", NULL);
itemLength = *(double*)GetSetMediaItemInfo(item, "D_LENGTH", NULL);
sprintf(text, "item %d, length %.2f", itemNumber, itemLength);
ShowConsoleMsg(text);

}
In my crash log I get something like the following:

Code:
Process:         REAPER [3671]
Path:            /Applications/REAPER64.app/Contents/MacOS/REAPER
Identifier:      com.cockos.reaper
Version:         4.721
Code Type:       X86-64 (Native)
Parent Process:  launchd [189]
Responsible:     REAPER [3671]
User ID:         501

Date/Time:       2014-09-04 00:00:34.654 -0700
OS Version:      Mac OS X 10.9.4 (13E28)
Report Version:  11
Anonymous UUID:  8E8C5730-F53F-D9C7-62D6-CF1493BB7680


Crashed Thread:  0  Dispatch queue: com.apple.main-thread

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x0000000000000001

VM Regions Near 0x1:
--> 
    __TEXT                 0000000100000000-000000010081d000 [ 8308K] r-x/rwx SM=COW  /Applications/REAPER64.app/Contents/MacOS/REAPER

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   reaper_access.dylib           	0x0000000109fe1106 SelectMoveToItem(int) + 118 (reaper_access.cpp:636)
Any idea as to what may be happening here?

Thanks a lot.
Victor
__________________
--- --- ---
Visit me at https://www.youtube.com/vtsaran
vtsaran is offline   Reply With Quote
Old 09-04-2014, 05:11 AM   #2
Xenakios
Human being with feelings
 
Xenakios's Avatar
 
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 8,062
Default

edit : My previous post was probably completely wrong, so ignore it if you happened to see it.

I suggest :

Code:
int* itemNumber_ptr = (int*)GetSetMediaItemInfo(item, "IP_ITEMNUMBER", NULL);
int itemNumber=0;
if (itemNumber_ptr!=0)
{
  itemNumber=*itemNumber_ptr;
} else { //...whatever error handling or logging here... }
GetSetMediaItemInfo returns a pointer which could be null so it would be a very good idea to use that kind of a pattern to access the values always...(I've myself done some C++ function templates to deal with these.)

Code:
template<typename T>
T get_property(MediaItem* item,const char* propname,T defval=T())
{
    static_assert(!std::is_pointer<T>::value,"do not use this function with pointer properties");
    void* temp=GetSetMediaItemInfo(item,propname,nullptr);
    if (temp==nullptr)
        return defval;
    return *(T*)temp;
}
Use in code :

Code:
double item_length=get_property<double>(item,"D_LENGTH");
__________________
I am no longer part of the REAPER community. Please don't contact me with any REAPER-related issues.

Last edited by Xenakios; 09-04-2014 at 06:23 AM.
Xenakios is offline   Reply With Quote
Old 09-11-2014, 05:48 PM   #3
vtsaran
Human being with feelings
 
Join Date: Jan 2011
Posts: 64
Default

Hi.
Thanks for your response.
I switched to using a pointer and that did fix the crash. however, in order for me to do anything with the value returned by this function, I have to treat it by reference. The moment I tried reassigning it to a regular value, as in

Code:
int* itemNumberPtr = (int*)GetSetMediaItemInfo(item, "IP_ITEMNUMBER", NULL);
if (itemNumberPtr != NULL) {
int itemNumber = *itemNumberPtr;
// do something with the number
} else {
// simply return here
}
attempting to display the number or do something with it leads to a crash. So, for now, I am avoiding any conversions and just use the pointer to access the item number.
However, I still feel that it is not a normal behavior since all other parameters in this function can be casted. Why not this one?
__________________
--- --- ---
Visit me at https://www.youtube.com/vtsaran
vtsaran is offline   Reply With Quote
Old 09-13-2014, 06:55 AM   #4
Xenakios
Human being with feelings
 
Xenakios's Avatar
 
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 8,062
Default

Why do you need to use the IP_ITEMNUMBER, to begin with? Maybe it just isn't available to use under all circumstances...
__________________
I am no longer part of the REAPER community. Please don't contact me with any REAPER-related issues.
Xenakios is offline   Reply With Quote
Old 09-15-2014, 11:47 AM   #5
vtsaran
Human being with feelings
 
Join Date: Jan 2011
Posts: 64
Default Compiling SWS extension with Xcode 5.1 or higher

Quote:
Originally Posted by Xenakios View Post
Why do you need to use the IP_ITEMNUMBER, to begin with? Maybe it just isn't available to use under all circumstances...
Well, I need to find out the item number of a currently-selected item. Say, when I move from one item to the next, I would like to know what its number is. How would I do that otherwise if not through using IP_ITEMNUMBER parameter?
__________________
--- --- ---
Visit me at https://www.youtube.com/vtsaran
vtsaran is offline   Reply With Quote
Old 09-17-2014, 03:46 AM   #6
fingers
Human being with feelings
 
fingers's Avatar
 
Join Date: Dec 2009
Location: Wellington, NZ
Posts: 300
Default

From your crash log you are seg faulting when dereferencing a pointer with a value of 1.

I think from memory, IP_ITEMNUMBER is not a pointer like most other things returned from GetSetMediaItemInfo but the actual value.

So try:
Code:
int itemNumber = (int)GetSetMediaItemInfo(item, "IP_ITEMNUMBER", NULL);
fingers is offline   Reply With Quote
Old 09-27-2014, 12:21 PM   #7
Justin
Administrator
 
Justin's Avatar
 
Join Date: Jan 2005
Location: NYC
Posts: 15,721
Default

Yes, fingers is correct -- from the documentation:
Quote:
IP_ITEMNUMBER : int, item number within the track (read-only, returns the item number directly)
IP_ means integer cast to a pointer, so rather than a pointer to an integer, it is a pointer which should be used via (int)(INT_PTR)ptr.
Justin is offline   Reply With Quote
Old 09-27-2014, 06:39 PM   #8
vtsaran
Human being with feelings
 
Join Date: Jan 2011
Posts: 64
Default

Quote:
Originally Posted by fingers View Post
From your crash log you are seg faulting when dereferencing a pointer with a value of 1.

I think from memory, IP_ITEMNUMBER is not a pointer like most other things returned from GetSetMediaItemInfo but the actual value.

So try:
Code:
int itemNumber = (int)GetSetMediaItemInfo(item, "IP_ITEMNUMBER", NULL);
Hi, thanks for your suggestion.
I do not seem to be able to compile if I attempt to access IP_ITEMNUMBER in the way you described above. GetSetMediaTrackInfo returns a void pointer. The compiler complains that the cast to the plain integer cannot be performed (understandably so).
__________________
--- --- ---
Visit me at https://www.youtube.com/vtsaran
vtsaran is offline   Reply With Quote
Old 09-28-2014, 09:18 PM   #9
fingers
Human being with feelings
 
fingers's Avatar
 
Join Date: Dec 2009
Location: Wellington, NZ
Posts: 300
Default

Quote:
Originally Posted by vtsaran View Post
Hi, thanks for your suggestion.
I do not seem to be able to compile if I attempt to access IP_ITEMNUMBER in the way you described above. GetSetMediaTrackInfo returns a void pointer. The compiler complains that the cast to the plain integer cannot be performed (understandably so).
That's most likely because you can lose precision with 64-bit pointers. You may need to tweak your compiler settings.
fingers 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 01:14 AM.


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