COCKOS
CONFEDERATED FORUMS
Cockos : REAPER : NINJAM : Forums
Forum Home : Register : FAQ : Members List : Search :
Old 01-16-2018, 09:07 AM   #1
Derek Sapfe
Human being with feelings
 
Join Date: Jan 2018
Posts: 5
Default Problems with serialization

Hello,

I am developing my first plugin and ran to the following seemingly simple problem with serialization.

What I'm trying to do is save a string and fetch it on deserialize. I have a char array of 100 characters. The problem is, when I unserialize it I always get only nonsense. Other parameters etc. are correctly deserialized after it, so the length of the data seems not to be the issue.

Code:
char* file = new char[100];

bool BeatChopper::SerializeState(ByteChunk* pChunk) {
	TRACE;
	IMutexLock lock(this);

	if (!IsValidWaveFile(file)) {
		return false;
	}

	pChunk->Put(file); 
	return IPlugBase::SerializeParams(pChunk);
}

int BeatChopper::UnserializeState(ByteChunk* pChunk, int startPos) {
	TRACE;
	IMutexLock lock(this);
  
	char* filename = new char[100];
	startPos = pChunk->Get(filename, startPos);
	if (IsValidWaveFile(filename)) {
		// Never ends up here because filename contains only gibberish.
		strcpy(file, filename);
		ReadSoundFile(file);
	}

	return IPlugBase::UnserializeParams(pChunk, startPos);
}
Can you give some ideas what I may be doing wrong here?

Lot's of thanks in advance!
Derek Sapfe is offline   Reply With Quote
Old 01-16-2018, 09:26 AM   #2
Xenakios
Human being with feelings
 
Xenakios's Avatar
 
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 8,062
Default

pChunk->Put(file); just serializes the pointer which of course won't work. (The pointer isn't the data itself. To store the string you will probably need to do something like store the length of the string and then the contents of the string.)
__________________
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 01-16-2018, 06:00 PM   #3
Guod3
Human being with feelings
 
Guod3's Avatar
 
Join Date: Jan 2008
Posts: 506
Default

Quote:
Originally Posted by Xenakios View Post
pChunk->Put(file); To store the string you will probably need to do something like store the length of the string and then the contents of the string.)
I'm curious: will a null terminated string not work?
Guod3 is offline   Reply With Quote
Old 01-16-2018, 06:30 PM   #4
Xenakios
Human being with feelings
 
Xenakios's Avatar
 
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 8,062
Default

Quote:
Originally Posted by Guod3 View Post
I'm curious: will a null terminated string not work?
I suppose it could, but I would myself prefer the safety of the serialized data explicitly first having the information about how long the following string is going to be. In any case I would really prefer not to deal with these raw char* arrays and ByteChunks directly anyway.
__________________
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 01-17-2018, 03:36 AM   #5
stw
Human being with feelings
 
stw's Avatar
 
Join Date: Apr 2012
Posts: 279
Default

Try pChunk->PutStr() to serialize and pChunk->GetStr() to unserialize your string.
stw is offline   Reply With Quote
Old 01-18-2018, 07:08 AM   #6
jan hase
Human being with feelings
 
Join Date: Jul 2017
Posts: 25
Default

hey derek,

had the same problem a few feeks ago, this post helped me solving it:

https://forum.cockos.com/showthread.php?t=157732

but i didnt got it quite to work with oli's code, i had to add an "&" when unserialsing in front of my WDL_String (...&Text01,..)..still dont know, what this "&" is neccessary for, but it works for me ^^

startPos = pChunk->GetStr(&Text01, startPos);

and then copying it back to my Char with strcpy from Text01.Get()

strcpy(CharText01, Text01.Get());

Code:
WDL_String Text01;

char CharText01[4096] = "";

bool Plugname::SerializeState(ByteChunk* pChunk)
{
	TRACE;
	IMutexLock lock(this);

	Text01.Set(CharText01);

        pChunk->PutStr(Text01.Get());

	return IPlugBase::SerializeParams(pChunk); 
}

int Plugname::UnserializeState(ByteChunk* pChunk, int startPos)
{
	TRACE;
	IMutexLock lock(this);

        startPos = pChunk->GetStr(&Text01, startPos);

        strcpy(CharText01, Text01.Get());

	return IPlugBase::UnserializeParams(pChunk, startPos); 
}
jan hase is offline   Reply With Quote
Old 01-18-2018, 10:40 AM   #7
Derek Sapfe
Human being with feelings
 
Join Date: Jan 2018
Posts: 5
Default

Thanks for the answers! How stupid I was, serializing just the pointer.
Derek Sapfe 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:11 PM.


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